Cut a video in specific start and end time in Node.js

This tutorial will show you how to cut a video in specific start and end time in Node.js using ffmpeg library. For this purpose, we will use the fluent-ffmpeg library which is a wrapper library of ffmpeg.

Installation:

npm i fluent-ffmpeg

Write the above code in your terminal and execute it to install the fluent-ffmpeg library.

const ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
ffmpeg.setFfmpegPath(ffmpegPath)

In the above program, theffmpeg commands will be generated and executed by the fluent-ffmpeg library.

Then the @ffmpeg-installer/ffmpeg library will install a binary of ffpmeg automatically and will get the path and version for the current platform.

After that, I set FfmpegPath and passed the variable ffmpegPath that I have declared in the above line.

ffmpeg('video.mp4')
  .setStartTime('00:00:03')
  .setDuration('05')
  .output('output.mp4')

First of all, ffmpeg('video.mp4') is the path of the video that we want to cut. The video file must be present in the working directory.

.setStartTime('00:00:03') to define the starting time from where we would like to cut the video. Here our starting time will be 3rd second of the video.

.setDuration('10') to define the duration of the edited video. The duration will be 10 seconds here as I passed 10 inside in sec.

Then, .output('output.mp4') is the path where the edited video file will be created.

  .on('start', function(commandLine){
    console.log('Processing')
})

The .on('start') event will execute when the process starts. It will print Processing if there is no error.

.on('end', function(err) {
    if(!err) { console.log('Done') }
  })

When the process is over the .on("end") event will execute and print “Done” message through the console.log() if there is no error taking place.

.on('error', err => console.log('error: ', err))
.run()

The .on('error') event will execute if there is any sort of error taking place and display the error which is taken place.

.run() must be called to begin the process. without it, the process will not start.

Here’s the complete code below.

const ffmpeg = require('fluent-ffmpeg')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
ffmpeg.setFfmpegPath(ffmpegPath)

ffmpeg('video.mp4')
  .setStartTime('00:00:03')
  .setDuration('05')
  .output('output.mp4')

  .on('start', function(commandLine){
    console.log('Processing')
})
  .on('end', function(err) {
    if(!err) { console.log('conversion Done') }
  })
  .on('error', err => console.log('error: ', err))
  .run()

To run the above program open your terminal type node then your file name like below and press enter.

PS G:\saM> node index.js

By running the above program, if there is no error taken place you will get output.mp4 into your working directory that’s what you want.

Finally, we are able to trim our video from a specific start position and an end position.

Also read: Convert MP4 Video to GIF Animation in Node.js

Leave a Reply

Your email address will not be published. Required fields are marked *