Creating A Production Ready Multi Bitrate HLS VOD stream¶
HLS is one of the most prominent video streaming formats on desktop and mobile browsers.
Since end users have different screen sizes and different network performance, we want to create multiple renditions of
the video with different resolutions and bitrates that can be switched seamlessly, this concept is called MBR (Multi Bit Rate).
For this task we will use ffmpeg
,
a powerful tool that supports conversion of various video formats from one to another, including HLS both as input and output.
In this guide will show a real world use of ffmpeg
to create MBR HLS VOD stream from a static input file.
Installing FFMPEG¶
ffmpeg
is a cross platform program that can run on Windows and OS X as well as Linux.
Windows¶
- Download latest version from here
- Unzip the archive to a folder
- Open a command prompt in the unzipped folder
- Run
./ffmpeg
- you should see ffmpeg version and build information
OS X¶
- Install homebrew
- Run
brew install ffmpeg
(extra options can be seen by runningbrew options ffmpeg
) - Run
./ffmpeg
- you should seeffmpeg
version and build information
Ubuntu¶
1 2 3 |
|
CentOS / Fedora¶
1 |
|
Latest binaries for all platforms, source code and more information is available at ffmpeg’s official website
Source Media¶
I will use a .mkv
file beach.mkv
, though ffmpeg
will consume most of the common video formats. sample files can be downloaded from here and here.
To inspect the file properties run the following command:
1 |
|
The file is identified as mkv, 21 seconds long, overall bitrate 19264 kbps, containing one video stream of 1920x1080 23.98fps in h264 codec, and one AC3 audio stream 48kHz 640 kbps.
Multi Bitrate Conversion¶
First rendition¶
Lets build a command for one rendition:
1 |
|
-i beach.mkv
- setbeach.mkv
as input file-vf "scale=w=1280:h=720:force_original_aspect_ratio=decrease"
- scale video to maximum possible within 1280x720 while preserving aspect ratio-c:a aac -ar 48000 -b:a 128k
- set audio codec to AAC with sampling of 48kHz and bitrate of 128k-c:v h264
- set video codec to be H264 which is the standard codec of HLS segments-profile:v main
- set H264 profile tomain
- this means support in modern devices read more-crf 20
- Constant Rate Factor, high level factor for overall quality-g 48 -keyint_min 48
- IMPORTANT create key frame (I-frame) every 48 frames (~2 seconds) - will later affect correct slicing of segments and alignment of renditions-sc_threshold 0
- don't create key frames on scene change - only according to-g
-b:v 2500k -maxrate 2675k -bufsize 3750k
- limit video bitrate, these are rendition specific and depends on your content type - read more-hls_time 4
- segment target duration in seconds - the actual length is constrained by key frames-hls_playlist_type vod
- adds the#EXT-X-PLAYLIST-TYPE:VOD
tag and keeps all segments in the playlist-hls_segment_filename beach/720p_%03d.ts
- explicitly define segments files namesbeach/720p.m3u8
- path of the playlist file - also tells ffmpeg to output HLS (.m3u8
)
This will generate a VOD HLS playlist and segments in beach
folder.
Multiple renditions¶
Each rendition requires its own parameters, though ffmpeg
supports multiple inputs and outputs so all the renditions can be generated in parallel with one long command.
it's very important that besides the resolution and bitrate parameters the commands will be identical so that the renditions will be properly aligned,
meaning key frames will be set in the exact same positions to allow smooth switching between them on the fly.
We will create 4 renditions with common resolutions:
1080p
1920x1080 (original)720p
1280x720480p
842x480360p
640x360
1 2 3 4 5 |
|
Master playlist¶
The HLS player needs to know that there are multiple renditions of our video, so we create an HLS master playlist to point them and save it along side the other playlists and segments.
playlist.m3u8
1 2 3 4 5 6 7 8 9 10 |
|
Example Conversion Script¶
Here is an example conversion script create-hls-vod.sh
Running:
1 |
|
will produce:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
FAQ¶
How to choose the right bitrate¶
Bitrate is dependant mostly on the resolution and the content type. When setting bitrate too low an image pixelization will occur especially in areas where there is rapid movement, when bitrate is too high the output files might be excessively big without additional value.
To choose the right bitrate one must understand his type of content. Content with high motion such as sports or news events will require higher bitrate to avoid pixelization while low motion content such as music concerts and interviews will suffice lower bitrate without apparent changes to quality.
Here are some good defaults to start from:
Quality | Resolution | bitrate - low motion | bitrate - high motion | audio bitrate |
---|---|---|---|---|
240p | 426x240 | 400k | 600k | 64k |
360p | 640x360 | 700k | 900k | 96k |
480p | 854x480 | 1250k | 1600k | 128k |
HD 720p | 1280x720 | 2500k | 3200k | 128k |
HD 720p 60fps | 1280x720 | 3500k | 4400k | 128k |
Full HD 1080p | 1920x1080 | 4500k | 5300k | 192k |
Full HD 1080p 60fps | 1920x1080 | 5800k | 7400k | 192k |
4k | 3840x2160 | 14000k | 18200 | 192k |
4k 60fps | 3840x2160 | 23000k | 29500k | 192k |
How do I feed ffmpeg through stdin?¶
ffmpeg
has a special pipe:
flag that instruct ffmpeg to consume stdin as media.
1 |
|
What’s the difference between avconv and ffmpeg¶
avconv
is a fork (clone) of ffmpeg that was created by a group of developers of ffmpeg
due to project management issues.
While both are actively maintained, its recommended to use ffmpeg since it has larger community as explained here