使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器

来源:互联网 发布:人工智能作业答案 编辑:程序博客网 时间:2024/04/30 15:41

http://blog.csdn.net/xdwyyan/article/details/43198985



主要步骤及命令记录:

1、下载nginx,地址http://nginx.org/en/download.html,选择最新版本下载、解压。当前最新版为:nginx-1.7.9。

2、为了增加对rtmp的支持下载nginx-rtmp-module,地址:https://github.com/arut/nginx-rtmp-module#example-nginxconf,这个是个开源项目。解压后,为了和我在网上看到的教程同步,我改了文件夹名字,将其改成了nginx-rtmp-module。然后将其放到/home/user/目录下(今天才知道h和user非常像的目录usr是Unix System Resource的意思)。copy

cp -rf 123/nginx-rtmp-module/ /home/user/ 

3、进入到nginx-1.7.9文件夹目录下,执行如下命令:copy
  1.  

./configure --prefix=/usr/local/nginx  --add-module=/home/user/nginx-rtmp-module  --with-http_ssl_module 

4、待上述命令执行完成之后,执行如下命令安装:copy
  1. make  
5、make完成之后,执行:

copy
  1. make install  
6、希望能顺利完成以上步骤。安装完成后,打开nginx的配置文件nginx.conf进行配置:在root前提下执行下面命令打开nginx.confcopy
  1. gedit /usr/local/nginx/conf/nginx.conf  
7、nginx.conf中增加rtmp的支持,下面是修改过得文件,以后可能还会修改更新:
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.     rtmp {    
  16.         server {    
  17.             listen 1935;    
  18.         
  19.             application myapp {    
  20.                 live on;    
  21.             }    
  22.             application hls {    
  23.                 live on;    
  24.                 hls on;    
  25.                 hls_path /tmp/hls;    
  26.             }    
  27.         }    
  28.     }    
  29.   
  30.   
  31. http {  
  32.     include       mime.types;  
  33.     default_type  application/octet-stream;  
  34.   
  35.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  36.     #                  '$status $body_bytes_sent "$http_referer" '  
  37.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  38.   
  39.     #access_log  logs/access.log  main;  
  40.   
  41.     sendfile        on;  
  42.     #tcp_nopush     on;  
  43.   
  44.     #keepalive_timeout  0;  
  45.     keepalive_timeout  65;  
  46.   
  47.     #gzip  on;  
  48.   
  49.     server {  
  50.         listen       80;  
  51.         server_name  localhost;  
  52.   
  53.         #charset koi8-r;  
  54.   
  55.         #access_log  logs/host.access.log  main;  
  56.   
  57.         location / {  
  58.             root   html;  
  59.             index  index.html index.htm;  
  60.         }  
  61.   
  62.         #error_page  404              /404.html;  
  63.   
  64.         # redirect server error pages to the static page /50x.html  
  65.         #  
  66.         error_page   500 502 503 504  /50x.html;  
  67.         location = /50x.html {  
  68.             root   html;  
  69.         }  
  70.   
  71.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  72.         #  
  73.         #location ~ \.php$ {  
  74.         #    proxy_pass   http://127.0.0.1;  
  75.         #}  
  76.   
  77.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  78.         #  
  79.         #location ~ \.php$ {  
  80.         #    root           html;  
  81.         #    fastcgi_pass   127.0.0.1:9000;  
  82.         #    fastcgi_index  index.php;  
  83.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  84.         #    include        fastcgi_params;  
  85.         #}  
  86.   
  87.         # deny access to .htaccess files, if Apache's document root  
  88.         # concurs with nginx's one  
  89.         #  
  90.         #location ~ /\.ht {  
  91.         #    deny  all;  
  92.         #}  
  93.     }  
  94.   
  95.   
  96.     # another virtual host using mix of IP-, name-, and port-based configuration  
  97.     #  
  98.     #server {  
  99.     #    listen       8000;  
  100.     #    listen       somename:8080;  
  101.     #    server_name  somename  alias  another.alias;  
  102.   
  103.     #    location / {  
  104.     #        root   html;  
  105.     #        index  index.html index.htm;  
  106.     #    }  
  107.     #}  
  108.   
  109.   
  110.     # HTTPS server  
  111.     #  
  112.     #server {  
  113.     #    listen       443 ssl;  
  114.     #    server_name  localhost;  
  115.   
  116.     #    ssl_certificate      cert.pem;  
  117.     #    ssl_certificate_key  cert.key;  
  118.   
  119.     #    ssl_session_cache    shared:SSL:1m;  
  120.     #    ssl_session_timeout  5m;  
  121.   
  122.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  123.     #    ssl_prefer_server_ciphers  on;  
  124.   
  125.     #    location / {  
  126.     #        root   html;  
  127.     #        index  index.html index.htm;  
  128.     #    }  
  129.     #}  
  130.   
  131. }  
8、保存完配置文件之后,启动nginx服务copy
  1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
9、启动时可能会遇到端口占用的问题,copy
  1. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  2. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  3. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  4. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  5. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  6. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  7. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  8. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  9. nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)  
  10. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)  
  11. nginx: [emerg] still could not bind()  
上述的显示是因为我的nginx服务已经在运行了,所以再启动时1935和80端口会占用,可以使用下面命令来终止服务:copy
  1. pkill -9 nginx  
当遇到不是因为服务已经启动造成的端口占用时可以通过下面命令来查看是哪个线程占用的端口,然后杀死他(例如端口号1935):copy
  1. <pre name="code" class="plain">netstat -tlnp|grep 1935  
然后系统会显示:copy
  1. tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      20211/nginx.conf  
注意到这个20211就是线程号,后面跟着的是服务名字,也就是说nginx占用着1935端口,可以使用下面命令杀死他:
  1. kill -9 20211  
10、然后再启动服务,当服务启动成功的话,在本机浏览器输入:http://localhost/copy
  1. Welcome to nginx!  
  2.   
  3. If you see this page, the nginx web server is successfully installed and working. Further configuration is required.  
  4.   
  5. For online documentation and support please refer to nginx.org.  
  6. Commercial support is available at nginx.com.  
  7.   
  8. Thank you for using nginx.  
如果出现上述内容,则服务成功启动。

11、可以通过netstat -ltn 命令可以看到端口的监听情况.copy

  1. 激活Internet连接 (仅服务器)  
  2. Proto Recv-Q Send-Q Local Address           Foreign Address         State        
  3. tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN       
  4. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN       
  5. tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN       
  6. tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN       
  7. tcp6       0      0 ::1:631                 :::*                    LISTEN       
80是nginx默认的http监听端口。

12、用ffmpeg推流到nginx,myapp上,我推送的是网络摄像机的rtsp直播视频流。用如下命令:copy

  1. ffmpeg -i rtsp://admin:12345@172.27.35.56 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://172.27.35.2:1935/myapp/test1  

更正为:

ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -f flv rtmp://127.0.0.1:1935/myapp/test1
来源于:https://trac.ffmpeg.org/wiki/Capture/Webcam

ffmpeg -loop 1 -pattern_type glob -i '350_frame/*.png' -c:v libx264 -t 600 -pix_fmt yuv420p -f flv rtmp://www.matchyun.com:1935/myapp/test1

上述命令参照http://www.tuicool.com/articles/eAfIVv具体参数还有待修改。

在VLC上打开网络串流 ,填写 rtmp://172.27.35.2:1935/myapp/test1可以看到监控画面,有大约2~3秒的延时。


sudo apt-get autoremove autoconf automake build-essential cmake libass-dev libfreetype6-dev \  libmp3lame-dev libopus-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev \  libvorbis-dev libvpx-dev libx264-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev mercurial texinfo zlib1g-dev


0 0
原创粉丝点击