nginx 搭建 rtmp

3.搭建nginx服务器

参考博客

开发环境

Ubuntu 16.04 server

nginx-1.8.1

nginx-rtmp-module

nginx的服务器的搭建

下载nginx

mkdir ~/code
cd ~/code
wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压

tar -zxf nginx-1.18.0.tar.gz -C ~/code/

下载nginx-rtmp-module

git clone https://github.com/arut/nginx-rtmp-module.git

 

安装nginx的依赖库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev

配置并编译nginx

使用nginx的默认配置,添加nginx的rtmp模块。

 

cd ~/code/nginx-1.8.1/
./configure --add-module=../nginx-rtmp-module
make
sudo make install

运行测试nginx

进入安装目录

cd /usr/local/nginx

运行命令

sudo ./sbin/nginx

注意:以后所有的命令都在/usr/local/nginx目录运行,也是nginx配置文件的相对目录。

打开浏览器在地址栏输入:139.199.66.124。如果,如果显示 welcome to nginx 那样就证明您的nginx服务器搭建成功了。

 

4.点播视频服务器的配置

通过上一步nginx服务器已经搭建完成,然后我们就可以开启一个视频点播的服务了。打开配置文件nginx.conf,添加RTMP的配置。

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {                #RTMP service
    server {
        listen 1935;  #port
        chunk_size 4096;   #data size

        application vod {
            play /home/ubuntu/Videos; #video path
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    
    keepalive_timeout  65;
    
    server {
        listen       80;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

}

发送测试用的视频文件到服务器

rong@rong-ubuntu-07:~/Videos$ scp testfile.mp4 ubuntu@139.199.66.124:/home/ubuntu/Videos
ubuntu@139.199.66.124's password: 
testfile.mp4                                  100%  908KB 908.4KB/s   00:00

 

文件放好之后,那就让我们重新启动一下nginx

sudo ./sbin/nginx -s reload

打开视频播放软件选用的是VLC media-> open network stream….

点击play就可以播放了。
当然点播不使用RTMP插件nginx自身也是可以实现点播服务的。那就是配置location部分,由于下面我们要配置直播和回看功能所以选用了RTMP服务。

5.直播视频服务器的配置

接着我们就在点播服务器配置文件的基础之上添加直播服务器的配置。一共2个位置,第一处就是给RTMP服务添加一个application这个名字可以任意起,也可以起多个名字,由于是直播我就叫做它live吧,如果打算弄多个频道的直播就可以live_cctv1、live_cctv2名字任意。第二处就是添加两个location字段,字段的内容请直接看文件吧。

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {                #RTMP service
    server {
        listen 1935;  #port
        chunk_size 4096;   #data size

        application vod {
            play /home/ubuntu/Videos; #video path
        }

        application live{ #first live field
            live on;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location /stat {     #first location field
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #second location field
            root /home/ubuntu/code/nginx-rtmp-module/;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

}

添加完这两处之后,重新启动nginx打开浏览器 xxx.xxx.xxx.xxx/stat

 

 

有没有看到live字样呢?如果可以显示出来,证明你的配置生效了。试一下推流到“rtmp://139.199.66.124/live/test”

 

 

 

播放的地址就是“rtmp://139.199.66.124/live/test”,如果您本地有支持rtmp协议的播放器就可以试试了。

6.实时回看视频服务器的配置

我们想一想如果直播服务能够把节目录制在本地,我们不就可以直接进行回看先前的节目了吗?回看一分钟、一小时甚至一天的。想想就兴奋不用写代码有现成的可以使用。怎么用呢?继续看nginx的配置吧。

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application vod {
            play /home/ubuntu/Videos;
        }

        application live{
            live on;
            hls on;    #这个参数把直播服务器改造成实时回放服务器。
            wait_key on;  #对视频切片进行保护,这样就不会产生马赛克了。
            hls_path /home/ubuntu/Videos/hls;  #切片视频文件存放位置。
            hls_fragment 10s;     #每个视频切片的时长。
            hls_playlist_length 60s;  #总共可以回看的事件,这里设置的是1分钟。
            hls_continuous on; #连续模式。
            hls_cleanup on;    #对多余的切片进行删除。
            hls_nested on;     #嵌套模式。
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /home/ubuntu/code/nginx-rtmp-module/;
        }
        
        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /home/ubuntu/Videos/hls;
            expires -1;
            add_header Cache-Control no-cache;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

}

1.添加完成后需要重新启动nginx,由于这次nginx需要向服务器写切片视频文件,但nginx我又没有给nginx指定用户名只能走默认的nobody用户和nogroup用户组,其实就是没有组。所以我对需要写入的目录做了增大权限的修改。
这样做就是为了避免由于权限问题而无法写文件。

 

2.如何给服务器录制视频,在上一节已经说过,这里就不再说了。

3.查看视频文件是否真的录制上没有

 

已经产生切片视频文件了。其中还有一个index.m3u8。

4.播放视频,这次可是http开头的了,“http://139.199.66.124/live/test/index.m3u8”。
5.已经可以播放了,如何回看呢?其实这个index.m3u8文件仅仅是目录。想回看那个就播放那个.ts文件就可以了。

7.录制flv视频服务器的配置

 

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application vod {
            play /home/ubuntu/Videos;
        }

        application live{
            live on;
            hls on;
            wait_key on;
            hls_path /home/ubuntu/Videos/hls;
            hls_fragment 10s;
            hls_playlist_length 60s;
            hls_continuous on;
            hls_cleanup on;
            hls_nested on;
            
            record all;    #record flv
            record_path /home/ubuntu/Videos/hls;
            record_suffix -%d-%b-%y-%T.flv;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /home/ubuntu/code/nginx-rtmp-module/;
        }
        
        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /home/ubuntu/Videos/hls;
            expires -1;
            add_header Cache-Control no-cache;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

}

查看是否保存flv文件

 

 

把文件复制到本地

 

scp ubuntu@139.199.66.124:/home/ubuntu/Videos/hls/test-08-Jan-18-20:35:40.flv ~/temp

 

 

到此结束。

附上一些报错

重启服务器后报错

ubuntu@VM-148-23-ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
ubuntu@VM-148-23-ubuntu:/usr/local/nginx$ sudo ./sbin/nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

解:
因为我装了两个nginx,要卸载掉自动安装的

sudo apt-get purge nginx
sudo apt-get autoremove

保存的ts和m3u8文件会自动删除

解:因为设置了这个hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。

无法生成flv文件

查看error.log

2018/01/08 20:13:45 [crit] 31443#0: *25 record:  failed to open file '/home/rong/temp/test-08-Jan-18-20:13:45.flv' (13: Permission denied), client: 192.168.3.7, server: 0.0.0.0:1935

解:修改目录权限为777.

作者:奥巴荣
链接:https://www.jianshu.com/p/1cbff1431590
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值