Created
October 18, 2022 19:02
-
-
Save tayiorbeii/b8c4a2e089c2be954ea16e96a57304d4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import "@johnlindquist/kit" | |
| const leadingzero = await npm("leadingzero") | |
| const replaceAll = await npm("just-replace-all") | |
| // Name: split-video-on-scene | |
| // NOTE: SRT file should not have .mp4 in its name! | |
| // video.mp4 and video.srt Not video.mp4.srt! | |
| let longVideoFile = await getSelectedFile() | |
| await(hide) | |
| 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, ' ', '_')}` | |
| await exec(`ffmpeg -i "${longVideoFile}" -c copy -ss ${cut.from} ${cut.to ? `-to ${cut.to}`: ''} ${clipFileName}.mp4`) | |
| // also split the SRT file | |
| await exec(`ffmpeg -i "${replaceAll(originalFilename, '.mp4', '.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