【问题标题】:FFMPEG: How to combine video and image (video template)FFMPEG:如何结合视频和图像(视频模板)
【发布时间】:2021-09-06 11:43:50
【问题描述】:

目标

我有一个视频和一个图像(视频模板),我试图将它们组合成一个输出视频(1080w x 1920h - 9:16 宽高比)。

  • 输入视频 - 1920x1080
  • 输入图像 - 1080x1920
  • 输出视频 - 1080x1920

This image 显示了我想要完成的工作。两个橙色部分是输入图像 - 它是单个 .png,中间有一个透明部分用于视频。

正如标题中提到的,我正在尝试使用FFMPEG 来完成此任务。更具体地说,我使用的是fluent-ffmpeg npm 包。

当前状态

我可以很好地读取两个输入,但我无法让两者很好地相互配合。

如果我让叠加层正常工作,那么我的输出视频是 1920x1080 而不是 1080x1920。

如果输出视频尺寸正确,则视频要么被拉伸,要么在添加叠加层时出错。

代码

这是我目前所拥有的。我很乐意回答任何问题。提前感谢您查看:)

var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg();
var timemark = null;

command
  .on('end', onEnd )
  .on('progress', onProgress)
  .on('error', onError)
  .input('./input-video.mp4')
  .input('./template.png')
  .complexFilter([
    {
      filter: 'scale',
      options: { width: 1080, height: 1920 }
    },
    // {
    //   filter: 'overlay',
    //   options: { x: 100, y: 100 }
    // },
  ])
  .outputFps(30)
  .output('./output-video.mp4')
  .run();

/* Misc */

function onProgress(progress){
  if (progress.timemark != timemark) {
    timemark = progress.timemark;
    console.log('Time mark: ' + timemark + "...");
  }
}

function onError(err, stdout, stderr) {
  console.log('Cannot process video: ' + err.message);
}

function onEnd() {
  console.log('Finished processing');
}

【问题讨论】:

  • 听起来您需要先将输入视频缩放到 1080 的宽度,然后再将图像添加为第二个输入。通常命令是:ffmpeg -i input-video.mp4 -vf scale=1080:-1 -i template.png -filter_complex "overlay=100:100" output-video.mp4,所以在使用 Fluent-FFmpeg 时,在执行 complex filter 之前,您必须找到一种方法在第一个输入上设置 video filter

标签: node.js ffmpeg video-processing fluent-ffmpeg


【解决方案1】:

感谢 Reddit 上的用户 Voldrix_Suroku 我能够弄清楚这一点。

为避免成为DenverCoder9,使其工作的 CLI 命令是:

ffmpeg -i input-video.mp4 -i template.png -filter_complex "[0:V]scale=1080:-1,pad=0:1920:0:(oh-ih)/2[vid];[vid][1:v]overlay" output-video.mp4

如果你像我一样使用fluent-ffmpeg,完整代码如下:

var ffmpeg = require('fluent-ffmpeg');
var command = ffmpeg();
var timemark = null;

command
  .on('end', onEnd )
  .on('progress', onProgress)
  .on('error', onError)
  .input('./input-video.mp4')
  .input('./template.png')
  .complexFilter([
    "[0:V]scale=1080:-1,pad=0:1920:0:(oh-ih)/2[vid];[vid][1:v]overlay"
  ])
  .outputFps(30)
  .output('./output-video.mp4')
  .run();

/* Misc */

function onProgress(progress){
  if (progress.timemark != timemark) {
    timemark = progress.timemark;
    console.log('Time mark: ' + timemark + "...");
  }
}

function onError(err, stdout, stderr) {
  console.log('Cannot process video: ' + err.message);
}

function onEnd() {
  console.log('Finished processing');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 2017-03-20
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多