Skip to content

Instantly share code, notes, and snippets.

@tayiorbeii
Created October 18, 2022 04:45
Show Gist options
  • Select an option

  • Save tayiorbeii/1a49b0d9b87e6024390f846972d5f14c to your computer and use it in GitHub Desktop.

Select an option

Save tayiorbeii/1a49b0d9b87e6024390f846972d5f14c to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
const leadingzero = await npm("leadingzero")
const replaceAll = await npm("just-replace-all")
// Name: split-video-on-scene
let longVideoFile = await getSelectedFile()
cd(path.dirname(longVideoFile))
// requires the scenedetect Python library
// pip install --upgrade opencv
// pip install --upgrade scenedetect[opencv]
let {stdout} = await exec(`scenedetect --input "${longVideoFile}" --frame-skip 2 --min-scene-len 00:03:00 --merge-last-scene detect-content -t 40 list-scenes`)
// parse detected scenes, adding 0s for the start
let timecodelist = ['00:00:00', ...stdout.split('\n')[stdout.split('\n').length-1].trim().split(',')]
let originalFilename = longVideoFile.split('/')[longVideoFile.split('/').length - 1]
let cutsToMake = timecodelist.map((time, index) => {
if (time[index + 1] == undefined) {
return {
from: time,
desc: `${originalFilename}-part${leadingzero(index+1, 2)}`,
}
}
return {
from: time,
to: timecodelist[index+1],
desc: `${originalFilename}-part${leadingzero(index+1, 2)}`
}
})
for (let cut of cutsToMake) {
let clipFileName = `${replaceAll(cut.desc, ' ', '_')}.mp4`
await exec(`ffmpeg -i "${longVideoFile}" -c copy -ss ${cut.from} ${cut.to ? `-to ${cut.to}`: ''} ${clipFileName}`)
// also split the SRT file
await exec(`ffmpeg -i "${longVideoFile}.srt" -c copy -ss ${cut.from} ${cut.to ? `-to ${cut.to}`: ''} ${clipFileName}.srt`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment