Created
September 8, 2022 12:57
-
-
Save mattpocock/8aaaca204fa44ccaad9b06283a495839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Trim latest OBS video | |
import "@johnlindquist/kit"; | |
const { stdout } = await $`ls -t ~/Movies/*.mp4 | head -n 1`; | |
const inputVideo = stdout.trim(); | |
const outputVideo = home("Movies", "trimmed", path.parse(inputVideo).base); | |
const THRESH = "-40"; | |
const DURATION = "1"; | |
const output = | |
await $`source ~/.bash_profile && ffmpeg -hide_banner -vn -i ${inputVideo} -af "silencedetect=n=${THRESH}dB:d=${DURATION}" -f null - 2>&1 | grep "silence_end" | awk '{print $5 " " $8}'`; | |
const silence = output.stdout | |
.trim() | |
.split("\n") | |
.map((line) => line.split(" ")) | |
.map(([silenceEnd, duration]) => { | |
return { | |
silenceEnd: parseFloat(silenceEnd), | |
duration: parseFloat(duration), | |
}; | |
}); | |
const PADDING = 0.2; | |
const startTime = silence[0].duration - PADDING; | |
const endTime = | |
silence[silence.length - 1].silenceEnd - | |
silence[silence.length - 1].duration + | |
PADDING; | |
const formatFloatForFFmpeg = (num: number) => { | |
return num.toFixed(3); | |
}; | |
await ensureDir(home("Movies", "trimmed")); | |
await $`source ~/.bash_profile && ffmpeg -y -hide_banner -ss ${formatFloatForFFmpeg( | |
startTime, | |
)} -to ${formatFloatForFFmpeg( | |
endTime, | |
)} -i ${inputVideo} -c copy ${outputVideo}`; | |
await $`open -R ${outputVideo}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment