dziurkacz-ffmpeg/dziurkacz.mjs
2021-09-12 19:38:48 +02:00

63 lines
1.5 KiB
JavaScript

import { extname } from "path";
const args = process.argv.slice(-2);
const video = args[0];
const labels = args[1];
const ext = extname(video);
const labels_contents = (await $`awk '{print $3, $4, $5}' < ${labels}`).stdout;
const FREQ = 48000;
const working_dir = "/tmp/dziurkacz";
const before_path = `${working_dir}/before${ext}`;
const after_path = `${working_dir}/after${ext}`;
const working_path = `${working_dir}/working${ext}`;
const vdlist = `${working_dir}/vdlist.txt`;
async function makeCut({ duration, start, end }) {
await Promise.all([
$`ffmpeg -i ${working_path} -to ${start} -c copy -an -y ${before_path}`,
$`ffmpeg -ss ${end} -i ${working_path} -c copy -an -y ${after_path}`,
]);
await Promise.all([
$`echo file ${before_path} > ${vdlist}`,
$`echo file ${after_path} >> ${vdlist}`,
]);
await $`ffmpeg -f concat -safe 0 -i ${vdlist} -c copy -y ${working_path} `;
}
await $`mkdir -p ${working_dir}`;
await $`cp -f ${video} ${working_path}`;
const cuts = labels_contents
.split(os.EOL)
.slice(0, -1)
.map((e) =>
e
.split(" ")
.slice(1)
.map((x) => parseFloat(x))
)
.map(([samples, start]) => ({
duration: samples / FREQ,
start,
end: start + samples / FREQ,
}));
// await makeCut(cuts[0]);
// await makeCut(cuts[1]);
for (const cut of cuts) {
await makeCut(cut);
}
console.log(
`Removed ${cuts
.map((e) => e.duration)
.reduce((a, b) => a + b, 0)
.toFixed(3)}s from video`
);