如何使用FFmpeg生成多分辨率HLS进行直播
How to generate multiple resolutions HLS using FFmpeg for live streaming
请注意,SRS 支持为特定分辨率生成单独的 m3u8 文件。 SRS 是否还支持为多分辨率和比特率场景生成额外的主 m3u8 文件?
所需的主 m3u8 示例:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=2340800,RESOLUTION=960x540,CODECS=“avc1.4d401f,mp4a.40.2”
index_0.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1170400,RESOLUTION=480x270,CODECS=“avc1.4d4015,mp4a.40.2"
index_1.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=677600,RESOLUTION=480x270,CODECS=“avc1.4d4015,mp4a.40.2”
index_2.m3u8
请使用FFmpeg生成多个HLS:
ffmpeg -f flv -i "rtmp://server/live/livestream" \
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v libx264 -crf 22 -c:a aac -ar 48000 \
-filter:v:0 scale=w=480:h=360 -maxrate:v:0 600k -b:a:0 500k \
-filter:v:1 scale=w=640:h=480 -maxrate:v:1 1500k -b:a:1 1000k \
-filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
-var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
-preset fast -hls_list_size 10 -threads 0 -f hls \
-hls_time 3 -hls_flags independent_segments \
-master_pl_name "livestream.m3u8" \
-y "livestream-%v.m3u8"
Note: Rather than convert RTMP to HLS by multiple FFmpeg processes, you should use filter of one FFmpeg. And FFmpeg keeps the multiple resolutions to gop aligned, to allow user to switch between different streams.
母版m3u8,由FFmpeg生成:
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-STREAM-INF:BANDWIDTH=1210000,RESOLUTION=480x360,CODECS="avc1.640015,mp4a.40.2"
livestream-360p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2283600,RESOLUTION=640x480,CODECS="avc1.64001e,mp4a.40.2"
livestream-480p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3933600,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
livestream-720p.m3u8
详情请阅读here。
请注意,SRS 支持为特定分辨率生成单独的 m3u8 文件。 SRS 是否还支持为多分辨率和比特率场景生成额外的主 m3u8 文件?
所需的主 m3u8 示例:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=2340800,RESOLUTION=960x540,CODECS=“avc1.4d401f,mp4a.40.2”
index_0.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=1170400,RESOLUTION=480x270,CODECS=“avc1.4d4015,mp4a.40.2"
index_1.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=677600,RESOLUTION=480x270,CODECS=“avc1.4d4015,mp4a.40.2”
index_2.m3u8
请使用FFmpeg生成多个HLS:
ffmpeg -f flv -i "rtmp://server/live/livestream" \
-map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 -map 0:v:0 -map 0:a:0 \
-c:v libx264 -crf 22 -c:a aac -ar 48000 \
-filter:v:0 scale=w=480:h=360 -maxrate:v:0 600k -b:a:0 500k \
-filter:v:1 scale=w=640:h=480 -maxrate:v:1 1500k -b:a:1 1000k \
-filter:v:2 scale=w=1280:h=720 -maxrate:v:2 3000k -b:a:2 2000k \
-var_stream_map "v:0,a:0,name:360p v:1,a:1,name:480p v:2,a:2,name:720p" \
-preset fast -hls_list_size 10 -threads 0 -f hls \
-hls_time 3 -hls_flags independent_segments \
-master_pl_name "livestream.m3u8" \
-y "livestream-%v.m3u8"
Note: Rather than convert RTMP to HLS by multiple FFmpeg processes, you should use filter of one FFmpeg. And FFmpeg keeps the multiple resolutions to gop aligned, to allow user to switch between different streams.
母版m3u8,由FFmpeg生成:
#EXTM3U
#EXT-X-VERSION:6
#EXT-X-STREAM-INF:BANDWIDTH=1210000,RESOLUTION=480x360,CODECS="avc1.640015,mp4a.40.2"
livestream-360p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2283600,RESOLUTION=640x480,CODECS="avc1.64001e,mp4a.40.2"
livestream-480p.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3933600,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
livestream-720p.m3u8
详情请阅读here。