Tag Archives: ffmpeg

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

When ffmpeg is used to push streaming, there may be an error: unknown encoder ‘libx264’

When using the following command to push the stream:

ffmpeg -re -i a.mp4 -vcodec libx264 -acodec aac -f flv rtmp://172.31.102.165:1935/myapp 

The following errors may occur:

Unknown encoder 'libx264'

The libx264 library is missing and needs to be installed.

1. gitclonegit://git.videolan.org/x264.git

2. cdx264

3. ./configure –enable-static –enable-share

4. make

5. sudo make install

Need to recompile and install ffmpeg

1. When configuring ffmpeg, add x264, e.g.

./configure –enable-gpl –enable-libx264

2. make clean

3. make

4. make install

At this point, when entering ffmpeg, an error may occur:.

ffmpeg: error while loading shared libraries: libavdevice.so.57: cannot open shared object file: No such file or directory

*****************************************

Modify the ld.so.conf file

1. $ sudo vi /etc/ld.so.conf

2. Modify the following

include ld.so.conf.d/*.conf
/usr/local/ffmpeg/lib ///usr/local/ffmpeg directory is my ffmpeg installation directory, change it according to your installation directory

3. make it work

$ sudo ldconfig

*****************************************

Add environment variables for Ffmpeg
vi /etc/profile
Add:
export PATH=”/usr/local/ffmpeg/bin:$PATH”

Then save and run source /etc/profile

If you are still prompted for missing libx264, you need to go into the compile ffmpeg directory and use the following command to push the stream.

./ffmpeg -re -i a.mp4 -vcodec libx264 -acodec aac -f flv rtmp://172.31.102.165:1935/myapp