Node.js Fluent-FFMPEG Example to Cut or Trim MP4 Video Without Re Encoding With Specified Start & End Time in Javascript Full Project For Beginners

 

 

npm i fluent-ffmpeg

 

 

To use the same codec and avoid re encoding all the video, simply pass copy as argument in withVideoCodec and withAudioCodec:

 

 

index.js

 

 

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

Leave a Reply