Skip to content

Instantly share code, notes, and snippets.

@kpnemo
Created September 3, 2014 12:20
Show Gist options
  • Select an option

  • Save kpnemo/d79cc342b3fdd7493cfb to your computer and use it in GitHub Desktop.

Select an option

Save kpnemo/d79cc342b3fdd7493cfb to your computer and use it in GitHub Desktop.
var _mergeChunks = function(id, chunksArr, callback){
var exec = require('child_process').exec,
tmpDir = config.getUploadPath(),
fileHash = crypto.createHash('md5').update(id + moment().unix()).digest('hex'),
txtFile = 'concat_' + fileHash + '_' + id + '.txt',
videoOut = 'concat_' + fileHash + '_' + id + '.mp4',
stream = fs.createWriteStream(tmpDir + '/' + txtFile),
cmd;
stream.once('open', function(fd) {
_.each(chunksArr, function(chunk, chunkIndex){
stream.write('file ' + '\'' + chunk.file.path + '\'' + "\n");
if(chunkIndex == chunksArr.length -1) stream.end();
});
});
cmd = 'ffmpeg -f concat'
+ ' -i ' + tmpDir + '/' + txtFile
+ ' -c copy ' + tmpDir + '/' + videoOut;
console.log('FFMPEG', moment().unix(), cmd);
exec(cmd, function(err, stdout, stderr){
if(err) console.error('err: ' + err);
console.info('stdout: ' + stdout);
console.info('stderr: ' + stderr);
if(null == err){
_.each(chunksArr, function(chunkObj){
console.log('Cleanup chunk ', chunkObj.file.path);
fs.unlink(chunkObj.file.path);
});
console.log('Cleanup ffmpeg txt ', tmpDir + '/' + txtFile);
fs.unlink(path.join(tmpDir, txtFile));
callback(null, {outFile: path.join(tmpDir, videoOut), outFileName: videoOut});
} else {
callback(true, err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment