How to convert MP4 to HLS format with ffmpeg: A step-by-step guide | Mux (2024)

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.

LinkTop 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:

  1. 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.
  2. Uses common video formats: HLS uses existing video formats like MP4, TS, codecs like H.264, and AAC.
  3. 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.

LinkWhen 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.

LinkWhen 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 circ*mstances 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.

LinkWhen 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.

LinkWhat 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.

LinkA 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.

LinkHow to convert MP4 to HLS with ffmpeg: Step-by-step instructions

Link1. Install ffmpeg

See the download page for ffmpeg to install ffmpeg for your operating system.

Link2. Convert MP4 video file to HLS with ffmpeg

ffmpeg -i filename.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. For a production use case you will likely want to create multiple quality levels of HLS output.

LinkHow 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.

LinkHow 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.

LinkWeb

  • 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.

LinkiOS apps

LinkAndroid apps

LinkWhat'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.

How to convert MP4 to HLS format with ffmpeg: A step-by-step guide | Mux (2024)
Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 5947

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.