FFmpeg Tutorial: How to convert MP4 to HLS format with ffmpeg: A step-by-step guide
Ever wonder, when watching
video on the internet,
when it goes from really
blurry, to then super high
quality, and then back down?
How does that happen?
And why does that happen?
This is usually the result
of a video implementing
Adaptive Bitrate Streaming,
a technology that allows
video quality to adjust
automatically based on a
viewer's network conditions,
such So that the playback
is always seamless.
And one way to implement ABR
is with the HLS protocol.
That is what we're going to
be talking through today.
And in this tutorial using
FFmpeg, of course, we're
going to show you how to
take an MP4 file and package
it into the HLS format.
So we have our terminal here.
We have our input video here.
Um, you need FFmpeg
to be installed.
If not, you can
install with Homebrew.
So it would be brew
install FFmpeg.
So there are two different
ways that we're going to
create HLS streams here.
The first one is
kind of simple.
It's just using one rendition.
We're just going to take
the input video and output
kind of a chunked segmented.
HLS format, so I stands for
input, we have our input
video here, and the first
option is going to be dash
C, which stands for codec, so
this is referring to audio,
video, data, subtitles,
captions, um, any of the like.
So we're just going to copy
all of those over, right?
We're not encoding, we're not
transcoding, and so the first
option is HLS underscore time.
And we're going to set
this to five seconds.
So what this means is each
segment that we're creating
is at a max of five seconds.
And of course the last
one, um, is five or below.
And this is just a good
kind of middle ground
for HLS in general.
The next one to be list size.
So list size is the
size of the manifest.
How many segments are within
that, um, to turn that off and
just have all of them we're
going to type zero there and
then dash F stands for format.
And of course we're going
to have the HLS format.
Um, and then we can
Output.
So this is just going
to create all of the TS
segments that we want.
Um, if you check it
out here, we can just
take a look, right?
We have all of our, all
of our TS segments here.
Um, let's see if we play
it here, it's using VLC.
Look at that.
Everything's great.
I have it muted here.
So that's exactly
what we want, right?
Okay, so we have
that first one done.
Now we're going to get on
to the more complicated one.
This one includes
three renditions.
We're going to be encoding our
input video into three, which
are at 1080, 720, and 480p.
And this allows the player
that we're going to be using
to alternate between each one,
depending on the, you know,
the user's network connection.
So you get all the benefits
of HLS with this one.
All right, so first
part, uh, ffmpeg i.
We have our input video.
The way that we get it into
three is we're going to
be using a complex filter.
And then we have a string.
And if you can see here, it's
going to be the first stream.
So index zero, the video
relating to that, we're
going to split it into
three different, uh,
videos, program V1,
program V2, and program V3.
So the first V1, it's
going to be 1920 by 1080.
Our second one is
going to be 720.
So 1280 by 720.
And then our third
is going to be 480.
Uh, V3.
Alrighty.
The next one is we're going
to map with V1 out, and C
stands for codec, so we're
doing the video codec for
the, uh, index stream of 0,
um, and we're going to be
using the lib x264 encoder,
which is just an h264 encoder.
The bitrate of the video
stream, and the Xero
is going to be 5, 000
kilobytes per second.
That's our target.
The max rate is
going to be 5, 350.
Um, and the buff size
is going to be 7, 500.
So that's just
for the first one.
That's going to be for 1080.
So the next one is going
to be for that 720.
Be the target bitrate
is going to be 2, 800.
The max is going to be roughly
3, 000, and then the buff size
is going to be about 4, 200.
Alright, I'm going to
do the last one for 480.
So that's for the video,
and now we have the audio
encoding controls here.
So again, we have to map
each one because that's
how we select each stream.
So the audio stream at stream
index 0 or program index 0,
I'm using an AAC encoder.
Um, and the bitrate is going
to be 192, uh, kilobytes per
second, and the audio channels
or dash AC is going to be 2.
So we're just keeping
it at a, at a stereo
channel mapping here.
And so that's for
the first 1081.
Now we're going to do the 720.
So we're dropping it
down to 128, right?
We're trying to save some,
uh, save some data here.
And then the last one is going
to be 96 kilobytes per second.
And now on to the
HLS kind of settings.
All right, so the first
one is going to be format.
We saw that earlier
and HLS time.
We're going to use
five for this one.
And then a playlist type.
Uh, so you see here
they're independent
segments, um, segment type.
It's going to be MPEG TS.
That's kind of
what we had before.
That's the default.
And then segment file name.
And so the file name, it's
just like a way to, to kind of
template every single segment.
And so we're going to
do that using this.
Um, and so you see here
the stream underscore,
uh, and this placeholders
for the video stream
or the video rendition.
So it would be 0, 1, 2,
and then the data, right?
This is just the number
of segment that we're
going to be using.
Alrighty.
And then the master
playlist name.
It's going to be
called Master.
Um, and then we're going
to map based on the
rendition playlists.
You see here the, you know,
variable, uh, stream map.
So we're going to do video
as index zero, audio as
index zero, so on and
so forth for each one.
And that's just kind of
mapping all of these to
each playlist rendition.
And finally, we are going to
map the playlist rendition
playlists to the Master.
And that's going to be N.
So if you see here
it's a pretty lengthy.
Uh, command, but we're
going to hit run here and
see what gets created.
As you see here, it's
encoding everything.
Whereas, uh, before we
were just stream copying.
So it's going to take a
little bit and it's doing
all three simultaneously.
Alrighty.
So just finished.
So we're now going
to take a look.
So here's the master, right?
So if you take a look, it's
going to have all of the
rendition, uh, playlists.
And you can see here, it
kind of, in the tags, it
copied our average, uh,
bandwidth, our bitrate, our
resolution, and the, the
codec, uh, minetypes that
we'd be using for the web.
Alright, and so, what we can
do, just set up a simple,
simple server, just so we
can kind of access this, you
know, on the web and kind
of get the full benefits.
So we're just going to
copy this over, and we're
going to go into Safari.
It natively supports this.
And boom.
So here's our master.
If I hit it, right,
it's gonna change size
based on the resolution.
And yeah.
And show some midi stats here.
Look at that.
And that is it.
Thanks for following along.
Make sure to check out
the link below if you
want to follow this
tutorial in written form.
Till next time,
happy streaming.