fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.64k stars 874 forks source link

fluent-ffmpeg doesn't resize videos correctly when given specific size #898

Open anonrig opened 5 years ago

anonrig commented 5 years ago

Version information

Code to reproduce

First cut the videos using this command:

const cutVideo = async (currentWord) => {
    return new Promise((resolve, reject) => {
        console.log('cutting video', currentWord.file)
        ffmpeg(currentWord.file)
            .videoCodec('libx264')
            // .addOptions('-vf "setdar=ratio=16/9:max=1000"')
            .on('start', command => console.log('command', command))
            .on('error', reject)
            .on('end', resolve)
            .withSize('640x360')
            .withAspect('16:9')
            .applyAutopadding(true, 'black')
            .saveToFile(currentWord.file.replace('-unfinished', ''), './')
    })
}

Later merge them together using .mergeToFile() command:

const mergeFilesAsync = async function(files, folder, filename)
{
    return new Promise((resolve, reject) => {
        console.log('merging files', files)
        var cmd = ffmpeg({ logger: console })
            .videoCodec('libx264')
            .on('start', command => console.log('command', command))
            .on('error', reject)
            .on('end', resolve)

        for (var i = 0; i < files.length; i++)
        {
            const currentWord = files[i]
            cmd.input(currentWord.file.replace('-unfinished', ''))
        }

        cmd.mergeToFile(folder + "/" + filename, folder);
    });
}

(note: if the problem only happens with some inputs, include a link to such an input file)

Expected results

The videos resized in cutVideo function should have 640x360 size with 16:9 aspect ratio.

Observed results

The first video processed had a dimension of: 850 × 480, the output after processing it is: 642 × 360 (It should be 640x360)

The second video processed had a dimension of: 1152 × 480, the output after processing it is: 638 × 360

The third video processed had a dimension of 853 × 480, the output after processing it is: 642 × 360

FFmpeg command produced by fluent-ffmpeg:

ffmpeg -i /Users/yagiz/Desktop/video-creator/what's-unfinished.mp4 -y -vcodec libx264 -filter:v scale=w='if(gt(a,1.7777777777777777),640,trunc(360*a/2)*2)':h='if(lt(a,1.7777777777777777),360,trunc(640/a/2)*2)',pad=w=640:h=360:x='if(gt(a,1.7777777777777777),0,(640-iw)/2)':y='if(lt(a,1.7777777777777777),0,(360-ih)/2)':color=black /Users/yagiz/Desktop/video-creator/what's.mp4
cutting video /Users/yagiz/Desktop/video-creator/up?-unfinished.mp4

and

ffmpeg -i /Users/yagiz/Desktop/video-creator/up?-unfinished.mp4 -y -vcodec libx264 -filter:v scale=w='if(gt(a,1.7777777777777777),640,trunc(360*a/2)*2)':h='if(lt(a,1.7777777777777777),360,trunc(640/a/2)*2)',pad=w=640:h=360:x='if(gt(a,1.7777777777777777),0,(640-iw)/2)':y='if(lt(a,1.7777777777777777),0,(360-ih)/2)':color=black /Users/yagiz/Desktop/video-creator/up?.mp4

I think that 1.77777 value in this command produces a lower bound or an upper bound of the actual item.

anonrig commented 5 years ago

I think that because of this issue, I'm getting this error on ffmpeg

Input link in1:v0 parameters (size 640x360, SAR 399:400) do not match the corresponding output link in0:v0 parameters (640x360, SAR 1:1)