synopsis-video
Version:
Create Video Synopsis from provided videos. Frames are extracted from each video provided and backgrounds are subtracted around moving elements and merged into a single video for quick review.
77 lines (74 loc) • 2.98 kB
JavaScript
const {
mainDirectory,
mergedFramesDirectory,
stripNameJpg,
splitForFfmpeg,
} = require('./utils.js');
const fs = require('fs');
const spawn = require('child_process').spawn;
const createVideoFromJpegImages = (frameDirectory,frames,framesPerSecond,videoOutputLocation) => {
return new Promise((resolve,reject) => {
framesPerSecond = parseInt(framesPerSecond)
if(!framesPerSecond || isNaN(framesPerSecond))framesPerSecond = 2
var concatFiles = []
const stripName = (name) => {
return parseInt(name.replace('.jpg',''))
}
frames.forEach(function(filename){
var n = stripName(filename)
concatFiles[n] = frameDirectory + filename
})
if(concatFiles.length > framesPerSecond){
videoOutputLocation = videoOutputLocation ? videoOutputLocation : mainDirectory + 'videoOutput.mp4'
var commandTempLocation = `concatImagesAndMakeVideo.sh`
var currentFile = 0
var commandString = `ffmpeg -y -pattern_type glob -f image2pipe -vcodec mjpeg -r ${framesPerSecond} -analyzeduration 10 -i - -q:v 1 -c:v libx264 -r ${framesPerSecond} "${videoOutputLocation}"`
fs.writeFileSync(commandTempLocation,commandString)
var videoBuildProcess = spawn('sh',[commandTempLocation])
var isKilling = false
var killTimeout
const exitBuilding = () => {
setTimeout(()=>{
resolve({
ok: true,
fileLocation: videoOutputLocation,
msg: 'Done Building'
})
},5000)
}
videoBuildProcess.stderr.on('data',function(data){
console.log(data.toString())
clearTimeout(killTimeout)
killTimeout = setTimeout(()=>{
if(isKilling)return;
isKilling = true
videoBuildProcess.kill('SIGTERM')
},4000)
})
videoBuildProcess.on('exit',function(data){
exitBuilding()
})
var readFile = function(){
var filePath = concatFiles[currentFile]
fs.readFile(filePath,function(err,buffer){
if(!err)videoBuildProcess.stdin.write(buffer)
if(currentFile === concatFiles.length - 1){
//is last
}else{
setTimeout(function(){
++currentFile
readFile()
},1/framesPerSecond)
}
})
}
readFile()
}else{
resolve({
ok: false,
msg: 'More Frames or Lower FPS'
})
}
})
}
exports.createVideoFromJpegImages = createVideoFromJpegImages