0

I am trying to combine a couple of .mp4 video files into one. I assumed, I can just append them to a writeStream just like we do with normal files. I wanted to do this synchronously. I have my files named 1.mp4, 2.mp4 and so on. If i have n such files, this code gets executed:

 var combineFiles = function(len,cur,ext){
 if(cur <= len)
  {
    var r = fs.createReadStream('videos/'+cur+'.'+ext);
    r.pipe(fs.createWriteStream('videos/compilation.'+ext, { flags: 'a'}))
    .on('finish', function(){combineFiles(len,cur+1,ext);});

  }
  else if(cur == len+1)
     console.log("Compilation successful!");
 };


 combineFiles(n,1,"mp4"); //n is the total number of files, 1 is the starting file name, and mp4 is the extension

So I listen to the finish event to confirm that the file writing operation is over and then call this recursive function again.

I see that the size of the combined file is roughly the sum of individual files being combined but I am not able to play it on any media Player. Only the first video out of the n gets played. So if the first video is of 15 seconds and I have 10 such files, the final file is the size of these 10 combined; however, the duration is only 15 seconds and one only sees the first video when played in a media player.

I hope I was able to explain my problem correctly. What am I doing wrong here? How do I check that the files are getting combined properly and that they are not corrupted in any way. Also, I hope I can use writeStream and readStream to combine mp4 files.

Any help is appreciated.

Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
  • Did you read mp4 spec? I think You need to remove headers from 2+ files and alter header for file 1. could be even more work – E_p Jan 18 '16 at 19:00
  • No, I haven't @E_p and thanks for pointing it out. Could you help me with some documentation or links I can read up which will help me move in the right direction? – Vivek Pradhan Jan 18 '16 at 19:03
  • use ffmpeg to concat AV files, it will glue them together properly. – dandavis Jan 18 '16 at 19:13
  • @dandavis, I understand. `ffmpeg` seems to be the suitable way of doing this but I was wondering if this can be accomplished simply using file streams in nodejs. – Vivek Pradhan Jan 18 '16 at 19:27
  • http://stackoverflow.com/questions/29565068/mp4-file-format-specification – E_p Jan 18 '16 at 21:24

0 Answers0