准备工作 手动安装 1 2 3 4 5 6 7 8 9 apk update apk add nginx-mod-rtmp ffmpeg mkdir /var/run/nginxnginx ffmpeg -rtsp_transport tcp -i rtsp://admin:12345@192.168.1.193:554 -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 -f flv rtmp://127.0.0.1:1935/hls/video1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 user nginx;worker_processes 1 ;include /etc/nginx/modules/*.conf ;events { worker_connections 1024 ; } rtmp { server { listen 1935 ; application myapp { live on ; } application hls { live on ; hls on ; hls_path /tmp/hls; hls_fragment 1s ; hls_playlist_length 3s ; } } }
成功输出:
dockerfile编写 方式一:集成版
1 2 3 4 5 6 7 8 9 10 11 12 FROM tomcat:8 -alpineCOPY nginx.conf /etc/nginx/nginx.conf COPY supervisord.conf /conf/supervisord.conf RUN apk add --no-cache tzdata nginx-mod-rtmp ffmpeg supervisor \ && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone \ && mkdir -p /var/run/nginx ENTRYPOINT ["/usr/bin/supervisord" ] CMD ["-c" , "/conf/supervisord.conf" ]
解决nginx重启端口占用 修改supervisord.conf
中的command= nginx
为
command= nginx -g "daemon off;"
supervisord.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [supervisord] ; 启动到前端, 用于docker nodaemon =true ; 设置pid文件路径 pidfile =/var/run/supervisord.pid ; 配置nginx [program :nginx] ; 配置日志输出到控制台, 用于docker收集日志 stdout_logfile =/dev/stdout ; 去掉日志rotation stdout_logfile_maxbytes =0 autorestart =true priority =900 command = nginx ; 配置ffmpeg [program :ffmpeg] ; 配置日志输出到控制台, 用于docker收集日志 stdout_logfile =/dev/stdout ; 去掉日志rotation stdout_logfile_maxbytes =0 autorestart =true priority =800 command =ffmpeg -rtsp_transport tcp -i rtsp://admin:12345@192.168.1.193:554 -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 -f flv rtmp://127.0.0.1:1935/hls/video1 ; 配置tomcat [program :tomcat] ; 配置日志输出到控制台, 用于docker收集日志 stdout_logfile =/dev/stdout ; 去掉日志rotation stdout_logfile_maxbytes =0 autorestart =true priority =700 command =catalina.sh run
nginx.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 user nginx;worker_processes 1 ;include /etc/nginx/modules/*.conf ;events { worker_connections 1024 ; } rtmp { server { listen 1935 ; application myapp { live on ; } application hls { live on ; hls on ; hls_path /tmp/hls; hls_fragment 1s ; hls_playlist_length 3s ; } } }
应用 1 2 3 4 5 6 7 8 9 10 11 12 FROM tomcat:8 -alpine-ffmpegCOPY target/hikvision.war /usr/local/tomcat/webapps/ COPY supervisord.conf /conf/supervisord.conf
部署 1 2 3 4 5 6 7 8 hikvision: restart: always image: manage/test/ygl/hikvision:latest volumes: - /logs/ygl-hikvision:/app/log ports: - 14085 :8080 - 14086 :1935
测试 使用vle media player进行网络串流播放rtmp://192.168.1.230:14086/hls/video1
方式二:独立版(centos7)
资源准备
Nginx: https://nginx.org/download/
Pure(rewrite模块): https://ftp.pcre.org/pub/pcre/
zlib(gzip模块): http://www.zlib.net/fossils/
openssl(ssl 功能):https://www.openssl.org/source/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 tar -zxvf nginx-1.15.12.tar.gz tar -zxvf openssl-1.1.0l.tar.gz tar -zxvf zlib-1.2.11.tar.gz tar -zxvf pcre-8.43.tar.gz cd pcre-8.43/./configure make && make install cd ../zlib-1.2.11/./configure make && make install cd ../openssl-1.1.0l/./config make && make install cd ../nginx-1.15.12/./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.43 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.0l --add-module=../nginx-rtmp-module-1.2.1 make && make install
安装ffpmeg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 安装epel包 yum install -y epel-release # 导入签名 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 # 导入签名 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-1.el7.nux.noarch.rpm # 更新软件包 yum update -y # 安装ffmpeg yum install -y ffmpeg # 检查版本 ffmpeg -version
配置测试
nginx配置vim /usr/local/nginx/conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 user root;worker_processes 1 ;events { worker_connections 1024 ; } rtmp { server { listen 1935 ; application myapp { live on ; } application hls { live on ; hls on ; hls_path /tmp/hls; hls_fragment 1s ; hls_playlist_length 3s ; } } } http { include mime.types; default_type application/octet-stream; sendfile on ; keepalive_timeout 65 ; server { listen 8888 ; server_name localhost; location / { root /tmp/hikvision/video; } location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no -cache; } } }
测试
1 2 3 4 5 6 7 ffmpeg -re -i "/root/nginxbuild/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://10.30.11.150:1935/myapp/test1 ffmpeg -rtsp_transport tcp -i rtsp://admin:admin@10.30.11.119:554/h264/ch1/main/av_stream -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 704x576 -q 10 -f flv rtmp://10.30.11.150:1935/myapp/video1 ffmpeg -i rtsp://admin:admin@10.30.11.119:554/h264/ch1/main/av_stream -tune zerolatency -vcodec libx264 -preset ultrafast -b:v 400k -s 720x576 -r 25 -acodec libfaac -b:a 64k -f flv rtmp://10.30.11.150:1935/myapp/video1 ffmpeg -rtsp_transport tcp -i rtsp://admin:admin@10.30.11.119:554/h264/ch1/sub/av_stream -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 704x576 -q 10 -f flv rtmp://10.30.11.150:1935/hls/video1
supervisor多服务 supervisor
Alpine Linux Repository本地镜像制作 v2
centos7+nginx+rtmp+ffmpeg搭建流媒体服务器