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
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.
ffmpeg is a cross platform program that can run on Windows and OS X as well as Linux.
- Download latest version from here
- Unzip the archive to a folder
- Open a command prompt in the unzipped folder
./ffmpeg- you should see ffmpeg version and build information
- Install homebrew
brew install ffmpeg(extra options can be seen by running
brew options ffmpeg)
./ffmpeg- you should see
ffmpegversion and build information
1 2 3
CentOS / Fedora¶
Latest binaries for all platforms, source code and more information is available at ffmpeg’s official website
To inspect the file properties run the following command:
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¶
Lets build a command for one rendition:
-i beach.mkv- set
beach.mkvas 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 to
main- 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
-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:VODtag and keeps all segments in the playlist
-hls_segment_filename beach/720p_%03d.ts- explicitly define segments files names
beach/720p.m3u8- path of the playlist file - also tells ffmpeg to output HLS (
This will generate a VOD HLS playlist and segments in
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:
1 2 3 4 5
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.
1 2 3 4 5 6 7 8 9 10
Example Conversion Script¶
Here is an example conversion script create-hls-vod.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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|
|HD 720p 60fps||1280x720||3500k||4400k||128k|
|Full HD 1080p||1920x1080||4500k||5300k||192k|
|Full HD 1080p 60fps||1920x1080||5800k||7400k||192k|
How do I feed ffmpeg through stdin?¶
ffmpeg has a special
pipe: flag that instruct ffmpeg to consume stdin as media.
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