Skip to content

Support for gracefully exiting ffmpeg #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ module.exports = function(proto) {
niceness: self.options.niceness,
cwd: self.options.cwd,
windowsHide: true
},
},

function processCB(ffmpegProc, stdoutRing, stderrRing) {
self.ffmpegProc = ffmpegProc;
Expand Down Expand Up @@ -659,4 +659,23 @@ module.exports = function(proto) {

return this;
};

/**
* quit current ffmpeg process, if any
*
* @method FfmpegCommand#quit
* @category Processing
*
* @return FfmpegCommand
*/
proto.quit = function(signal) {
if (!this.ffmpegProc) {
this.logger.warn('No running ffmpeg process, cannot quit');
} else {
this.ffmpegProc.stdin.setEncoding('utf-8');
this.ffmpegProc.stdin.write("q\n");
}

return this;
};
};
33 changes: 33 additions & 0 deletions test/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,39 @@ describe('Processor', function() {
});
});


it('should gracefully quit the process with .quit', function(done) {
var testFile = path.join(__dirname, 'assets', 'testProcessQuit.avi');
this.files.push(testFile);

var ffmpegJob = this.getCommand({ source: this.testfilebig, logger: testhelper.logger })
.usingPreset('divx');

var startCalled = false;
var errorCalled = false;
var endCalled = false;

ffmpegJob
.on('start', function() {
startCalled = true;
setTimeout(function() { ffmpegJob.quit(); }, 500);
ffmpegJob.ffmpegProc.on('exit', function() {
setTimeout(function() {
errorCalled.should.equal(false);
endCalled.should.equal(true);
done();
}, 1000);
});
})
.on('error', function(err) {
errorCalled = true;
})
.on('end', function() {
endCalled = true;
})
.saveToFile(testFile);
});

describe('Events', function() {
it('should report codec data through \'codecData\' event', function(done) {
this.timeout(60000);
Expand Down