javascriptmethodschainedfluent-ffmpeg

How to loop through chained methods with dynamic data?


Using fluent-ffmpeg, I have a dynamic list of videos I am trying to stitch together using Node. The below code works but was manually hard-written in. How would I turn this into a loop with the chained function?

I looked into eval() and couldn't get that to work, and not sure if chained promises is what I'm looking for (not exactly sure what to do here).

ffmpeg()
  .input('video/video_after0.mp4')
  .input('video/video_after1.mp4')
  .input('video/video_after2.mp4')
  .input('video/video_after3.mp4')
  .input('video/video_after4.mp4')
  .input('video/video_after5.mp4')
  .input('video/video_after6.mp4')
  .input('video/video_after7.mp4')
  .input('video/video_after8.mp4')
  .input('video/video_after9.mp4')
  .input('video/video_after10.mp4')
  .input('video/video_after11.mp4')
  .input('video/video_after12.mp4')
  .input('video/video_after13.mp4')
  .input('video/video_after14.mp4')
  .input('video/video_after15.mp4')
  .input('video/video_after16.mp4')
  .input('video/video_after17.mp4')
  .input('video/video_after18.mp4')
  .input('video/video_after19.mp4')
  .input('video/video_after20.mp4')
  .input('video/video_after21.mp4')
  .input('video/video_after22.mp4')
  .input('video/video_after23.mp4')
  .input('video/video_after24.mp4')
  .input('video/video_after25.mp4')
  .input('video/video_after26.mp4')
  .input('video/video_after27.mp4')
  .input('video/video_after28.mp4')
  .input('video/video_after29.mp4')
  .input('video/video_after30.mp4')
  .input('video/video_after31.mp4')
  .input('video/video_after32.mp4')
  .input('video/video_after33.mp4')
  .input('video/video_after34.mp4')

  .on('error', function(err) {
    console.log('An error occurred while merging video files: ' + err.message);
  })
  .on('end', function() {
    logger.info('****** STEP 6 ENDED ******');
    callback(null);
  })
  .mergeToFile('video/video_after.mp4', '/video/temp');

Here is my attempt at using eval() but it doesn't work:

  var ffmpegInputCommand = "ffmpeg()";

  // For Loop to create the ffmpeg().input() command for multiple inputs:
  async.forEach(array, function(data, done) {
    if (data.id < array.length - 1) {
      ffmpegInputCommand += ".input('" + videoAfter + data.id + ".mp4')";
    }
    done();
  }, function (err) {
    if (err) throw err;
    else {
      eval(ffmpegInputCommand).on('error', function(err) {
        console.log('An error occurred while merging video files: ' + err.message);
      })
      .on('end', function() {
        logger.info('****** STEP 6 ENDED ******');
        callback(null);
      })
      .mergeToFile('video/video_after.mp4', '/video/temp');
    }
  });
  // End: For-Loop
}

The variables were defined prior to this above code.


Solution

  • Why not making a simple loop?

    start = 0;
    max = 35;
    while (start < max) {
      fname = 'video/video_after'+str(start)+'.mp4'
      ffmpeg().input(fname)
      start += 1;
    }