Skip to content

Instantly share code, notes, and snippets.

@lleaff
Created June 23, 2020 14:59
Show Gist options
  • Save lleaff/267c0e4617ff391fefe10bc6afebde0b to your computer and use it in GitHub Desktop.
Save lleaff/267c0e4617ff391fefe10bc6afebde0b to your computer and use it in GitHub Desktop.
Copy styles from one ASS subtitle file to another
#!/usr/bin/env node
// Copy styles from one ASS subtitle file to another by copying every section above [Events]
const fs = require('fs').promises
const sourcePath = process.argv[2]
const targetPath = process.argv[3]
async function main() {
if (!await fs.stat(sourcePath) || !await fs.stat(targetPath)) {
console.error(`Error: File not found "${sourcePath}" or "${targetPath}"`)
return 1
}
const sourceText = (await fs.readFile(sourcePath)).toString()
const targetText = (await fs.readFile(targetPath)).toString()
const [ sourceHeaders, sourceEvents ] = sourceText.split(/^\[Events\] *$/m)
const [ targetHeaders, targetEvents ] = targetText.split(/^\[Events\] *$/m)
if (!sourceHeaders || !sourceEvents || !targetHeaders || !targetEvents) {
console.error(`Files parsed incorrectly, aborting`)
return 1
}
const finalText = sourceHeaders + '[Events]\n' + targetEvents
const originalFilePath = targetPath.replace(/\.([^.]+)$/, '.orig.$1')
await fs.rename(targetPath, originalFilePath)
await fs.writeFile(targetPath, finalText)
console.log(`Copied headers from "${sourcePath}" to "${targetPath}".`)
console.log(`Original file renamed to "${originalFilePath}".`)
return 0
}
main().then(code => process.exit(code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment