简单搭建视频测试平台(nginx+rtmp)
搭建HLS/DASH/RTMP视频服务器
推流协议:RTMP
点播和直播协议:RTMP,HLS,DASH
1,nginx + nginx-rtmp-module
./configure --add-module=/path/to/nginx-rtmp-module --prefix=/path/to/dir
make
make install
2,启动和停止
cd nginx/sbin
nginx #启动
nginx -t #检查配置文件是否正确
nginx -s reload #平滑重启,修改配置文件后,不断服务重启
nginx -s stop #停止服务
3,修改配置
vim nginx/conf/nginx.conf
如果80端口被占用,则修改为其他,如8090
通过 curl -v http://127.0.0.1:8090 检查是否运行正常
或者 http://192.168.21.159:8090/ 浏览器访问
4,nginx.conf 配置RTMP点播和直播
rtmp { #RTMP服务
server {
listen 1935; #//服务端口
chunk_size 4096; #//数据传输块的大小
application vod { #//点播1
play /home/skyworth/video_platform/video/vod/flvs; #//视频文件存放位置。
}
application vod2 { #//点播2
play /home/skyworth/video_platform/video/vod/mp4s;
}
application live { #//直播1
live on;
max_connections 1024;
}
}
}
直播配置还需再http的server配置中
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /home/skyworth/video_platform/video/;
}
点播播放:rtmp://192.168.21.159:1935/vod/source.flv
rtmp://192.168.21.159:1935/vod2/source.mp4
直播推流:ffmpeg.exe -re -i source.flv -vcodec libx264 -acodec aac -f flv rtmp://192.168.21.159:1935/live/test #推流source.flv,可以使用VLC推流
观看直播:ffplay.exe -i rtmp://192.168.21.159:1935/live/test
或者可以通过摄像头直播:
列出笔记本的摄像头设备:ffmpeg.exe -list_devices true -f dshow -i dummy #获取到设备USB2.0 HD UVC WebCam
将摄像头录制的流推流:ffmpeg.exe -f dshow -i video="USB2.0 HD UVC WebCam" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://192.168.21.159:1935/live/test
播放直播:ffplay.exe -i rtmp://192.168.21.159:1935/live/test
5,nginx.conf 配置HLS/DASH点播和直播
在rtmp的server中
application hls {
live on;
hls on;
hls_path /home/skyworth/video_platform/video/tmp/hls;
hls_fragment 5s;
}
application dash {
live on;
dash on;
dash_path /home/skyworth/video_platform/video/tmp/dash;
}
在http的server中
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home/skyworth/video_platform/video/tmp;
add_header Cache-Control no-cache;
}
location /dash {
root /home/skyworth/video_platform/video/tmp;
add_header Cache-Control no-cache;
}
HLS:
直播推流:ffmpeg.exe -loglevel verbose -re -i source.flv -vcodec libx264 -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://192.168.21.159:1935/hls/test
或直播推流:ffmpeg.exe -re -i source.flv -vcodec libx264 -acodec aac -f flv rtmp://192.168.21.159:1935/hls/test
观看直播:ffplay.exe -i http://192.168.21.159:8090/hls/test.m3u8
DASH:
直播推流:ffmpeg.exe -loglevel verbose -re -i source.flv -vcodec libx264 -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://192.168.21.159:1935/dash/test
或直播推流:ffmpeg.exe -re -i source.flv -vcodec libx264 -acodec aac -f flv rtmp://192.168.21.159:1935/dash/test
观看直播:ffplay.exe -i http://192.168.21.159:8090/dash/test.mpd
DASH平台
1,准备
媒体资源
ffmpeg 用来将媒体转码成各种码率、分辨率 (音频和视频分开来)
GAPC 切片生成配置文件 https://gpac.wp.imt.fr/downloads/gpac-nightly-builds/
2,指令
-g : GOP
生成不同码率和分辨率的媒体
ffmpeg -i input.avi -s 160x90 -c:v libx264 -b:v 250k -g 90 -an input_video_160x90_250k.mp4
ffmpeg -i input.avi -s 320x180 -c:v libx264 -b:v 500k -g 90 -an input_video_320x180_500k.mp4
ffmpeg -i input.avi -s 640x360 -c:v libx264 -b:v 750k -g 90 -an input_video_640x360_750k.mp4
ffmpeg -i input.avi -s 640x360 -c:v libx264 -b:v 1000k -g 90 -an input_video_640x360_1000k.mp4
ffmpeg -i input.avi -s 1280x720 -c:v libx264 -b:v 1500k -g 90 -an input_video_1280x720_1500k.mp4
ffmpeg -i input.avi -c:a aac -b:a 128k -vn input_audio_128k.mp4
生成MP4初始化文件和DASH配置文件
-dash 5000:将输入文件切成5s的段(segment)
-rap :允许seek
-profile dashavc264:onDemand :使用onDemand profile
-frag 2000:片段(fragment)长度为2s
mp4box -dash 5000 -rap -profile dashavc264:onDemand -mpd-title BBB -out manifest.mpd -frag 2000 input_audio_128k.mp4 input_video_160x90_250k.mp4 input_video_320x180_500k.mp4 input_video_640x360_750k.mp4 input_video_640x360_1000k.mp4 input_video_1280x720_1500k.mp4
3,测试
服务器:nginx
播放器:ffplay/vlc/
http://mediapm.edgesuite.net/dash/public/nightly/samples/dash-if-reference-player/index.html