Keywords: FFMPEG | libx264 | video encoding
Abstract: This article delves into the "height not divisible by 2" error encountered when using FFMPEG's libx264 encoder. By analyzing the H.264/AVC standard requirements for video dimensions, it explains the root cause of the error and provides solutions without scaling the video. Based primarily on the best answer, it details the use of the pad filter to ensure width and height are even numbers through mathematical calculations while preserving original dimensions. Additionally, it supplements with other methods like crop and scale filters for different scenarios and discusses the importance of HTML escaping in technical documentation. Aimed at developers, this guide offers comprehensive insights to avoid common encoding issues with non-standard resolution videos.
When using FFMPEG for video encoding, developers often encounter a specific error: [libx264 @ 0xa3b85a0] height not divisible by 2 (520x369). This error stems from the strict requirements of the H.264/AVC encoding standard for video frame dimensions. According to the standard, the width and height of encoded videos must be divisible by 2, typically to optimize encoding efficiency and compatibility, especially during chroma subsampling. In the example, the input frame size is 520x369, where the height 369 is odd and does not meet this requirement, causing the libx264 encoder to throw an error.
Core Problem Analysis
The H.264 standard mandates even video dimensions, which is not an arbitrary restriction by FFMPEG or libx264 but based on intrinsic needs of the encoding algorithm. When input frame sizes fail to satisfy this condition, direct encoding leads to failure. Developers might attempt to use scaling filters as a fix, but as noted in the question, sometimes it is necessary to maintain original dimensions to avoid image distortion or additional processing overhead. Thus, finding a method that adjusts size without altering image content is crucial.
Solution: Using the Pad Filter
The best answer provides an elegant solution: using FFMPEG's pad filter. This filter adjusts video dimensions by adding padding pixels to ensure width and height are even numbers, without scaling. The specific command is as follows:
ffmpeg -i frame_%05d.jpg -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -r 24 -y -an video.mp4
Here, the -vf parameter specifies the video filter chain. pad=ceil(iw/2)*2:ceil(ih/2)*2 is the filter expression, where iw and ih represent the input image's width and height, respectively. The ceil() function rounds up the division result, then multiplies by 2 to ensure an even outcome. For example, for a height of 369, the calculation is: ceil(369/2) = ceil(184.5) = 185, then 185 * 2 = 370, so the output height becomes 370, one pixel more than the original, achieved by adding black padding. The default padding color is black, but it can be modified by adding a :color=white parameter, e.g., pad=ceil(iw/2)*2:ceil(ih/2)*2:color=white.
Additional Methods
Beyond the pad filter, other answers mention crop and scale filters as alternatives. The crop filter makes dimensions divisible by 2 through cropping, e.g., crop=trunc(iw/2)*2:trunc(ih/2)*2, but this loses part of the image content and is suitable for scenarios where cropping is acceptable. The scale filter allows video scaling, e.g., scale=1280:-2, where -2 ensures the height is automatically calculated and even, but it changes image dimensions and may affect quality. These methods should be chosen based on specific needs, with the pad filter being the most direct for preserving original size.
Practical Considerations
In practice, developers should ensure input frames have consistent formats and resolutions to avoid other encoding errors. Additionally, understanding FFMPEG filter syntax and parameters is key for custom solutions. For instance, the pad filter supports other options like padding position and color, refer to official documentation for advanced configurations. For batch processing, scripting is recommended to automate the process and improve efficiency.
HTML Escaping in Technical Documentation
When writing technical articles, proper HTML escaping is essential to prevent code examples from being misparsed. For example, when describing HTML tags, such as <br>, angle brackets must be escaped to avoid them being interpreted as tag instructions. This ensures content accuracy and readability, especially when displaying code on online platforms.
In summary, by using the pad filter, developers can easily resolve the "height not divisible by 2" error while maintaining original video dimensions. This method not only complies with the H.264 standard but also offers flexibility, making it an ideal choice for handling non-standard resolution videos. Combined with other filter options, FFMPEG provides a powerful toolkit for video encoding, helping developers tackle various challenges.