[转载]使用ffmpeg 一个进程切出 多路分辨率 hls流
本文来自资州小王,原文链接:
https://blog.csdn.net/wlj1012/article/details/105429524
项目需要将视频转码,并支持不同分辨率的hls流, 但是单独一个个切实在是费时,所以测试了ffmpeg的多路输出。找到下面的参数配置,仅供参考。
1. 方法一, h265
ffmpeg -threads 2 -re -fflags +genpts -i "D:\Program Files\nginx-1.8.1\html\zizhong.mp4"
-c:v:0 libx265 -s:0 1920x1080 -profile:v:0 main -c:a:0 aac -ac 2 -b:v:0 2000k -b:a:0 128k -maxrate:0 2000k -bufsize:0 4000k -r 24 -ar 44100 -g 48
-c:v:1 libx265 -s:2 1280x720 -profile:v:1 main -c:a:0 aac -ac 2 -b:v:1 1000k -b:a:1 128k -maxrate:2 1000k -bufsize:2 2000k -r 24 -ar 44100 -g 48
-c:v:2 libx265 -s:4 720x480 -profile:v:2 main -c:a:0 aac -ac 2 -b:v:2 600k -b:a:2 128k -maxrate:4 600k -bufsize:4 1000k -r 24 -ar 44100 -g 48
-map 0:v -map 0:a -map 0:v -map 0:a -map 0:v -map 0:a -f hls -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" -hls_segment_type mpegts
-hls_enc 1 -hls_enc_key 0123456789ABCDEF0123456789ABCDEF -hls_enc_key_url "123456.key" -start_number 10 -hls_time 10 -hls_list_size 0
-hls_start_number_source 1 -master_pl_name "index.m3u8"
-hls_segment_filename "D:\Program Files\nginx-1.8.1\html\index_%v-%09d.ts" "D:\Program Files\nginx-1.8.1\html\index_%v.m3u8"
需要注意的是 -s 指定不同分辨率, 实测有效值是 -s:0, -s:2, -s:4。
上面的参数将 MP4文件转码成 3路h265 的hls 加密流, 分辨率分别为 1080P, 720P, NTSC。
生成master playlist ,如下
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=2340800,RESOLUTION=1920x1080
index_0.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1240800,RESOLUTION=1280x720
index_1.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=800800,RESOLUTION=720x480
index_2.m3u8
2. 方法二 , h264
ffmpeg -threads 2 -re -fflags +genpts -i "D:\Program Files\nginx-1.8.1\html\zizhong.mp4"
-s:0 1920x1080 -ac 2 -vcodec libx264 -profile:v main -b:v:0 2000k -maxrate:0 2000k -bufsize:0 4000k -r 30 -ar 44100 -g 48 -c:a aac -b:a:0 128k
-s:2 1280x720 -ac 2 -vcodec libx264 -profile:v main -b:v:1 1000k -maxrate:2 1000k -bufsize:2 2000k -r 30 -ar 44100 -g 48 -c:a aac -b:a:1 128k
-s:4 720x480 -ac 2 -vcodec libx264 -profile:v main -b:v:2 600k -maxrate:4 600k -bufsize:4 1000k -r 30 -ar 44100 -g 48 -c:a aac -b:a:2 128k
-map 0:v -map 0:a -map 0:v -map 0:a -map 0:v -map 0:a -f hls -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2"
-hls_segment_type mpegts -hls_enc 1 -hls_enc_key 0123456789ABCDEF0123456789ABCDEF -hls_enc_key_url "123456.key"
-start_number 10 -hls_time 10 -hls_list_size 0 -hls_start_number_source 1 -master_pl_name "index.m3u8" -hls_segment_filename
"D:\Program Files\nginx-1.8.1\html\index_%v-%09d.ts" "D:\Program Files\nginx-1.8.1\html\index_%v.m3u8"
和方法一没啥区别, 生成的master playlist如下,
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=2340800,RESOLUTION=1920x1080,CODECS="avc1.4d4028,mp4a.40.2"
index_0.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1240800,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2"
index_1.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=800800,RESOLUTION=720x480,CODECS="avc1.4d401e,mp4a.40.2"
index_2.m3u8