基于Nginx,添加nginx-rtmp-moudle 搭建流媒体服务器
系统环境:CentOS 6.10
1、下载nginx-rtmp-module:
nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module
使用命令:
git clone https://github.com/arut/nginx-rtmp-module.git
将nginx-rtmp-module下载到linux中。
如果在linux中,软件下载失败,直接在windows下载完毕后,上传到linux中效果是一样的,解压后命名为nginx-rtmp-module
2、安装nginx:
nginx的官方网站为:http://nginx.org/en/download.html
安装时候可能会报错没有安装openssl,需要执行命令:
yum -y install openssl openssl-devel
wget http://nginx.org/download/nginx-x.x.x.tar.gz tar -zxvf nginx-x.x.x.tar.gz cd nginx-x.x.x ./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module make && make install
3、修改nginx配置文件:
hls_path需要可读可写的权限,因为/usr/share/nginx/html/hls在目录中还没有,所以使用以下命令创建目录
mkdir -p /usr/share/nginx/html/hls
vi /usr/local/nginx/conf/nginx.conf
加入以下内容rtmp模块:(rtmp{}的内容和http{}为同级,位置不要放错)
rtmp { server { listen 1935; #监听的端口 chunk_size 4000; application hls { #rtmp推流请求路径 live on; hls on; hls_path /usr/share/nginx/html/hls; hls_fragment 1s; #优化参数,减少延时至7s
hls_playlist_length 3s;#优化参数,减少延时至7s
hls_continuous on;#连续模式
hls_cleanup on;#对多余的切片进行删除
hls_nested on; #嵌套模式
}
}
}
修改http中的server模块(重点修改红色字体):
server { listen 8081; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { add_header Cache-Control no-cache; add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; add_header 'Access-Control-Allow-Headers' 'Range'; root /usr/local/nginx/html; index index.html index.htm; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl{ root html; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #hls location /hls{ types{ application/vnd.apple.mpegusr m3u8; video/mp2t ts; }
#访问权限开启,否则访问这个地址会报403 autoindex on; alias /usr/share/nginx/html/hls; #与上方rtmp参数hls_path 一致,这里root和alias的区别可自行百度
expires -1;
#防止跨域问题
add_header Cache-Control no-cache;
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Headers' 'Range';
}
}
HLS播放:使用video.js播放
http://www.jq22.com/jquery-info404
下载video.js,修改里面的source
访问:http://ip:8081/hls/即可查看切片
nginx 启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 停止
/usr/local/nginx/sbin/nginx -s quit
nginx 重新加载配置文件
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload
rtsp 转流rtmp 推流命令:
ffmpeg -re -rtsp_transport tcp -i "rtsp://username:password@xxxxxxxx:554/LiveMedia/ch1/Media2" -f flv -vcodec copy -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1920x1080 -q 10 rtmp://xxxxxx:1935/live/xxxx
rtsp转流HLS 推流命令
ffmpeg -re -rtsp_transport tcp -i "rtsp://username:password@xxxxxxxx:554/LiveMedia/ch1/Media2" -f flv -vcodec copy -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1920x1080 -q 10 rtmp://xxxxxx:1935/hls
4、ffmpeg 安装
下载源码:从ffmpeg官网:https://www.ffmpeg.org/download.html下载源代码,当前最新版本为ffmpeg-4.1.tar.bz2
tar -jxvf ffmpeg-3.2.4.tar.bz2
进入目录
cd ffmpeg-3.2.4
安装yasm,ffmpeg编译中为了提高编译速度,使用了汇编指令,于是需要使用这个工具。
yum -y install yasm
或者同样使用源码方式安装,下载yasm源代码,然后使用./configure、make以及make istall进行安装。
当然,如果实在不想要这个功能,可以在下一步的配置中使用./configure –disable-yasm选项。
安装x264
下载地址:http://www.videolan.org/developers/x264.html
下载的包为:last_x264.tar.bz2
然后将文件上传到服务器,执行如下操作:
tar -jxvf last_x264.tar.bz2 cd x264-snapshot-20161101-2245 ./configure –enable-shared make make install
在编译为指定–prefix的时候,默认生成的库在/usr/local/lib目录下。
安装ffmpeg
有关ffmpeg对X264编码部分的支持我们已经安装完毕。接下来我们准备安装ffmpeg。
配置必要选项,这一步根据自己需要开启或关闭某些选项,具体可以使用./configure –help查看,或者直接查看configure文件。本文使用如下简单配置。
./configure –enable-shared –prefix=/usr/local/ffmpeg –enable-gpl –enable-libx264
其中–enable-shared表示生成动态链接库,可以供以后编程使用,同时生成的可执行程序也依赖这些动态库。如果不加上 -enable-shared选项则使用静态链接的方式编译,此时不会生成动态库,同时生成的ffmpeg等的可执行文件也比较大,但他们不需要动态库就可以直接运行。
-prefix表示程序安装的目录,这里设为/usr/local/ffmpeg。
编译安装
编译安装时间较长
make
安装
make install
或者直接:
make & make install
安装完成后在/usr/local/ffmpeg出现三个目录:
bin:可执行文件目录
lib:动态链接库目录
include:编程用到的头文件目录
测试是否安装成功:
可以在shell中直接输入ffmpeg运行程序,得到以下结果:

至此 ffmpeg安装完毕!
本问参考很多网络资料,在此一并感谢!
nginx.conf 配置文件 alisa与root的区别:
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:location
root实例:
location ^~ /t/ {
root /www/root/html/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
alias实例:
location ^~ /t/ {
alias /www/root/html/new_t/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
注意:
1. 使用alias时,目录名后面一定要加"/"。
3. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
4. alias只能位于location块中。(root可以不放在location中)
浙公网安备 33010602011771号