【问题标题】:Cutting a video without re encoding using ffmpeg and nodejs (fluent-ffmpeg)使用 ffmpeg 和 nodejs (fluent-ffmpeg) 在不重新编码的情况下剪切视频
【发布时间】:2021-01-07 20:41:52
【问题描述】:

我有一个简单的问题, 我正在尝试做一个云视频编辑器,我希望能够使用 nodejs 剪切视频。

我正在使用fluent-ffmpeg。这是我的代码:

const cutVideo = async (sourcePath, outputPath, startTime, duration) => {
  console.log('start cut video');

  await new Promise((resolve, reject) => {
    ffmpeg(sourcePath)
      .setFfmpegPath(pathToFfmpeg)
      .setFfprobePath(ffprobe.path)
      .output(outputPath)
      .setStartTime(startTime)
      .setDuration(duration)
      .on('end', function (err) {
        if (!err) {
          console.log('conversion Done');
          resolve();
        }
      })
      .on('error', function (err) {
        console.log('error: ', err);
        reject(err);
      })
      .run();
  });
};

它工作正常,但不是最佳的,一旦我尝试编辑长视频(从视频中获得 10 分钟而不是 1 分钟),它就会超长。

我的理解是 ffmpeg 重新编码所有内容,所以这就是为什么编辑越长,过程就越长的原因。 有没有办法在不重新编码所有内容的情况下使用 node-fluent-ffmpeg 进行删减?

感谢社区!

【问题讨论】:

  • 您需要精确到帧吗?如果是这样,那么所要求的就是不可能的。否则,只需添加-codec copy
  • 不幸的是我没有使用命令行但是node ffmepg fluent,你会如何使用-codec copy?
  • 我这辈子从来没有用过 fluent ffmpeg,但是通过谷歌搜索在 5 秒内找到了答案。我建议你试试看。

标签: node.js ffmpeg fluent-ffmpeg


【解决方案1】:

在挖掘 node fluent doc 和来自@szatmary 的信息后,可以定义音频和视频编解码器。要使用相同的编解码器并避免重新编码所有视频,只需将 copy 作为参数传递给 withVideoCodecwithAudioCodec

const ffmpeg = require('fluent-ffmpeg');
const pathToFfmpeg = require('ffmpeg-static');
const ffprobe = require('ffprobe-static');

const cutVideo = async (sourcePath, outputPath, startTime, duration) => {
  console.log('start cut video');

  await new Promise((resolve, reject) => {
    ffmpeg(sourcePath)
      .setFfmpegPath(pathToFfmpeg)
      .setFfprobePath(ffprobe.path)
      .output(outputPath)
      .setStartTime(startTime)
      .setDuration(duration)
      .withVideoCodec('copy')
      .withAudioCodec('copy')
      .on('end', function (err) {
        if (!err) {
          console.log('conversion Done');
          resolve();
        }
      })
      .on('error', function (err) {
        console.log('error: ', err);
        reject(err);
      })
      .run();
  });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 2020-09-22
    • 2023-04-02
    • 2016-01-27
    • 2017-10-01
    • 2011-12-23
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多