Skip to content

Instantly share code, notes, and snippets.

@carlozamagni
Created January 23, 2022 13:54
Show Gist options
  • Select an option

  • Save carlozamagni/7311f8f182a70e716d3c32db3f3d6bf0 to your computer and use it in GitHub Desktop.

Select an option

Save carlozamagni/7311f8f182a70e716d3c32db3f3d6bf0 to your computer and use it in GitHub Desktop.
Fix SRT file translated by Google Translate API batch
'use strict'
const fs = require("fs")
const buffer = fs.readFileSync("./source.txt")
const fullText = buffer.toString()
// const REGEX_MATCHES_BAD_MARKER = /(?<!\n{2})^[0-9]+\n/
const REGEX_MATCHES_NEWLINE_WHEN_ONE_MORE_IS_NEEDED = /(?<!\n)\n(?=^[0-9]+\n)/gm
const fixed = fullText.replace(REGEX_MATCHES_NEWLINE_WHEN_ONE_MORE_IS_NEEDED, '\n\n')
fs.writeFileSync("./dest.srt", fixed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment