Skip to content

Instantly share code, notes, and snippets.

@TomGalla11
Created February 6, 2021 12:49
Show Gist options
  • Save TomGalla11/c48f94398ef3dcbaa6ffe9998f02bea4 to your computer and use it in GitHub Desktop.
Save TomGalla11/c48f94398ef3dcbaa6ffe9998f02bea4 to your computer and use it in GitHub Desktop.
'use strict';
const { spawn } = require('child_process');
const util = require('util');
const fs = require('fs-extra');
// eslint-disable-next-line import/order
const exec = util.promisify(require('child_process').exec);
const logOutput = name => data => console.log(`[${name}] ${data}`);
function run(videoData) {
return new Promise((resolve, reject) => {
const process = spawn('python', ['handlers/blur_video.py', JSON.stringify(videoData)]);
const out = [];
process.stdout.on('data', data => {
out.push(data.toString());
logOutput('stdout')(data);
});
const err = [];
process.stderr.on('data', data => {
err.push(data.toString());
logOutput('stderr')(data);
});
process.on('exit', (code, signal) => {
logOutput('exit')(`${code} (${signal})`);
if (code === 0) {
resolve(out);
} else {
reject(new Error(err.join('\n')));
}
});
});
}
module.exports = async (input, videoData) => {
console.log('Initializing temporary files');
if (!fs.existsSync('./temp')) await fs.mkdir('./temp');
if (fs.existsSync(`./temp/${videoData.id}`)) await fs.remove(`./temp/${videoData.id}`);
await fs.mkdir(`./temp/${videoData.id}`);
await fs.mkdir(`./temp/${videoData.id}/raw-frames`);
await fs.mkdir(`./temp/${videoData.id}/edited-frames`);
console.log('Decoding');
await exec(`ffmpeg -i ${input} -vf fps=30 temp/${videoData.id}/raw-frames/%d.png`, { shell: true });
try {
console.log('Rendering');
const output = await run(videoData);
logOutput('main')(output);
console.log('Encoding');
await exec(
// eslint-disable-next-line max-len
`ffmpeg -f image2 -framerate 30 -i temp/${videoData.id}/edited-frames/%d.png -c:v libx264 -vf fps=30 -pix_fmt yuv420p temp/${videoData.id}/no-audio.mp4`,
{ shell: true },
);
console.log('Adding audio');
await exec(
`ffmpeg -i temp/${videoData.id}/no-audio.mp4 -i ${input} -c copy -map 0:v:0 -map 1:a:0 output/${Date.now()}.mp4`,
{ shell: true },
);
console.log('Cleaning up');
await fs.remove(`temp/${videoData.id}`);
} catch (e) {
console.error('Error during script execution ', e.stack);
process.exit(1);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment