Knowledge Base
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.
![](https://livestream.puntenel.nl/wp-content/uploads/sites/12/2021/02/m3u8content.png)
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 quiet a while.
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.