# Ffmpeg scaling — the solution of “width / height not divisible by 2”

Recently, when dealing with video, there is such a demand

If the resolution width of the video is greater than 960, the width is set to 960, and the height is scaled in proportion to it

If the resolution height of the video is greater than 540, the height is set to 540, and the width is scaled in proportion to it

Many methods have been used before, which can only solve part of the problem

And it will report:

# The solution of “width/height not divisible by 2”

In this case, the width/height cannot be divided by two

In fact, the solution is very simple, first look at the code

``````//Set or save the parameters of ffmepg
string ffmpegFormart = string;
if ( width > height && width > 960 )
{
//If width is larger than height and greater than 960, scale with width of 960

ffmpegFormart = string.Format (" -vf scale=960:{0}/({1}*1.00/960) " , height , width);

//The following method can solve part of the problem, but it will report an error if the height is not divisible by 2
//ffmpegFormart = \$" -vf scale=960:-1 ";

}
else if ( width < height && height > 540 )
{
//If height is larger than width and greater than 540, scale with height 540

ffmpegFormart = string.Format (" -vf scale={0}/({1}*1.00/540):540 " , width , height);
}``````

Thinking is a primary school mathematics thinking

For example, there is a video resolution of 1280 * 720

If I want to set the height to a fixed 540, then the width should be 1280/(720/540), which is about 962

Of course, there will be a gap within one pixel, but it will not have any impact

So if the video is being processed

width: width

height: height

googol: xheight

then it can be concluded that

Dynamic width ≈ + width/(height * 1.00/xheight)

The image below is after I have processed the irregular resolution 