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.
