Skip to content

Instantly share code, notes, and snippets.

@boutell
Created December 20, 2020 05:41
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 boutell/269745623772730b76353c6057bf0a79 to your computer and use it in GitHub Desktop.
Save boutell/269745623772730b76353c6057bf0a79 to your computer and use it in GitHub Desktop.
Crop and overlay videos using ffmpeg, to copy and paste in space rather than time
const exec = require('child_process').execSync;
// I got coordinates using the screenshot picker in quicktime player,
// but those are scaled to my screen, not the real video. Do some
// corrective fakery
const realDimensions = [ 1920, 1080 ];
const fakeWidth = 1680;
const fakeTom = [ 1400, 0, 280, 156 ];
const fakeVideo = [ 0, 100, 1470, 760 ];
const realTom = scale(fakeTom);
const realVideo = scale(fakeVideo);
console.log(realTom);
console.log(realVideo);
const name = 'whats-new-in-modern-javascript-20201205';
exec(`ffmpeg -i ${name}.mp4 -vf "crop=${realTom[2]}:${realTom[3]}:${realTom[0]}:${realTom[1]}" -c:v libx264 -c:a copy ${name}-tom.mp4 -y`);
exec(`ffmpeg -i ${name}.mp4 -vf "crop=${realVideo[2]}:${realVideo[3]}:${realVideo[0]}:${realVideo[1]}" -c:v libx264 -c:a copy ${name}-video.mp4 -y`);
exec(`ffmpeg -i ${name}-video.mp4 -vf "movie=${name}-tom.mp4[clip];[0][clip]overlay=main_w-overlay_w:0" ${name}-merged.mp4 -y`);
function scale(args) {
return args.map(n => Math.floor(n * realDimensions[0] / fakeWidth));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment