|
|
|
'use strict'; |
|
|
|
var exec = require('child_process').exec; |
|
var isWindows = require('os').platform().match(/win(32|64)/); |
|
var which = require('which'); |
|
|
|
var nlRegexp = /\r\n|\r|\n/g; |
|
var streamRegexp = /^\[?(.*?)\]?$/; |
|
var filterEscapeRegexp = /[,]/; |
|
var whichCache = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseProgressLine(line) { |
|
var progress = {}; |
|
|
|
|
|
line = line.replace(/=\s+/g, '=').trim(); |
|
var progressParts = line.split(' '); |
|
|
|
|
|
for(var i = 0; i < progressParts.length; i++) { |
|
var progressSplit = progressParts[i].split('=', 2); |
|
var key = progressSplit[0]; |
|
var value = progressSplit[1]; |
|
|
|
|
|
if(typeof value === 'undefined') |
|
return null; |
|
|
|
progress[key] = value; |
|
} |
|
|
|
return progress; |
|
} |
|
|
|
|
|
var utils = module.exports = { |
|
isWindows: isWindows, |
|
streamRegexp: streamRegexp, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
copy: function(source, dest) { |
|
Object.keys(source).forEach(function(key) { |
|
dest[key] = source[key]; |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args: function() { |
|
var list = []; |
|
|
|
|
|
var argfunc = function() { |
|
if (arguments.length === 1 && Array.isArray(arguments[0])) { |
|
list = list.concat(arguments[0]); |
|
} else { |
|
list = list.concat([].slice.call(arguments)); |
|
} |
|
}; |
|
|
|
|
|
argfunc.clear = function() { |
|
list = []; |
|
}; |
|
|
|
|
|
argfunc.get = function() { |
|
return list; |
|
}; |
|
|
|
|
|
argfunc.find = function(arg, count) { |
|
var index = list.indexOf(arg); |
|
if (index !== -1) { |
|
return list.slice(index + 1, index + 1 + (count || 0)); |
|
} |
|
}; |
|
|
|
|
|
argfunc.remove = function(arg, count) { |
|
var index = list.indexOf(arg); |
|
if (index !== -1) { |
|
list.splice(index, (count || 0) + 1); |
|
} |
|
}; |
|
|
|
|
|
argfunc.clone = function() { |
|
var cloned = utils.args(); |
|
cloned(list); |
|
return cloned; |
|
}; |
|
|
|
return argfunc; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
makeFilterStrings: function(filters) { |
|
return filters.map(function(filterSpec) { |
|
if (typeof filterSpec === 'string') { |
|
return filterSpec; |
|
} |
|
|
|
var filterString = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(filterSpec.inputs)) { |
|
filterString += filterSpec.inputs.map(function(streamSpec) { |
|
return streamSpec.replace(streamRegexp, '[$1]'); |
|
}).join(''); |
|
} else if (typeof filterSpec.inputs === 'string') { |
|
filterString += filterSpec.inputs.replace(streamRegexp, '[$1]'); |
|
} |
|
|
|
|
|
filterString += filterSpec.filter; |
|
|
|
|
|
if (filterSpec.options) { |
|
if (typeof filterSpec.options === 'string' || typeof filterSpec.options === 'number') { |
|
|
|
filterString += '=' + filterSpec.options; |
|
} else if (Array.isArray(filterSpec.options)) { |
|
|
|
filterString += '=' + filterSpec.options.map(function(option) { |
|
if (typeof option === 'string' && option.match(filterEscapeRegexp)) { |
|
return '\'' + option + '\''; |
|
} else { |
|
return option; |
|
} |
|
}).join(':'); |
|
} else if (Object.keys(filterSpec.options).length) { |
|
|
|
filterString += '=' + Object.keys(filterSpec.options).map(function(option) { |
|
var value = filterSpec.options[option]; |
|
|
|
if (typeof value === 'string' && value.match(filterEscapeRegexp)) { |
|
value = '\'' + value + '\''; |
|
} |
|
|
|
return option + '=' + value; |
|
}).join(':'); |
|
} |
|
} |
|
|
|
|
|
if (Array.isArray(filterSpec.outputs)) { |
|
filterString += filterSpec.outputs.map(function(streamSpec) { |
|
return streamSpec.replace(streamRegexp, '[$1]'); |
|
}).join(''); |
|
} else if (typeof filterSpec.outputs === 'string') { |
|
filterString += filterSpec.outputs.replace(streamRegexp, '[$1]'); |
|
} |
|
|
|
return filterString; |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
which: function(name, callback) { |
|
if (name in whichCache) { |
|
return callback(null, whichCache[name]); |
|
} |
|
|
|
which(name, function(err, result){ |
|
if (err) { |
|
|
|
return callback(null, whichCache[name] = ''); |
|
} |
|
callback(null, whichCache[name] = result); |
|
}); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timemarkToSeconds: function(timemark) { |
|
if (typeof timemark === 'number') { |
|
return timemark; |
|
} |
|
|
|
if (timemark.indexOf(':') === -1 && timemark.indexOf('.') >= 0) { |
|
return Number(timemark); |
|
} |
|
|
|
var parts = timemark.split(':'); |
|
|
|
|
|
var secs = Number(parts.pop()); |
|
|
|
if (parts.length) { |
|
|
|
secs += Number(parts.pop()) * 60; |
|
} |
|
|
|
if (parts.length) { |
|
|
|
secs += Number(parts.pop()) * 3600; |
|
} |
|
|
|
return secs; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extractCodecData: function(command, stderrLine, codecsObject) { |
|
var inputPattern = /Input #[0-9]+, ([^ ]+),/; |
|
var durPattern = /Duration\: ([^,]+)/; |
|
var audioPattern = /Audio\: (.*)/; |
|
var videoPattern = /Video\: (.*)/; |
|
|
|
if (!('inputStack' in codecsObject)) { |
|
codecsObject.inputStack = []; |
|
codecsObject.inputIndex = -1; |
|
codecsObject.inInput = false; |
|
} |
|
|
|
var inputStack = codecsObject.inputStack; |
|
var inputIndex = codecsObject.inputIndex; |
|
var inInput = codecsObject.inInput; |
|
|
|
var format, dur, audio, video; |
|
|
|
if (format = stderrLine.match(inputPattern)) { |
|
inInput = codecsObject.inInput = true; |
|
inputIndex = codecsObject.inputIndex = codecsObject.inputIndex + 1; |
|
|
|
inputStack[inputIndex] = { format: format[1], audio: '', video: '', duration: '' }; |
|
} else if (inInput && (dur = stderrLine.match(durPattern))) { |
|
inputStack[inputIndex].duration = dur[1]; |
|
} else if (inInput && (audio = stderrLine.match(audioPattern))) { |
|
audio = audio[1].split(', '); |
|
inputStack[inputIndex].audio = audio[0]; |
|
inputStack[inputIndex].audio_details = audio; |
|
} else if (inInput && (video = stderrLine.match(videoPattern))) { |
|
video = video[1].split(', '); |
|
inputStack[inputIndex].video = video[0]; |
|
inputStack[inputIndex].video_details = video; |
|
} else if (/Output #\d+/.test(stderrLine)) { |
|
inInput = codecsObject.inInput = false; |
|
} else if (/Stream mapping:|Press (\[q\]|ctrl-c) to stop/.test(stderrLine)) { |
|
command.emit.apply(command, ['codecData'].concat(inputStack)); |
|
return true; |
|
} |
|
|
|
return false; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extractProgress: function(command, stderrLine) { |
|
var progress = parseProgressLine(stderrLine); |
|
|
|
if (progress) { |
|
|
|
var ret = { |
|
frames: parseInt(progress.frame, 10), |
|
currentFps: parseInt(progress.fps, 10), |
|
currentKbps: progress.bitrate ? parseFloat(progress.bitrate.replace('kbits/s', '')) : 0, |
|
targetSize: parseInt(progress.size || progress.Lsize, 10), |
|
timemark: progress.time |
|
}; |
|
|
|
|
|
if (command._ffprobeData && command._ffprobeData.format && command._ffprobeData.format.duration) { |
|
var duration = Number(command._ffprobeData.format.duration); |
|
if (!isNaN(duration)) |
|
ret.percent = (utils.timemarkToSeconds(ret.timemark) / duration) * 100; |
|
} |
|
command.emit('progress', ret); |
|
} |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extractError: function(stderr) { |
|
|
|
return stderr.split(nlRegexp).reduce(function(messages, message) { |
|
if (message.charAt(0) === ' ' || message.charAt(0) === '[') { |
|
return []; |
|
} else { |
|
messages.push(message); |
|
return messages; |
|
} |
|
}, []).join('\n'); |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
linesRing: function(maxLines) { |
|
var cbs = []; |
|
var lines = []; |
|
var current = null; |
|
var closed = false |
|
var max = maxLines - 1; |
|
|
|
function emit(line) { |
|
cbs.forEach(function(cb) { cb(line); }); |
|
} |
|
|
|
return { |
|
callback: function(cb) { |
|
lines.forEach(function(l) { cb(l); }); |
|
cbs.push(cb); |
|
}, |
|
|
|
append: function(str) { |
|
if (closed) return; |
|
if (str instanceof Buffer) str = '' + str; |
|
if (!str || str.length === 0) return; |
|
|
|
var newLines = str.split(nlRegexp); |
|
|
|
if (newLines.length === 1) { |
|
if (current !== null) { |
|
current = current + newLines.shift(); |
|
} else { |
|
current = newLines.shift(); |
|
} |
|
} else { |
|
if (current !== null) { |
|
current = current + newLines.shift(); |
|
emit(current); |
|
lines.push(current); |
|
} |
|
|
|
current = newLines.pop(); |
|
|
|
newLines.forEach(function(l) { |
|
emit(l); |
|
lines.push(l); |
|
}); |
|
|
|
if (max > -1 && lines.length > max) { |
|
lines.splice(0, lines.length - max); |
|
} |
|
} |
|
}, |
|
|
|
get: function() { |
|
if (current !== null) { |
|
return lines.concat([current]).join('\n'); |
|
} else { |
|
return lines.join('\n'); |
|
} |
|
}, |
|
|
|
close: function() { |
|
if (closed) return; |
|
|
|
if (current !== null) { |
|
emit(current); |
|
lines.push(current); |
|
|
|
if (max > -1 && lines.length > max) { |
|
lines.shift(); |
|
} |
|
|
|
current = null; |
|
} |
|
|
|
closed = true; |
|
} |
|
}; |
|
} |
|
}; |
|
|