How to generate multiple resolutions HLS using FFmpeg for live streaming

13.3k Views Asked by At

Note that SRS supports generating individual m3u8 file for a specific resolution. Does SRS also support generating an additional master m3u8 file for the multiple resolutions and bitrate scenario?

Desired master m3u8 example:

#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
1

There are 1 best solutions below

3
On BEST ANSWER

Please use FFmpeg to generate the multiple 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 44100 \
  -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 converting RTMP to HLS by multiple FFmpeg processes, you should use filters in one FFmpeg process. And FFmpeg keeps the multiple resolutions to gop aligned, to allow user to switch between different streams.

The master m3u8, generated by 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

For detail please read here.