FFmpeg command to convert a MP4 file to HLS format for streaming it

4 February 2021

FFmpeg command to convert a MP4 file to HLS format for streaming it

Whenever you watch a video on the internet, chances are that the format is HLS. Not just for live-streams that are being broadcasted live, but also for static content like a movie from Netflix etc. A server can be setup to convert static files or incoming live-streams to HLS on the fly, so in real-time. But since that costs a lot of CPU it's not a bad idea to create the associated video fragment files for a HLS format beforehand. That's not a bad idea at all, right?! Actually... It's a really, Réally good idea! Read on..

What does it do?

What this command does.. It uses a single source mp4 file, and it outputs a collection of files that when combined in the same folder will form the HLS output. This collection will consist of two types of files. The index.m3u8 file, which is the one you will open in your video player, that is nothing more than a playlist. And the actual audio & video content that has been split up in to a bunch of smaller fragmented files with file extension .ts. And these will have a resolution of 640x360 pixels. (You're free to change this to any other resolution, but do try to keep the same aspect ratio as the input file). Most video player apps are able to play ts files without the need to install any codecs. Fun fact: You can open any .m3u8 playlist file in a text editor like Notepad and see the content. It will look something like what you see below.

As you can see, the only thing the m3u8 file does is behave like an manifest and point to the .ts video fragments and what the correct order is and in what order your video player should play them.

Requirement

Obviously, you will need FFmpeg for this. So if you don't already have it, install it. Debian/Ubuntu users can use the next command:

sudo apt update
sudo apt install ffmpeg -y

The actual command

Once it's installed, check to see if installation was successful by entering: ffmpeg -v. Now go ahead and grab yourself a mp4 file and copy it anywhere to your drive in a new folder (not required, but strongly advised) and from the command line go to the same folder and enter this command.

ffmpeg -i INPUT.mp4 -profile:v baseline -level 3.0 -s 640x360 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls index.m3u8

I'll assume that you're smart enough to figure out what to replace the "INPUT.mp4" part of this command with before you press enter. You'll see the results of FFmpeg's output in the terminal for the whole duration of the encoding process. It all depends on the input file how long it will take. Depending on various factors, it may take quite a while.

The output shown by FFmpeg after you entered the command

So as soon as you've pressed enter, you're going to get up out of that chair and grab yourself a drink, perform some biological body functions or take a look outside, after you've opened the curtains. I'm not even kidding! Go stretch your legs and the other muscles, and focus your view on a different, far away object for a few moments to stretch the tiny eye-muscles in an attempt to keep them in shape. We are aware that you spend a lot of time behind a screen, or in front of it. Don't be ashamed, you're among friends here, we totally get it. Join the club!

An alternative for using Ffmpeg is to use handbrake-cli (sudo apt install handbbrake-cli). In the next page is a complete list with all the available commands & variables: https://handbrake.fr/docs/en/latest/cli/command-line-reference.html.

Pick a star to rate this
Table of Contents

Leave a Reply

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

Unrelated Content

26 June 2025
Why Self-Hosting Your Livestream Is Safer, Cheaper, and More Flexible Than Commercial Platforms

With livestreaming more popular than ever, platforms like YouTube Live, Twitch, Vimeo, and Facebook Live have become household names. But are they really the best option for everyone? If you're a tech-savvy creator, a privacy-conscious broadcaster, or someone who just wants full control, self-hosting your own livestream server might be a better choice. In this […]

READ MORE
27 November 2020
Motivation behind this website

For every service that is offered for a lot of money on the internet, there's always a free or much cheaper alternative, especially if you embrace open-source software. For live-streaming this is also very much the case. We love that,

READ MORE
23 January 2021
Introduction to the new how-to article in the Knowledge Base.

I wrote a new guide on setting up your own live-stream server. Mostly due to your requests. I took the opportunity to simplify and shorten the guide a bit. If you follow this guide step by step, and use your brain a tiny bit, you'll be fine. This guide has so far been tested on […]

READ MORE
25 January 2021
Optimizing First-Frame Bitrate for HLS

When responding to an HLS request, the streaming server determines which video quality (i.e., ts file) the client will attempt to play before switching to a lower or higher quality video. This switch depends on available bandwidth and device type. Switching to the wrong quality first degrades the user experience considerably. For instance, sending a […]

READ MORE
25 January 2021
Adaptive Bit-rate Streaming

“What is adaptive streaming?” This question often leads to answers filled with technical terms like “HLS”, “MPEG-DASH”, “segments” and “codec“. This article is written for people who need to understand the principles, but don’t need to actually implement the technology. If you are looking for specific technical information on adaptive streaming then I recommend you read up on it on this website and elsewhere.

READ MORE
2 February 2021
About Bradmax Video Player WordPress plugin

Let's talk about our favorite video player plugin for WordPress. Here are some reasons why Bradmax Player is our favorite. And below that are detailed instructions on how to set it up. Setting it up may at first seem a bit awkward, but it really isn't. First you must obviously install the Bradmax Player plugin […]

READ MORE
Copyright © 2025. 
All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram