Skip to content

Commit

Permalink
Fix progress reported as Infinity% when ffmpeg line output is spread …
Browse files Browse the repository at this point in the history
…across several chunks
  • Loading branch information
rprieto committed Dec 16, 2017
1 parent 3a38b4b commit ddb9baa
Show file tree
Hide file tree
Showing 4 changed files with 2,144 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/ffmpeg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const childProcess = require('child_process')
const trace = require('debug')('thumbsup:trace')
const debug = require('debug')('thumbsup:debug')
const EventEmitter = require('events')
const split = require('split')

class ProgressEmitter extends EventEmitter {}

// Duration: 00:01:00.10
Expand All @@ -11,20 +15,23 @@ const REGEX_PROGRESS = /time=(\d\d):(\d\d):(\d\d).\d\d/
exports.exec = function (args, callback) {
const progressEmitter = new ProgressEmitter()
var durationSeconds = 0
const child = childProcess.spawn('ffmpeg', args, {
})
child.stderr.on('data', (data) => {
const child = childProcess.spawn('ffmpeg', args, {})
child.stderr.pipe(split()).on('data', (data) => {
if (!durationSeconds) {
durationSeconds = parseTime(data.toString(), REGEX_DURATION)
durationSeconds = parseTime(data, REGEX_DURATION)
}
const progressSeconds = parseTime(data.toString(), REGEX_PROGRESS)
const progressSeconds = parseTime(data, REGEX_PROGRESS)
if (progressSeconds) {
const percent = Math.floor(progressSeconds * 100 / durationSeconds)
progressEmitter.emit('progress', percent)
}
})
child.on('error', err => callback(err))
child.on('error', err => {
debug('ffmpeg error:', err)
callback(err)
})
child.on('exit', (code, signal) => {
trace(`ffmpeg exited with code ${code}`)
if (code > 0) callback(new Error(`ffmpeg exited with code ${code}`))
else callback(null)
})
Expand Down
Loading

0 comments on commit ddb9baa

Please sign in to comment.