Cut multiple parts from a video and merge them tog

2019-08-08 06:00发布

I am trying to cut specific parts out from a video and then merge all those parts into a single video file using nodejs and ffmpeg.

Here's my code and currently I can cut only one part out of the video from .setStartTime to .setDuration and that part is being saved.

var ffmpeg = require('fluent-ffmpeg');

var command = ffmpeg()
  .input('./videos/placeholder-video.mp4')
  .setStartTime('00:00:03')
  .setDuration('02')
  .output('./videos/test.mp4')

  .on('start', function(commandLine) {
    console.log('Started: ' + commandLine);
  })

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

  .on('error', function(err){
    console.log('error: ', +err);
  }).run();

How do I cut out multiple parts out from the video and merge them in a single video file. I know about .mergeToFile method but how do I use it after cutting different parts from my video.

I tried using .setStartTime and .setDuration twice like below but the first one's are being ignored.

.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:03')
.setDuration('02')
.setStartTime('00:00:15')
.setDuration('05')
.output('./videos/test.mp4')

1条回答
看我几分像从前
2楼-- · 2019-08-08 06:45

PLEASE READ UPDATE

Could not test it yet, but try following: Set the input video two times, like this:

.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:03')
.setDuration('02')
.input('./videos/placeholder-video.mp4')
.setStartTime('00:00:15')
.setDuration('05')

Then you can merge the multiple inputs with .mergeToFile.


UPDATE

My answer cannot work due to restriction of .setDuration. It is an output option, so it defines how long transcoding to the output file is done: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg It is not used to define the length/duration of the input.

Another option would be .loop, but apparently it is not supported for this purpose: https://video.stackexchange.com/questions/12905/repeat-loop-input-video-with-ffmpeg/12906#12906

If you really want to use nodejs, you need multiple commands

  • cut the input video to temporary files, one for each cut
  • merge the temporary files to one output file

Something like this:

var command1 = ffmpeg()
  .input('./small.mp4')
  .seekInput('00:00:02')
  .withDuration(1)
  .output('./temp1.mp4')
  .run();

var command2 = ffmpeg()
  .input('./small.mp4')
  .seekInput('00:00:01')
  .withDuration(3)
  .output('./temp2.mp4')
  .run();

var command2 = ffmpeg()
  .input('./temp1.mp4')
  .input('./temp2.mp4')
  .mergeToFile('./merge.mp4', './temp');

Big problem: your nodejs script will not start if temporary files are not present. So it would be much more easier to use a bash or batch script.

查看更多
登录 后发表回答