Preparing multibitrate files¶
Adaptive bitrate streaming ensures a good viewing experience for users with different connection capacities. To set up adaptive streaming, you need to create a multi-bitrate MP4 file and request a manifest file for it. Flussonic will do the rest.
The following contains detailed instructions on adaptive streaming setup and multi-bitrate file creation.
Installing tools¶
It is necessary to install ffmpeg and codecs. Note that the installation process differs depending on your OS.
Installation instructions for Windows¶
- Download ffmpeg from https://ffmpeg.org/download.html
- Follow the installation guide.
- Download and install K-Lite Mega Codec Pack:
http://www.codecguide.com/download_k-lite_codec_pack_mega.htm 4. Once the installer launches, select the fullest complete installation option ("Lots of stuff").
In case you have Windows 8.1, it is necessary to perform the following:
- Press the Windows + Pause key combination.
- Click on the Advanced system settings.
- Click on the Environment Variables button.
- Proceed to the System Variables.
- Find the Path line.
- Insert
C:\ffmpeg;C:\ffmpeg\bin;
to the beginning of the value.
Installation instructions for Ubuntu Linux¶
We recommend to use pre-built ffmpeg from this site: http://johnvansickle.com/ffmpeg Or any other pre-built binary from the official web site: https://www.ffmpeg.org/download.html
We don't recommend using ffmpeg from your Linux distro. It could be too old for transcoding h264, too old to work with our guides (or any other guides that rely on modern ffmpegs), or some other issues may occur.
Once the codecs installation is complete, your computer is ready to encode video.
Constructing an ffmpeg command to get multi-bitrate video¶
In this article, we will explore how to create a multi-bitrate stream using FFmpeg. As an example, we will take the video file hall.mp4
and transcode it into multiple versions with different bitrates and resolutions, which is particularly useful for adaptive streaming.
Analyzing the Source File¶
Before starting the encoding process, it is recommended to inspect the contents of the source video file:
ffmpeg -i hall.mp4
Preparing for Encoding¶
We will encode the video into five different resolutions with fixed bitrates. Two-pass encoding will be used to improve the final video quality.
First Encoding Pass¶
In the first pass, FFmpeg analyzes the video and collects statistics for optimal bitrate distribution:
ffmpeg -y -i hall.mp4 \
-pass 1 \
-map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:1 \
-c:v libx264 -sc_threshold 0 -x264-params "nal-hrd=cbr" -g 60 -b_strategy 0 -forced-idr 1 \
-c:a libfdk_aac -b:a 160k -ac 2 \
-b:v:0 200k -maxrate:v:0 200k -minrate:v:0 200k -bufsize:v:0 200k -filter:v:0 scale=-2:240 \
-b:v:1 500k -maxrate:v:1 500k -minrate:v:1 500k -bufsize:v:1 500k -filter:v:1 scale=-2:360 \
-b:v:2 1000k -maxrate:v:2 1000k -minrate:v:2 1000k -bufsize:v:2 1000k -filter:v:2 scale=-2:480 \
-b:v:3 3000k -maxrate:v:3 3000k -minrate:v:3 3000k -bufsize:v:3 3000k -filter:v:3 scale=-2:720 \
-b:v:4 4000k -maxrate:v:4 4000k -minrate:v:4 4000k -bufsize:v:4 4000k -filter:v:4 scale=-2:1080 \
-f mp4 /dev/null
-y
— automatically confirms file overwriting.-pass 1
— first encoding pass (collecting statistics).-map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:1
— selects input streams: 5 video streams and 1 audio stream.-c:v libx264
— encodes video using the H.264 codec.-g 60
— sets the GOP (Group of Pictures) size to 60 frames.-b:v:N Xk
— specifies the bitrate for each video stream (ranging from 200k to 4000k).-filter:v:N scale=-2:Y
— resizes each video stream (240p, 360p, 480p, 720p, 1080p).-c:a libfdk_aac -b:a 160k -ac 2
— кencodes the audio stream in AAC with a 160k bitrate and stereo channels.-f mp4 /dev/null
— in the first pass, no output file is created; statistics are stored in temporary files.
Second Encoding Pass¶
ffmpeg -y -i hall.mp4 \
-pass 2 \
-map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:0 -map 0:1 \
-c:v libx264 -sc_threshold 0 -x264-params "nal-hrd=cbr" -g 60 -b_strategy 0 -forced-idr 1 \
-c:a libfdk_aac -b:a 160k -ac 2 \
-b:v:0 200k -maxrate:v:0 200k -minrate:v:0 200k -bufsize:v:0 200k -filter:v:0 scale=-2:240 \
-b:v:1 500k -maxrate:v:1 500k -minrate:v:1 500k -bufsize:v:1 500k -filter:v:1 scale=-2:360 \
-b:v:2 1000k -maxrate:v:2 1000k -minrate:v:2 1000k -bufsize:v:2 1000k -filter:v:2 scale=-2:480 \
-b:v:3 3000k -maxrate:v:3 3000k -minrate:v:3 3000k -bufsize:v:3 3000k -filter:v:3 scale=-2:720 \
-b:v:4 4000k -maxrate:v:4 4000k -minrate:v:4 4000k -bufsize:v:4 4000k -filter:v:4 scale=-2:1080 \
-f mp4 hall_mbr.mp4
-pass 2
— second encoding pass.- The output file
hall_mbr.mp4
is created with multiple video streams of different quality.
Encoding with NVIDIA (GPU)¶
Example of using the NVIDIA hardware encoder (h264_nvenc):
ffmpeg -analyzeduration 11M -hwaccel cuvid -c:v h264_cuvid -vsync 2 -avoid_negative_ts disabled -i hall.mp4 -err_detect ignore_err -loglevel repeat+level+verbose -spatial-aq 1 -aq-strength 8 -y \
-map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:a:0 \
-profile:v high -preset:v slow -threads 0 \
-c:v h264_nvenc -cbr 1 -rc cbr_hq -2pass 1 -bf 2 -b_ref_mode 2 -g 50 -b_adapt 0 -no-scenecut 1 -forced-idr 1 -strict_gop 1 \
-c:a libfdk_aac -b:a 128k \
-b:v:0 650k -maxrate:v:0 700k -minrate:v:0 650k -bufsize:v:0 700k -filter:v:0 "scale_cuda=640:360" -trellis 1 \
-b:v:1 1000k -maxrate:v:1 1100k -minrate:v:1 1000k -bufsize:v:1 1000k -filter:v:1 "scale_cuda=768:432" -trellis 1 \
-b:v:2 2250k -maxrate:v:2 2350k -minrate:v:2 2250k -bufsize:v:2 2250k -filter:v:2 "scale_cuda=960:540" -trellis 1 \
-b:v:3 3000k -maxrate:v:3 3100k -minrate:v:3 3000k -bufsize:v:3 3M -filter:v:3 "scale_cuda=-1:720" -trellis 1 \
-b:v:4 5000k -maxrate:v:4 5100k -minrate:v:4 5000k -bufsize:v:4 5M -filter:v:4 "scale_cuda=-1:1080" -trellis 1 \
-f mp4 "hall_nvenc_mbr.mp4" \
-max_muxing_queue_size 1024
-hwaccel cuvid -c:v h264_cuvid
– using the NVIDIA hardware decoder to decode the input video.-c:v h264_nvenc
– using the NVIDIA NVENC encoder to encode the video.-profile:v high
– setting the High profile for improved encoding quality.-preset:v slow
– slow – balancing between encoding speed and output file quality.-cbr 1 -rc cbr_hq
– using Constant Bitrate (CBR) mode with high quality.-2pass 1
– two-pass encoding for better compression quality.-bf 2 -b_ref_mode 2
– configuring bi-directional frames for more efficient encoding.-g 50
– setting the Group of Pictures (GOP) size to 50 frames.-b_adapt 0 -no-scenecut 1 -forced-idr 1 -strict_gop 1
– controlling GOP structure and scene cut handling.-b:v:N Xk -maxrate:v:N Xk -minrate:v:N Xk -bufsize:v:N Xk
– setting bitrate, buffering, and limits for each video quality.-filter:v:N "scale_cuda=W:H"
– scaling with CUDA hardware acceleration.-c:a libfdk_aac -b:a 128k
– encoding the audio stream using AAC (libfdk_aac) with a bitrate of 128 kbps.-max_muxing_queue_size 1024
– increasing the muxing queue size to prevent errors when writing to MP4.
Encoding a video segment¶
Sometimes you need to encode only a specific segment of your video stream.
To do this, use the following parameters: -ss 00:00:00 -t 00:05:00
.
The value of the ss
parameter specifies the start of the segment in seconds. The value of the t
represents the segment's duration.
These parameters can be used with other commands. For instance:
ffmpeg -analyzeduration 11M -hwaccel cuvid -c:v h264_cuvid -vsync 2 -avoid_negative_ts disabled -i hall.mp4 -ss 00:00:00 -t 00:05:00 -err_detect ignore_err -loglevel repeat+level+verbose -spatial-aq 1 -aq-strength 8 -y \
-map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:v:0 -map 0:a:0 \
-profile:v high -preset:v slow -threads 0 \
-c:v h264_nvenc -cbr 1 -rc cbr_hq -2pass 1 -bf 2 -b_ref_mode 2 -g 50 -b_adapt 0 -no-scenecut 1 -forced-idr 1 -strict_gop 1 \
-c:a libfdk_aac -b:a 128k \
-b:v:0 650k -maxrate:v:0 700k -minrate:v:0 650k -bufsize:v:0 700k -filter:v:0 "scale_cuda=640:360" -trellis 1 \
-b:v:1 1000k -maxrate:v:1 1100k -minrate:v:1 1000k -bufsize:v:1 1000k -filter:v:1 "scale_cuda=768:432" -trellis 1 \
-b:v:2 2250k -maxrate:v:2 2350k -minrate:v:2 2250k -bufsize:v:2 2250k -filter:v:2 "scale_cuda=960:540" -trellis 1 \
-b:v:3 3000k -maxrate:v:3 3100k -minrate:v:3 3000k -bufsize:v:3 3M -filter:v:3 "scale_cuda=-1:720" -trellis 1 \
-b:v:4 5000k -maxrate:v:4 5100k -minrate:v:4 5000k -bufsize:v:4 5M -filter:v:4 "scale_cuda=-1:1080" -trellis 1 \
-f mp4 "hall_nvenc_mbr.mp4" \
-max_muxing_queue_size 1024
This is the aforementioned encoding command, but applied only to the first 5 seconds of the video clip.
Converting files for web streaming¶
Flussonic supports the following Containers and codecs. If your video file is encoded with a different codec, it will not play via Flussonic. For example, the file might have an old codec like Xvid or MPEG4-Video that is no longer supported by new versions of browsers. In such cases, file transcoding is required.
To convert any file to H.264, in the console change the directory to where you've put this file, and run the command:
ffmpeg -i input_file.avi -c:v libx264 -g 100 -c:a aac -f mp4 output_file.mp4
Similarly, to convert any file to H.265/HEVC:
ffmpeg -i input_file.avi -c:v libx265 -g 100 -c:a aac -f mp4 output_file.mp4