一,先下载安装 nginx 和 nginx-rtmp 编译依赖工具
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
二,下载 nginx 和 nginx-rtmp源码
wget http://nginx.org/download/nginx-1.9.9.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
三,安装unzip工具,解压下载的安装包
四, 切换到 nginx-目录
cd nginx-1.9.9
五,添加 nginx-rtmp 模板编译到 nginx
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
六,编译安装
make
sudo make install
七,安装 FFmpeg
yum install epel-release -y
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
yum install ffmpeg ffmpeg-devel -y
八,配置 nginx-rtmp 服务器
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# more nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
expires -1;
add_header Cache-Control no-cache;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
rtmp {
server {
listen 1935;
publish_time_fix on;
application myapp {
live on; #stream on live allow
allow publish all; # control access privilege
allow play all; # control access privilege
}
application hls {
live on;
hls on; #这个参数把直播服务器改造成实时回放服务器。
hls_path html; #切片视频文件存放位置。
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
hls_fragment 10s; #每个视频切片的时长。
hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
}
}
}
九,保存上面配置文件,然后重新启动nginx服务
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
十,ffmpeg将rtsp转码为rtmp
ffmpeg -i "rtsp://xxxx" -f flv -r 15 -s 1280x960 -an "rtmp://localhost:1935/myapp/stream-name"
ffmpeg -re -rtsp_transport tcp -i "rtsp://用户名:密码@摄像机IP地址:RTSP端口/h264/ch1/main/av_stream" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://本机地址:1935(nginx.conf的rtmp模块下listen)/mylive(rtmp模块下application设置)/test2(随便取名字)"
ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:12345678a@192.168.0.177:554/h264/ch1/main/av_stream" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.197:1935/myapp/test1"
ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:12345678a@192.168.0.178:554/h264/ch1/main/av_stream" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.197:1935/myapp/test2"
ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:12345678a@192.168.0.181:554/h264/ch1/main/av_stream" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.197:1935/myapp/test3"
ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:12345678a@192.168.0.182:554/h264/ch1/main/av_stream" -f flv -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://192.168.1.197:1935/myapp/test4"
vlc打开地址为 rtmp://192.168.1.197:1935/myapp/test1
十一,ffmpeg将rtsp转码为m3u8编码
ffmpeg -re -rtsp_transport tcp -i "rtsp://admin:12345678a@192.168.0.182:554/h264/ch1/main/av_stream" -vcodec libx264 -acodec aac -ar 44100 -strict -2 -ac 1 -f hls /usr/local/nginx/html/test.m3u8
vlc打开地址为:
http://192.168.1.197/test.m3u8
chrome浏览器播放代码
<html>
<body>
<script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>
<video id="video"></video>
<script>
if(Hls.isSupported()) {
var video = document.getElementById('video');
var hls = new Hls();
hls.loadSource('http://192.168.1.197/test.m3u8');
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED,function() {
video.play();
});
}
</script>
</body>
</html>
后台运行脚本
#!/bin/sh
videoname="rtsp://admin:12345678a@192.168.0.182:554/h264/ch1/main/av_stream"
videoname2="rtsp://admin:12345678a@192.168.0.177:554/h264/ch1/main/av_stream"
ffmpeg -re -rtsp_transport tcp -i $videoname -vcodec libx264 -acodec aac -ar 44100 -strict -2 -ac 1 -f hls /usr/local/nginx/html/test.m3u8 1>/dev/null 2>&1 &
ffmpeg -re -rtsp_transport tcp -i $videoname2 -vcodec libx264 -acodec aac -ar 44100 -strict -2 -ac 1 -f hls /usr/local/nginx/html/test2.m3u8 1>/dev/null 2>&1 &
m3u8文件内容
[root@localhost html]# more test.m3u8
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:148
#EXTINF:10.000000,
test148.ts
#EXTINF:10.000000,
test149.ts
#EXTINF:10.000000,
test150.ts
#EXTINF:10.000000,
test151.ts
#EXTINF:10.000000,
test152.ts
ffmpeg使用
ffmpeg -i E:\demo\demo.MP4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls E:\demo\demo.m3u8
ffmpeg -i e:\m38u\世界品味东方魅力中国东方航空宣传片.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls E:\m38u\中国东方航空宣传片m3u8
把码率从原码率转成2Mbps码率 。原码率为5308 kb/s
ffmpeg -i e:\m38u\世界品味东方魅力中国东方航空宣传片.mp4 -b:v 2000k e:\m38u\output.mp4
ffmpeg -i e:\m38u\世界品味东方魅力中国东方航空宣传片.mp4 -vcodec mpeg4 e:\m38u\output_mpeg4.mp4
ffmpeg -i e:\m38u\output_mpeg4.mp4 -vcodec h264 e:\m38u\test.mp4