Search code examples
node.jsffmpegfluent-ffmpeg

Cutting a video without re encoding using ffmpeg and nodejs (fluent-ffmpeg)


I have a quick question, I'm trying to do a cloud video editor, and i want to be able to cut out video usng nodejs.

I'm using fluent-ffmpeg. Here is my code :

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();
  });
};

It's working but not optimal, and as soon as i try to edit to a long video (getting 10 min from a video instead of 1 min out) it's super long.

What i understand is that ffmpeg re encode everything, so that's why the longer the edit is the longer the process will. Is there way to cut out using node-fluent-ffmpeg without re encoding everything ?

Thanks to the community !


Solution

  • After digging in node fluent doc and the info from @szatmary, it's possible to define audio and video codec. To use the same codec and avoid re encoding all the video, simply pass copy as argument in withVideoCodec and withAudioCodec:

    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();
      });
    };