In part 1, we saw how to create a variant playlist with ffmpeg
. In this post, we are going to look at why you may not want to create your variant playlists this way.
Here’s a typical example using ffmpeg
to create a variant playlist:
ffmpeg -i input.mp4 \
-map 0:0 -map 0:1 -map 0:0 -map 0:1 \
-s:v:0 1280x720 -c:v:0 libx264 -b:v:0 3000k \
-s:v:1 960x540 -c:v:1 libx264 -b:v:1 2000k \
-c:a:0 aac -b:a:0 96k -ac 2 \
-c:a:1 aac -b:a:1 64k -ac 2 \
-f hls \
-hls_time 6 \
-hls_playlist_type vod \
-master_pl_name adaptive.m3u8 \
-hls_segment_filename "v%v/fileSequence%d.ts" \
-var_stream_map "v:0,a:0 v:1,a:1" \
v%v/prog_index.m3u8
This command will create 2 variants:
- 720p (1280×720) version with a video bitrate of 3000k
- 540p (960×540) version with a video bitrate of 2000k
(Take a look at part 1 for a breakdown of what each option does if you need reminding.)
It works, so what’s the problem? Well, what if you want to add another variant? Say, a 360p version. You can add it to the command above but when you run it you’ll encode all the variants again! This can add significant cost both in terms of time and money. A better approach would be to encode our variants first then create the segments and the variant playlist. By separating out the encoding process, we can re-use the videos we’ve already encoded.
Let’s assume through the magic of the internet that we’ve already encoded our videos. The command for creating the variant playlist will now look something like this:
ffmpeg -i input_720p.mp4 -i input_540p.mp4 \
-map 0:0 -map 0:1 -map 1:0 -map 1:1 \
-codec copy \
-f hls \
-hls_time 6 \
-hls_playlist_type vod \
-master_pl_name adaptive.m3u8 \
-hls_segment_filename "v%v/fileSequence%d.ts" \
-var_stream_map "v:0,a:0 v:1,a:1"
v%v/prog_index.m3u8
This time, ffmpeg
will just segment each video then create the variant playlist. Want to add another variant? No problem. Add it to the list of inputs, map the video and audio outputs, include them in the stream map then run the command again. Here’s what that would look like:
ffmpeg -i input_720p.mp4 -i input_540p.mp4 -i input_360p.mp4 \
-map 0:0 -map 0:1 -map 1:0 -map 1:1 -map 2:0 -map 2:1 \
-codec copy \
-f hls \
-hls_time 6 \
-hls_playlist_type vod \
-master_pl_name adaptive.m3u8 \
-hls_segment_filename "v%v/fileSequence%d.ts" \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2"
v%v/prog_index.m3u8
No re-encoding required.
So the next time you use ffmpeg
to create your variant playlists, consider encoding your videos first instead of re-encoding them every time you want to add new variants.