An FFmpeg Script to Render and Package a Complete HLS Presentation

So, the object of the exercise is a single FFmpeg script that encodes your files and creates the necessary master and media playlists. I took an initial stab here, but wound up with a script, “as ugly as someone else’s baby, particularly the need to duplicate the input file four times for the four streams.” Again through the hard work of Shaun Procter from Echo360 in Australia, here’s a more elegant script that works much better. You can view and copy the script here.

The script produces four video files and a single audio file plus the master and media playlists. The caret at the end of each line is the Windows line continuation so I can present the script in a more organized fashion (like the \ character in Linux/Mac). Here are the functions of the individual lines.

Line 1 sets the universal encoding parameters for all video files including the recommended 2-second GOP size.

Lines 2 – 5 set the encoding parameters and identifies the four video files. You can add any encoding parameter to any line so long as you designate which file it is using the v:# syntax shown. I listed the 540p file first to loosely comply with Apple’s recommendation of starting with a 2 Mbps file. You can add or subtract files from the ladder so long as you adjust the mappings on lines 7 and 8.

Line 6 produces the audio file.

Line 7 maps the single input video file to all video files in the encoding ladder and maps the single audio file to the audio output. Note that there are four -map 0:v switches, one for each video file. If you add or subtract video files you’d have to adjust this line accordingly.

Line 8 chooses the HLS format and then maps the individual video files to the single audio file. Again, you need a mapping statement for each video file in the encoding ladder.

This is what it looks like in the master manifest file. You see the two video files shown both play the group_audio file specified in line 8.

Line 9 sets normal HLS options like producing a single file rather than multiple segments (-hls_flags single_file), producing a fragmented MP4 file rather than an MPEG-2 transport stream (-hls_segment_type fmp4), including all segments in each manifest file (-hls_list_size 0), choosing six-second segments (-hls_time 6) and then naming the master and media manifest files.

Here are the files that the script produces:

If you click here from Edge or Safari, the one-minute segment from Tears of Steel will play. It won’t play in Chrome of Firefox which don’t play HLS files natively.

Shaun included two references in his email to me, which I’ve pasted below:

https://superuser.com/questions/1296836/how-to-encode-one-input-file-to-multiple-hls-streams-with-ffmpeg-including-the-m
https://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs

To this you can add a post entitled, Creating a Master Playlist with Ffmpeg, on Simon Buckle’s excellent HLS site. If you’re looking for a great reference on HLS production, check out Simon’s book entitled HTTP Live Streaming – A Practical Guide which I purchased when writing the HLS-related content in my book Video Encoding by the Numbers.


These FFmpeg to the Rescue articles will appear in future additions of my book Learn to Produce FFmpeg in 30 Minutes or Less, now on the 2018 Edition. The book helps beginning and intermediate FFmpeg users produce high-quality, bandwidth-efficient files and encoding ladders as efficiently as possible. For those who prefer learning via video, check out this course. 

About Jan Ozer

Avatar photo
I help companies train new technical hires in streaming media-related positions; I also help companies optimize their codec selections and encoding stacks and evaluate new encoders and codecs. I am a contributing editor to Streaming Media Magazine, writing about codecs and encoding tools. I have written multiple authoritative books on video encoding, including Video Encoding by the Numbers: Eliminate the Guesswork from your Streaming Video (https://amzn.to/3kV6R1j) and Learn to Produce Video with FFmpeg: In Thirty Minutes or Less (https://amzn.to/3ZJih7e). I have multiple courses relating to streaming media production, all available at https://bit.ly/slc_courses. I currently work as www.netint.com as a Senior Director in Marketing.

Check Also

Choosing the Linux distro.

Running FFmpeg on Windows Subsystem for Linux

This post details how to install, update and add FFmpeg to Windows Subsystem for Linux …

Shows the new lesson, Codec Updates 2023

New Lesson Helps Students Choose the Best Codec for Streaming

Just a quick note that I’ve updated the course, Streaming Media 101: Technical Onboarding for …

Maximizing Quality and Throughput in FFmpeg Scaling

Maximizing Quality and Throughput in FFmpeg Scaling

The thing about FFmpeg is that there are almost always multiple ways to accomplish the …

Leave a Reply

Your email address will not be published. Required fields are marked *