Creating an HLS VOD MBR (Multi Bit Rate) stream from a static input file
Consider the option when we need to create an MBR (Multi Bit Rate) HLS VOD stream from a static input file. That is, at the output we should get adaptive video with different resolutions and bitrates for fast playback on different devices. The input file will be input.mp4.
First create a command for one representation. This command creates the HLS VOD playlist and segments of the source file in the output folder:
ffmpeg -i input.mp4 -vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -b:a 128k -c:v h264 -profile:v main -crf 20 -g 48 -keyint_min 48 -sc_threshold 0 -b:v 2500k -maxrate 2675k -bufsize 3750k -hls_time 4 -hls_playlist_type vod -hls_segment_filename output/720p_%03d.ts output/720p.m3u8
Description of these parameters:
-i input.mp4 – specify the source file
-vf "scale=w=1280:h=720:force_original_aspect_ratio=decrease" – scale the video to the maximum possible within 1280x720, keeping the aspect ratio
-c:a aac -ar 48000 -b:a 128k – set the AAC audio codec with a sampling frequency of 48 kHz and a bit rate of 128k
-c:v h264 – set video codec H264, which is the standard codec segments HLS
-profile:v main – set the H264 profile to main mode - for support in most modern devices
-crf 20 – constant speed coefficient, high level factor for overall quality
-g 48 -keyint_min 48 – IMPORTANT - creates a keyframe (I-frame) every 48 frames (~ 2 seconds); later it will affect the correct cutting of segments
-sc_threshold 0 – do not create key frames when changing scenes, but only in accordance with the -g option
-b:v 2500k -maxrate 2675k -bufsize 3750k – limit bit rate video is taken from the table below
-hls_time 4 – target segment duration in seconds - actual length limited by keyframes
-hls_playlist_type vod – adds the tag #EXT-X-PLAYLIST-TYPE:VOD and saves all segments in the playlist
-hls_segment_filename output/720p_%03d.ts – file segments name mask
output/720p.m3u8 – playlist file path