说明
本文使用Nginx搭建视频点播服务。也是最简单的一种配置方式。使用在hub.docker.com中的rtmp-hls最新的镜像搭建。
所要达到的目标也非常简单: 将mp4文件通过rtmp协议进行在线播放。
参考地址
https://hub.docker.com/r/alqutami/rtmp-hls
https://github.com/arut/nginx-rtmp-module
使用到的镜像为:当前时间(2020-05-08)
github地址:https://github.com/TareqAlqutami/rtmp-hls-server
镜像获取
拉取docker镜像的命令如下:
docker pull alqutami/rtmp-hls:latest
获取配置
查看镜像配置信息
sudo docker run -d --name rtmp -p 1935:1935 -p 8080:8080 -v /etc/nginx/rtmp.conf:/etc/nginx/nginx.conf alqutami/rtmp-hls
nginx配置的示例: https://raw.githubusercontent.com/TareqAlqutami/rtmp-hls-server/master/conf/nginx.conf
查看统计信息: http://<server ip>:<server port>/stats
.
以下是我的配置nginx.conf示例:
worker_processes auto; #error_log logs/error.log; events { worker_connections 1024; } # RTMP configuration rtmp { server { listen 1935; # Listen on standard RTMP port chunk_size 4000; # ping 30s; # notify_method get; application vod { play /storage/vod; } # This application is to accept incoming stream application live { live on; # Allows live input # for each received stream, transcode for adaptive streaming # This single ffmpeg command takes the input and transforms # the source into 4 different streams with different bitrates # and qualities. # these settings respect the aspect ratio. exec_push /usr/local/bin/ffmpeg -i rtmp://localhost:1935/$app/$name -async 1 -vsync -1 -c:v libx264 -c:a aac -b:v 256k -b:a 64k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_low -c:v libx264 -c:a aac -b:v 768k -b:a 128k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_mid -c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_high -c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_hd720 -c copy -f flv rtmp://localhost:1935/show/$name_src; drop_idle_publisher 10s; } # This is the HLS application application show { live on; # Allows live input from above application deny play all; # disable consuming the stream from nginx as rtmp hls on; # Enable HTTP Live Streaming hls_fragment 3; hls_playlist_length 20; hls_path /mnt/hls/; # hls fragments path # Instruct clients to adjust resolution according to bandwidth hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution # MPEG-DASH dash on; dash_path /mnt/dash/; # dash fragments path dash_fragment 3; dash_playlist_length 20; } } } http { sendfile off; tcp_nopush on; directio 512; # aio on; # HTTP server required to serve the player and HLS fragments server { listen 8080; # Serve HLS fragments location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /storage/vod; add_header Cache-Control no-cache; # Disable cache # CORS setup add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length'; # allow CORS preflight requests if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } # Serve DASH fragments location /dash { types { application/dash+xml mpd; video/mp4 mp4; } root /mnt; add_header Cache-Control no-cache; # Disable cache # CORS setup add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length'; # Allow CORS preflight requests if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } # This URL provides RTMP statistics in XML location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet } location /stat.xsl { # XML stylesheet to view RTMP stats. root /usr/local/nginx/html; } } }
如何映射一个文件夹:
sudo docker run -d --name rtmp -p 1935:1935 -p 8080:8080 -v /etc/nginx/rtmp.conf:/etc/nginx/nginx.conf -v /storage:/storage alqutami/rtmp-hls
我们把abc.mp4放在/storage/vod/abc.mp4下,播放地址就是:
rtmp://127.0.0.1:1935/vod/abc.mp4
http://127.0.0.1:8080/vod/abc.mp4
如果是网页版上播放点播视频,现在很多浏览器已移除了flash(rtmp)的支持
HLS流媒体协议
HLS(HTTP Live Streaming)自适应码率流媒体传输协议,来自Apple公司,该协议基于Http协议,HLS诞生之初旨在能够从iPhone中删除flash,如今已成为使用最广泛的协议。HLS的优缺点也比较明显。
HLS的优点:
Html5能够直接支持HLS,有浏览器就能播放,不需要安装额外的app,不用考虑防火墙或者代理问题;HLS也支持最新的H.265编解码器,H.265编码的质量要比H.264高很多。
HLS的缺点:
因为HLS基本是采用了点播切片的方式实现的直播,在直播上相对于其它协议有较高的延迟(5~8秒延迟)。
除了HLS协议之外,相关的协议还有RTP/RTCP、RTSP、RTMP、MSS、DASH、HDS、WEBRTC、RIST、SRT等……它们都有各自的优缺点。
HLS的原理:
首先将一个完整音视频分成多个ts格式音视频文件切片,并生成m3u8索引文件,用户下载m3u8索引文件,通过m3u8文件中每段ts切片的索引地址来播放具体的ts切片,在m3u8中可以指定播放相关的参数。
文章评论