Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Support for gracefully exiting ffmpeg #1001

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