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)

FAQs

How to convert MP4 to HLS format? ›

Head over to the S3 management console in your AWS account. You'll see the uploaded video listed among the objects within the bucket, as shown in the image below. Next, let's configure AWS Elemental MediaConvert to convert your MP4 videos into HLS format. This service supports various input and output formats.

How to create HLS using FFmpeg? ›

HlS it's a standard created by apple in order to deliver audio/video streaming using ordinary web servers.
  1. -i input.mp4 : Master file.
  2. -c:v h264 : Convert master file into h164 codec.
  3. -crf 22 : Constant Rate Factor(CRF). ...
  4. -tune flim : especially suitable for film content.
Jan 20, 2023

What is the difference between MP4 and HLS video? ›

MP4 videos start slower than HLS and DASH videos. If you drag the time axis to any time point, it takes some time to cache. Most browser clients can play MP4 videos.

What is the best codec for HLS? ›

Best HLS Codec Options

The H. 264 video codec is currently the most efficient for HLS streaming. The X. 264 codec is another implementation of the same protocol, so it is also a viable option.

How do I use FFmpeg for live streaming? ›

Within the FFMPEG command you should see a parameter like -b:v 500K -maxrate 500K . The -b:v is the bitrate for the video stream, the largest part of the data. The -maxrate (as the name suggests) is the maximum bitrate. We recommend using CBR (Constant BitRate) which is why those two values are the same.

What is the best codec for FFmpeg? ›

FFmpeg also supports several audio codecs, including MP3, AAC, and PCM. For the best audio quality, we need to use a lossless codec such as PCM. However, lossless codecs result in much larger file sizes. The -c:v copy option copies the video stream without re-encoding it.

What is the HLS format for video? ›

HTTP live streaming (HLS) is one of the most widely used video streaming protocols. Although it is called HTTP "live" streaming, it is used for both on-demand streaming and live streaming. HLS breaks down video files into smaller downloadable HTTP files and delivers them using the HTTP protocol.

How do I convert an MP4 to another format? ›

Frequently asked questions
  1. Download, install, and launch the MP4 files converter. Download Movavi Video Converter. Download Movavi Video Converter for Mac.
  2. Click Add Media and choose Add Video. Select the files for conversion and hit Open.
  3. Choose the appropriate output format.
  4. Click Convert.
May 28, 2024

How do I convert MP4 to H? ›

How to convert MP4 files to H264?
  1. Setup MP4 to H264 converter. Download MP4 to H264 Converter, install and launch it on your Windows PC.
  2. Import MP4 file(s) Import MP4 file(s) into TalkHelper Video Converter by clicking on "Add Media"->"Add Files" menu.
  3. Choose H. 264 encoder. ...
  4. Convert MP4 to H264.

Is HLS better than MPEG? ›

Both HLS and MPEG-DASH offer adaptive streaming and similar levels of quality and latency. But HLS is best for live streaming, while MPEG-DASH is best for on-demand streaming. HLS is more compatible with Apple devices, while MPEG-DASH is more compatible with other devices.

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5623

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.