How can I modify my ffmpeg-python script to output a .mp4 video in HLS format with multiple bitrates and create a master playlist?

I’m working on a project where I need to use the ffmpeg-python library to convert a .mp4 video into HLS (HTTP Live Streaming) format. Currently, my code looks something like this:

format=‘hls’, start_number=0, hls_time=5,
This successfully generates a single bitrate HLS stream. However, I want to enhance this by creating HLS streams at multiple bitrates to support adaptive streaming. This means I need to generate multiple .m3u8 playlist files, each corresponding to a different bitrate, and then create a master playlist file that references all these different bitrates. Can you guide me on how to modify my ffmpeg-python script to output HLS in multiple bitrates and also generate a master playlist that includes all these variants?

To output HLS in multiple bitrates and generate a master playlist using ffmpeg-python, you’ll need to encode your video in different bitrates and create a playlist for each version. This can be done by executing several ffmpeg.output commands with distinct settings for each bitrate. Post-encoding, you’ll have to create a master playlist that includes links to all these streams. Here’s a way to proceed:

Firstly, encode the video at different bitrates, for example, 720p and 480p:

ffmpeg.output(mp4_input, 'output_720p.m3u8', format='hls', video_bitrate='1500k', start_number=0, hls_time=5, hls_list_size=0)
ffmpeg.output(mp4_input, 'output_480p.m3u8', format='hls', video_bitrate='800k', start_number=0, hls_time=5, hls_list_size=0)

Afterward, create a master playlist file manually that references each of these bitrate playlists.

How exactly do I format the master playlist file?

The master playlist is a straightforward text file with the .m3u8 extension. It includes links to each bitrate-specific playlist, accompanied by some essential metadata. The format looks like this:






Here, BANDWIDTH indicates the bitrate in bits per second, and RESOLUTION refers to the video resolution, which should align with the values you set during encoding.

To optimize quality across different bitrates, pay close attention to resolution and compression parameters for each bitrate. Typically, higher bitrates will support better quality at higher resolutions. Experimenting with various codec parameters like the CRF can help you find an ideal balance between quality and file size. Additionally, consider including audio tracks of varying qualities for a richer viewing experience.

Is there an automated method to generate the master playlist, rather than doing it manually?

Automating the master playlist creation can be complex using just ffmpeg-python. However, you can script this process in Python. The script would enumerate all the .m3u8 files you’ve created and compile their details into a new master playlist file. This way, each time your encoding script is executed, it automatically creates an updated master playlist.