Fluent-ffmpeg 视频已拉伸图像

问题描述 投票:0回答:2

我有一个 mp3 音频文件和一个 jpg 图像文件。我想将这两个文件合并成一个新的mp4。我有一个工作流利的 ffmpeg 命令,它完全符合我的要求,除了图像将在最终输出视频中拉伸之外。这似乎与我从 Photoshop 导出的 jpg 一致。

有什么方法可以在 ffmpeg 命令中指定以保持图像的相同分辨率而不拉伸它?

我的功能如下:

async function debugFunction() {
    console.log('debugFunction()')
    //begin setting up ffmpeg
    const ffmpeg = require('fluent-ffmpeg');
    //Get the paths to the packaged versions of the binaries we want to use
    var ffmpegPath = require('ffmpeg-static-electron').path;
    ffmpegPath = ffmpegPath.replace('app.asar', 'app.asar.unpacked')
    var ffprobePath = require('ffprobe-static-electron').path;
    ffprobePath = ffprobePath.replace('app.asar', 'app.asar.unpacked')
    //tell the ffmpeg package where it can find the needed binaries.
    ffmpeg.setFfmpegPath(ffmpegPath);
    ffmpeg.setFfprobePath(ffprobePath);
    //end setting ffmpeg

    let imgPath = "C:\\Users\\marti\\Documents\\martinradio\\image.jpg";
    let audioPath = "C:\\Users\\marti\\Documents\\martinradio\\audio.mp3";
    let vidPath = "C:\\Users\\marti\\Documents\\martinradio\\video.mp4";

    //create ffmpeg command
    ffmpeg()
    //set rendering options
    .input(imgPath)
    .loop()
    .addInputOption('-framerate 2')
    .input(audioPath)
    .videoCodec('libx264')
    .audioCodec('copy')
    .audioBitrate('320k')
    .videoBitrate('8000k', true)
    .size('1920x1080')
    .outputOptions([
        '-preset medium',
        '-tune stillimage',
        '-crf 18',
        '-pix_fmt yuv420p',
        '-shortest'
    ])
    //set status events
    .on('progress', function (progress) {
        if (progress.percent) {
            console.log(`Rendering: ${progress.percent}% done`)
        }
    })
    .on('codecData', function (data) {
        console.log('codecData=', data);
    })
    .on('end', function () {
        console.log('Video has been converted succesfully');
    })
    .on('error', function (err) {
        console.log('errer rendering video: ' + err.message);
    })
    //run ffmpeg command
    .output(vidPath).run()

}

如果我给它一个音频文件和这个图像,就会成功渲染:

但是输出视频看起来像这样:

你可以看到图像被压扁并像矩形一样拉伸,而我想保持它是一个立方体。

javascript node.js npm ffmpeg fluent-ffmpeg
2个回答
0
投票

您似乎使用 16:9 的比例,例如.size('1920x1080') 对于 599X603 的图片。您可以更改输出视频的尺寸,使其符合您的实际图像尺寸(可以更大或更小,但保持相似的比例)或使用 16:9 但在两侧添加填充


0
投票

基本上,ffmpeg 的 .size 属性强制设置视频图形的尺寸。 您可以简单地忽略 .size('1920x1080') 属性。 那么你就可以出发了

ffmpeg()
//set rendering options
.input(imgPath)
.loop()
.addInputOption('-framerate 2')
.input(audioPath)
.videoCodec('libx264')
.audioCodec('copy')
.audioBitrate('320k')
.videoBitrate('8000k', true)
// .size('1920x1080')  just comment this out
.outputOptions([
    '-preset medium',
    '-tune stillimage',
    '-crf 18',
    '-pix_fmt yuv420p',
    '-shortest'
])
© www.soinside.com 2019 - 2024. All rights reserved.