This article shall give an example of the settings and best practices that we can use in the encoding configurations with Per-Title when we have a use case where we are restricted to specific requirements for renditions or bitrates. Following this tutorial, it is expected to have basic knowledge about the Per-Title functionality and how to set up a basic configuration. A comprehensive overview of the available parameters is available in Per-Title Configuration Options explained.
Use case requirements
The described use case has the following requirements and shall demonstrate an example.
- We want to encode a fixed amount of H.264 renditions with specified resolutions.
- We want to specify the maximum bitrate for each rendition.
- The Per-Title algorithm shall calculate the optimal bitrate for each rendition to achieve an optimised bitrate ladder (What is Per-Title Encoding?).
An example could be if we want to take the recommended bitrate ladder from Apple for H.264 and limit the bitrate to these specifications. Still, according to the complexity of the content, we want to save as much bandwidth as possible.
|416 x 234||145k|
|640 x 360||365k|
|768 x 432||730k|
|768 x 432||1100k|
|960 x 540||2000k|
|1280 x 720||3000k|
|1280 x 720||4500k|
|1920 x 1080||6000k|
|1920 x 1080||7800k|
Table1: HTTP Live Streaming (HLS) Authoring Specification for Apple Devices
When configuring Per-Title, we need to specify at least one Per-Title template for the stream mode to enable the algorithm to do its magic.
These three different kinds of Per-Title-Templates are available.
For a simple Per-Title encoding, we usually specify one
autoRepresentations, and Per-Title will take care of the rest. More information can be found in our tutorial How to create a Per-Title Encoding.
But according to our requirements, we need control over the resolutions and bitrates of each of our renditions; therefore, we will specify one
PER_TITLE_TEMPLATE_FIXED_RESOLUTION_AND_BITRATE per required rendition.
Creating stream templates and setting encoding parameters
To specify the resolution and the maximum bitrate, we must create one stream template for each rendition. We assign individual H.264 configurations with the required
height specified. In the
per_title_settings of the streams, we set our boundaries via the
stream_fixed_res_bit_settings. The here-defined
max_bitrate per rendition are crucial to enable Per-Title to pick the correct renditions. Together with the common settings
max_bitrate in the Per-Title settings'
start_encoding_request, it is essential to find the right combination. Suppose these settings are not optimised to each other; in that case, Per-Title will be forced to drop desired renditions or add additional unwanted renditions. In the worst case, finding renditions based on the given boundaries is impossible, leading to an encoding error.
To start with, we can specify all the settings specific to the single renditions in an object to keep a clear overview.
1EncodingProfile = collections.namedtuple('EncodingProfile', 'width min max')2video_profiles = [3 EncodingProfile(width=416, min=36_250, max=145_000),4 EncodingProfile(width=640, min=91_250, max=365_000),5 EncodingProfile(width=768, min=182_500, max=730_000),6 EncodingProfile(width=768, min=275_000, max=1_100_000),7 EncodingProfile(width=960, min=500_000, max=2_000_000),8 EncodingProfile(width=1280, min=750_000, max=3_000_000),9 EncodingProfile(width=1280, min=1_125_000, max=4_500_000),10 EncodingProfile(width=1920, min=1_500_000, max=6_000_000),11 EncodingProfile(width=1920, min=1_950_000, max=7_800_000)12]
width and the
max (bitrate) are clear; this comes from our initial requirements. The
min (bitrate) needs a bit of gut feeling (or complicated calculation). You don't want to set this parameter too low, even if this seems to widen the boundaries. As said before, this parameter works especially in combination with the
min_bitrate_step_size and the
max_bitrate_step_size in the
1# Start Encoding Job2start_encoding_request = StartEncodingRequest(3 per_title=PerTitle(4 h264_configuration=H264PerTitleConfiguration(5 min_bitrate=36_250,6 max_bitrate=7_800_000,7 min_bitrate_step_size=1.2,8 max_bitrate_step_size=4.0,9 target_quality_crf=22,10 complexity_factor=1.0,11 )12 )13)14bitmovin_api.encoding.encodings.start(encoding_id=encoding.id,start_encoding_request=start_encoding_request)
width (or the
height) is specified in the configuration,
1# Video Configuration2video_configuration = H264VideoConfiguration(3 width=video_profile.width,4 preset_configuration=PresetConfiguration.VOD_HIGH_QUALITY,5)6video_configuration = bitmovin_api.encoding.configurations.video.h264.create(h264_video_configuration=video_configuration)
max_bitrate are configured in the stream settings.
1# Video Streams2stream_fixed_res_bit_settings = StreamPerTitleFixedResolutionAndBitrateSettings(3 min_bitrate=video_profile.min,4 max_bitrate=video_profile.max,5 bitrate_selection_mode=BitrateSelectionMode.COMPLEXITY_RANGE,6 low_complexity_boundary_for_max_bitrate=1_950_000,7 high_complexity_boundary_for_max_bitrate=7_800_0008)9stream_per_title_settings = StreamPerTitleSettings(fixed_resolution_and_bitrate_settings=stream_fixed_res_bit_settings)10video_stream = Stream(11 input_streams=[stream_input],12 codec_config_id=video_configuration.id,13 mode=StreamMode.PER_TITLE_TEMPLATE_FIXED_RESOLUTION_AND_BITRATE,14 per_title_settings=stream_per_title_settings15)16video_stream = bitmovin_api.encoding.encodings.streams.create(encoding_id=encoding.id, stream=video_stream)
For this use case, I found promising results for the
min_bitrate by dividing the
max_bitrate_stepsize and setting the
max_bitrate_stepsize = 4.
max_bitrate_stepsize was also necessary to set the proper boundaries for this use case.
Here we have a good example of what happens when not finding the correct settings – Per-Title was forced to generate only seven renditions out of the nine desired renditions when using the default setting of
max_bitrate_stepsize = 1.9.
The same counts for the
min_bitrate_step_size; this can also mess up the algorithm if not chosen carefully.
Having found the right combination of bitrate and step size settings, one must understand that the normal Per-Title behaviour calculates the renditions from bottom to top. To gain some influence over the bitrate of the highest rendition, we can use the
COMPLEXITY_RANGE in the
bitrate_selection_mode of the stream settings.
1# Video Streams2stream_fixed_res_bit_settings = StreamPerTitleFixedResolutionAndBitrateSettings(3 min_bitrate=video_profile.min,4 max_bitrate=video_profile.max,5 bitrate_selection_mode=BitrateSelectionMode.COMPLEXITY_RANGE,6 low_complexity_boundary_for_max_bitrate=1_950_000,7 high_complexity_boundary_for_max_bitrate=7_800_0008)
Here the algorithm first calculates the bitrate of the top rendition in the ladder based on the complexity analysis of the source file and the configured Per-Title settings (such as
complexityFactor) and takes this into account while picking the renditions. It then compares this top bitrate with a configurable complexity range defined by its minimum and maximum bitrates. To ensure that the highest rendition falls into this range, in this use case, I specified the min and max values according to the specified min and max bitrates of this rendition.
With all these settings in place, we achieve the desired outcome with all the required renditions.
Influence the bitrate of the highest rendition
The main advantage of Per-Title is to determine the optimal renditions based on the complexity of the content. When setting the proper boundaries, Per-Title can significantly save bandwidth and storage for low-complex content compared to static renditions or ensure that quality requirements are still fulfilled with very high-complex content.
The default settings for using the contents’ calculated complexity to pick the optimum bitrates usually provide good results. Nevertheless, it is still possible to tweak the rendition ladder to achieve an overall higher or lower quality (or lower or higher bitrates). It is also possible to process different kinds of content with various complexities individually, for example, to reduce the bitrates of videos that are known to have high complexity.
One can influence these factors with the
target_quality_crf and the
complexity_factor in the
start_encoding_request. With the
target_quality_crf, it is possible to shift the quality of the entire ladder up or down (within the boundaries limited by all the other settings). The default CRF for H.264 is specified as 22; anything below will increase the quality (and the bitrates), and anything above will reduce accordingly.
1# Start Encoding Job2start_encoding_request = StartEncodingRequest(3 per_title=PerTitle(4 h264_configuration=H264PerTitleConfiguration(5 min_bitrate=36_250,6 max_bitrate=7_800_000,7 min_bitrate_step_size=1.2,8 max_bitrate_step_size=4,9 target_quality_crf=22,10 complexity_factor=1.0,11 )12 )13)
complexity_factor has basically the same effect. The default here is
1.0; a higher factor will affect Per-Title to consider the content more complex and assign higher bitrates, while a lower factor (>0) will cause the content to be considered lower complex and assign lower bitrates. Small changes to
complexity_factor (ie +/- 0.2) is recommended for your initial testing.
This is especially useful to specify the desired quality for an entire library with one target CRF but still be able to treat various kinds of content differently.
Please refer to the per_title_constraints.py Python script for a full example!
Frequently asked questions
Q. The default output from the algorithm is too high in quality
A1. Bring all assets up or down in quality using targetCRF (increase to reduce quality)
A2. Bring all assets up or down in quality using complexity factor (reduce to reduce quality). Note: only change by 0.1 initially. Recommend changing one, not both.
Q. My complex assets are too high in quality, but my simple assets are perfect. I want to change the bias.
A1. Adjust the gap between low and high_complexity_boundaries , think of this like a magnifying glass. The smaller the gap between the numbers, the more variation in quality you see between a complex and simple asset (you are bringing the magnifying glass closer to the page)
Q. In my assets, on average the lower renditions are too low in quality, the upper renditions are perfect.
A1. Adjust the minimum and maximum bitrate, found in the start request. In this scenario increasing the minimum would likely produce the output you are looking for.