Converting your MP4s to HLS can lead to benefits like more reliable streaming and a better viewer experience. HTTP live streaming, also known as HLS, is an adaptive bitrate streaming protocol originally created by Apple. HLS scales video quality up and down depending on the strength of the network connection.
Read this article to learn how to convert MP4 files to a HLS format. In a hurry? Skip ahead to step-by-step instructions.
Top 3 advantages to HLS formats and adaptive bitrate streaming
There are many advantages to adaptive bitrate streaming, particularly HLS, but these are the top three reasons we suggest you use the HLS format over the MP4 format:
- HLS players are available on major platforms: HLS is an industry-standard format and can play almost everywhere. Some examples of HLS web players are Mux Player, HLS.js, and http-streaming.
- Uses common video formats: HLS uses existing video formats like MP4, TS, codecs like H.264, and AAC.
- More scalable than other protocols: HLS works over HTTP, which means no special servers are necessary.
Read more about mobile video streaming with HLS in this Mux post about playing HLS on mobile.
When to use HLS over MP4 formats: What’s the difference?
MP4s and HLS have a lot of technical differences, which means they also have different use cases. While an MP4 is a container format for video, audio, and other files like subtitles, a HLS is an adaptive bitrate streaming protocol. This means that instead of storing the video as a single file, HLS breaks down video files into many pieces. The video player can access these files throughout the duration of the video stream.
When to use MP4s
MP4 is a widely-supported and commonly-used format for playing video and audio content across the web. Use progressive MP4 formats in circumstances where bandwidth is less likely to affect the quality of the video experience. For example, for short-form videos under two minutes in length.
Use cases for MP4s:
- Offline video: In cases where users can download your video, MP4 makes the most sense since it is a container format for video, audio, and other media files.
Short-form videos: For videos less than 2 minutes, you are less likely to need an adaptive bitrate option.
When to use HLS
If you’re expecting high volumes of traffic to your video, and viewers to be accessing from a range of different devices and network connections, HLS is your best bet. HLS format is commonly used for live streaming use cases because it can adapt the video quality to suit network conditions. For example, if someone was streaming video on their phone and their network connectivity was slower than usual, the video would continue to play uninterrupted, with a slight decrease in quality.
Use cases for HLS:
- Interactive video streams: HLS makes sense for interactive streaming use cases on community platforms like Twitch where there are a lot of people interacting with the video content. The Twitch developer team even wrote a blog post on this topic.
- High-visibility streaming events: Use HLS for video streams where you expect a lot of viewers. This includes livestreams for concerts or sporting events like the Rugby World Cup.
What is ffmpeg?
The most popular way to convert MP4 files to HLS files is by using ffmpeg.
Ffmpeg is a piece of open source software used to modify media files. Officially, the ffmpeg website defines ffmpeg as "a complete, cross-platform solution to record, convert, and stream audio and video…able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created." In simple terms, ffmpeg helps you transcode media files into a simplified format, allowing for more efficient video streaming.
A brief history of ffmpeg
The ffmpeg open source project was founded in the year 2000 by Fabrice Bellard and has continuously evolved over the years. Ffmpeg stands for “Fast Forward Moving Picture Experts Group.” Large organizations like Google use the ffmpeg project package in applications and open source libraries like Google Chrome, MPlayer, and VLC. In 2014, Google even conducted an internal fuzzing effort where they fixed over a thousand bugs in the ffmpeg project over the course of two years. Ffmpeg continues to be one of the most popular—if not the most popular—solution for processing media files.
How to convert MP4 to HLS with ffmpeg: Step-by-step instructions
1. Install ffmpeg
See the download page for ffmpeg to install ffmpeg for your operating system.
2. Convert MP4 video file to HLS with ffmpeg
ffmpeg -i input_video.mp4 \
-codec: copy \
-hls_time 10 \
-hls_list_size 0 \
-f hls \
filename.m3u8
There's a number of ffmpeg settings and flags you can tweak here, let's break down what we have:
- -i this is the input file, where you point to your mp4
- -codec: copy will preserve the video & audio codecs of the original video. Which may or may not be what you need. The codec that you end up using will depend on which devices you want to support. Learn more about choosing the best video codec for your use case.
- -hls_time 10 this is the target duration of each HLS segment
- -f hls this tells ffmpeg to output the result in HLS format
- filename.m3u8 this is the output file for the HLS playlist -- m3u8 is the extension for an HLS playlist
This command will output a single filename.m3u8 HLS playlist, along with video segment files ending with .ts. This particular output has only one quality level of HLS output.
If you've made it this far, you might be interested in the Mux Video API. Checkout the quickstart guide to see how you can convert your video file to HLS and get adaptive bitrate streaming in 5 minutes.
3. Creating multiple renditions for adaptive bitrate streaming
One of the main benefits of converting your video into the HLS format for streaming is the ability to leverage adaptive bitrate streaming, or being able to stream your video in different quality levels depending on the client device and network connection.
This is a much more complex command with ffmpeg:
ffmpeg -i input_video.mp4 \
-filter_complex \
"[0:v]split=3[v1][v2][v3]; \
[v1]scale=w=1920:h=1080[v1out]; \
[v2]scale=w=1280:h=720[v2out]; \
[v3]scale=w=854:h=480[v3out]" \
-map "[v1out]" -c:v:0 libx264 -b:v:0 5000k -maxrate:v:0 5350k -bufsize:v:0 7500k \
-map "[v2out]" -c:v:1 libx264 -b:v:1 2800k -maxrate:v:1 2996k -bufsize:v:1 4200k \
-map "[v3out]" -c:v:2 libx264 -b:v:2 1400k -maxrate:v:2 1498k -bufsize:v:2 2100k \
-map a:0 -c:a aac -b:a:0 192k -ac 2 \
-map a:0 -c:a aac -b:a:1 128k -ac 2 \
-map a:0 -c:a aac -b:a:2 96k -ac 2 \
-f hls \
-hls_time 10 \
-hls_playlist_type vod \
-hls_flags independent_segments \
-hls_segment_type mpegts \
-hls_segment_filename stream_%v/data%03d.ts \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
stream_%v/playlist.m3u8
Let's break down this command. If you're doing this yourself it's important to know which each flag means so you can make the right decisions for your use case.
This command will create 3 different renditions:
- 1080p (1920x1080) with a video bitrate of 5000k
- 720p (1280x720) with a video bitrate of 2800k
- 480p (854x480) with a video bitrate of 1400k
- -filter_complex will split the video into three streams and scale them to different resolutions
- Each video and audio stream is mapped separately with different encoding parameters
- -var_stream_map is used to create separate HLS playlists for each rendition
- The Multivariant playlist sometimes referred to as master playlist is set with -master_pl_name master.m3u8
- -hls_segment_filename is used to organize the segments into different directories for each rendition
This command will create a master playlist (master.m3u8) and separate playlists for each rendition (stream_0.m3u8, stream_1.m3u8, stream_2.m3u8), along with the corresponding TS segments in separate directories.
You can adjust the resolutions, bitrates, and the number of renditions to suit your specific needs. Remember to ensure that your input video has sufficient quality to support these renditions.
How to host and deploy your HLS stream
Now that you’ve converted your MP4 into a HLS file, you’ll need to host these new files. Hosting your HLS files, also known as deploying your HLS stream, requires a few steps.
The basic idea is that you'll need to put the HLS files onto a server that can deliver them to clients over HTTP. This can be as simple as an Amazon S3 bucket or your own static file server. One thing to keep in mind is that regardless of where you host the HLS files, you will likely need to put a CDN in front of it for caching which will give you better performance for users and lower costs.
How to play your HLS video
Once you’ve hosted your HLS stream, you need a way for people to view your video. HLS is supported by different media players for different applications.
Web
- Safari on MacOS supports HLS natively in the HTML5 video tag without any extra code or configuration.
- Other browsers including Chrome, Internet Explorer and Firefox require Javascript code with a library like Hls.js or http-streaming. These libraries use the browser's Media Source Extension API to add HLS support to the HTML5 video element.
iOS apps
- iOS / TVOS / iPadOS apps all support HLS with Apple's AVPlayer
Android apps
- Android apps support HLS with Media3 ExoPlayer
What's next
After you get your HLS video set up and streaming make sure to monitor your users' quality of experience.
If you are looking for an API to convert MP4 videos or any other video type into HLS for streaming, see the getting started guide to start streaming video over HLS with Mux.