Skip to content
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

Support for gracefully exiting ffmpeg #1001

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
add FfmpegCommand.quit() to gracefully exit
jbcpollak committed Apr 22, 2020
commit 1b51621b49e158773e9c8705e889c2b1c04f07d2
21 changes: 20 additions & 1 deletion lib/processor.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
};
};
32 changes: 32 additions & 0 deletions test/processor.test.js
Original file line number Diff line number Diff line change
@@ -365,6 +365,38 @@ 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;

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

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