Skip to content

Instantly share code, notes, and snippets.

@PatrickKalkman
Created January 27, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrickKalkman/c52f1ecfddadeda2afc101d9cde6708d to your computer and use it in GitHub Desktop.
Save PatrickKalkman/c52f1ecfddadeda2afc101d9cde6708d to your computer and use it in GitHub Desktop.
if (encodingInstructions.videoEncoder === constants.ENCODER_TYPES.X265) {
const ffmpegCommand = ffmpeg()
.input(inputAsset)
.videoBitrate(encodingInstructions.videoBitrate)
.videoCodec(encodingInstructions.videoEncoder)
.size(encodingInstructions.videoSize)
.audioCodec(encodingInstructions.audioEncoder)
.audioBitrate(encodingInstructions.audioBitrate)
.audioFrequency(encodingInstructions.audioFrequency)
.withOutputOptions('-force_key_frames "expr:gte(t,n_forced*2)"')
.outputOption('-x265-params keyint=48:min-keyint=48:scenecut=0:ref=5:bframes=3:b-adapt=2')
.on('progress', (info) => {
const message = {};
message.type = constants.WORKER_MESSAGE_TYPES.PROGRESS;
message.message = `Encoding: ${Math.round(info.percent)}%`;
parentPort.postMessage(message);
})
.on('end', () => {
const message = {};
message.type = constants.WORKER_MESSAGE_TYPES.DONE;
const endTime = Date.now();
message.message = `Encoding finished after ${(endTime - startTime) / 1000} s`;
parentPort.postMessage(message);
})
.on('error', (err, stdout, stderr) => {
const message = {};
message.type = constants.WORKER_MESSAGE_TYPES.ERROR;
message.message = `An error occurred during encoding. ${err.message}`;
parentPort.postMessage(message);
log.error(`Error: ${err.message}`);
log.error(`ffmpeg output: ${stdout}`);
log.error(`ffmpeg stderr: ${stderr}`);
})
.save(outputAsset);
parentPort.on('message', (message) => {
if (message.type === constants.WORKER_MESSAGE_TYPES.STOP_ENCODING) {
// Main thread asks to kill this thread.
log.info('Main thread asked to stop this thread');
ffmpegCommand.kill();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment