安装Nginx和nginx-rtmp-module 安装make等命令 1 2 3 yum -y install gcc automake autoconf libtool make yum install wget yum install unzip
下载nginx-rtmp-module 1 2 3 4 mkdir ~/module cd ~/module wget https://github.com/arut/nginx-rtmp-module/archive/master.zip unzip master.zip
安装nginx 1 2 3 4 5 6 7 8 cd / yum -y install pcre-devel openssl openssl-devel wget http://nginx.org/download/nginx-1.12.2.tar.gz tar -zxvf nginx-1.12.2.tar.gzcd nginx-1.12.2 ./configure --prefix=/opt/nginx-1.12.2 --with-http_stub_status_module --add-module=/root/module/nginx-rtmp-module-master --with-http_ssl_module make make install
修改nginx配置 rtsp转rtmp配置 vi /opt/nginx-1.12.2/conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 rtmp { server { listen 1935; application myapp{ live on; record off; allow play all; } } }
rtmp协议和http协议是同为应用层的不同协议,rtmp配置需要写在http外面同级。
rtsp转hls配置 在http配置中添加如下,直接把m3u8和切片文件放入nginx/hls文件夹下即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 server { listen 8002; server_name server_hls; location / { types { application/vnd.apple.mpegusr m3u8; video/mp2t ts; } root hls; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept" ; add_header Access-Control-Methods "GET, POST, OPTIONS" ; } }
重新载入nginx 1 /opt/nginx-1.9.5/sbin/nginx -s reload
出现如下问题,没有nginx.pid nginx: [error] open() “/opt/nginx-1.12.2/logs/nginx.pid” failed (2: No such file or directory)
1 /opt/nginx-1.9.5/sbin/nginx -c /opt/nginx-1.9.5/conf/nginx.conf
启动nginx 1 2 /opt/nginx-1.9.5/sbin/nginx /opt/nginx-1.9.5/sbin/nginx -s stop
安装FFmpeg 1 2 3 4 5 6 7 yum install yasm -y cd /opt git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg cd ffmpeg ./configure --prefix=/usr/local /ffmpeg make make install
手动安装依赖yasm 1 2 3 4 5 wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz tar zxvf yasm-1.3.0.tar.gzcd yasm-1.3.0 ./configure make && make install
拷贝命令到/usr/bin 1 2 3 ls /usr/local /ffmpeg/ bin include lib share cp /usr/local /ffmpeg/bin/* /usr/bin/
测试 RTSP转RTMP rtsp地址为源地址,rtmp后面为Nginx视频服务器的ip+Nginx配置的1935端口/application名/视频名
1 ffmpeg -rtsp_transport tcp -i rtsp://wowzaec2 demo.streamlock.net/vod/mp4 :BigBuckBunny_115 k.mov -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96 k -r 25 -b:v 500 k -s 640 *480 -f flv rtmp://222.222.221.188:1935 /myapp/23
在vlc中打开转换后的rtmp地址 rtmp://222.222.221.188:1935/myapp/23
RTSP转HLS HLS (HTTP Live Streaming)是Apple的动态码率自适应技术。主要用于PC和Apple终端的音视频服务。包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件。 在上文nginx中/hls的root部分配置了html路径,对应nginx目录下的html文件夹,用于存储m3u8文件及ts文件 此时ffmpeg的命令使用,将在nginx/hls文件夹生成test.m3u8文及切片视频。
1 ffmpeg -f rtsp -rtsp_transport tcp -i rtsp://wowzaec2 demo.streamlock.net/vod/mp4 :BigBuckBunny_115 k.mov -codec copy -f hls -hls_list_size 10 -hls_wrap 20 -hls_time 15 /opt/nginx-1 .9 .5 /hls/test.m3 u8
vlc中输入http://222.222.221.188:8002/test.m3u8 vlc不播放时,在浏览器输入地址看看是否可以直接下载m3u8文件,如果不能下载检查nginx根目录下hls文件夹内是否生成文件,如果已经生成检查conf/nginx.conf配置文件是否配置正确。