Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Forked from tayiorbeii/cut-video-and-srt.js
Created December 21, 2022 21:50
Show Gist options
  • Save johnlindquist/8ece568cddc543f87a3c998206dbebce to your computer and use it in GitHub Desktop.
Save johnlindquist/8ece568cddc543f87a3c998206dbebce to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
const replaceAll = await npm('just-replace-all')
const leadingZeros = await npm('leading-zeros')
// Name:
/*
* Highlight the long video we are taking clips out of in Finder
*/
let longVideoFile = await getSelectedFile()
await(hide)
/**
* Tell kit we want to be working in the same directory as the source file
*/
cd(path.dirname(longVideoFile))
let originalFileName = path.parse(longVideoFile).name
// try to read a .timestamps file
let timestampsFile = await readFile(`${originalFileName}.timestamps`, 'utf8')
let cutsToMake = []
// if there's a .timestamps file, use it
// otherwise manually edit the `cutsToMake` array
if (timestampsFile.length > 0) {
let splitTimestampsFile = timestampsFile.split('\n').map((line, index) => {
let [key, value] = line.split(';')
return { [key]: value }
})
cutsToMake = splitTimestampsFile.reduce((acc, item, index) => {
if (item.from) {
acc.push({
from: item.from,
to: splitTimestampsFile[index + 1].to,
desc: `${originalFileName}-segment${leadingZeros(parseInt(splitTimestampsFile[index + 2].part), 1)}`
})
}
return acc
}, [])
} else {
// Remember to increment the desc number!
cutsToMake = [
{
"from": "00:00:37.517",
"to": "01:15:18.516",
"desc": `${originalFileName}-segment01`
},
// {
// "from": "00:25:25.000",
// "to": "00:51:07",
// "desc": `${originalFileName}-segment02`
// },
// {
// "from": "00:54:21.517",
// "to": "00:58:47.400",
// "desc": `${originalFileName}-segment03`
// },
]
}
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`)
}
// split srt also
for (let cut of cutsToMake) {
let clipFileName = `${replaceAll(cut.desc, ' ', '_')}`
try {
await exec(`ffmpeg -i "${originalFileName}.srt" -c copy -ss ${cut.from} ${cut.to ? `-to ${cut.to}`: ''} ${clipFileName}.srt`)
} catch (err) {
console.log(`No SRT found for ${clipFileName}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment