Skip to content

Instantly share code, notes, and snippets.

@NeverBehave
Created June 9, 2020 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeverBehave/b96de45cb2407bcebe17f78c1b7baa1f to your computer and use it in GitHub Desktop.
Save NeverBehave/b96de45cb2407bcebe17f78c1b7baa1f to your computer and use it in GitHub Desktop.
[srt] merge line with same time code
const subtitle = require("subtitle")
const fs = require("fs")
const path = require("path")
function merge(parsed) {
let i = 0, currentStart = parsed[i].start, currentEnd = parsed[i].end, text = parsed[i].text, pushed = true
const result = []
i++
for (;i < parsed.length; i++) {
if (currentStart !== parsed[i].start && currentEnd !== parsed[i].end) {
result.push({
start: currentStart,
end: currentEnd,
text
})
text = parsed[i].text
currentStart = parsed[i].start
currentEnd = parsed[i].end
pushed = true
} else {
text += '\n' + parsed[i].text
pushed= false
}
}
// The last entry
if (!pushed)
result.push({
start: currentStart,
end: currentEnd,
text
})
return result
}
const infile = process.argv[2]
const outfile = process.argv[3] || 'converted.srt'
if (infile && outfile) {
const workdir = process.cwd()
const input = path.resolve(workdir, infile)
const output = path.resolve(workdir, outfile)
const origin = fs.readFileSync(input,
{encoding:'utf8', flag:'r'});
const parsed = subtitle.parse(origin)
const result = merge(parsed)
const stringify = subtitle.stringify(result)
fs.writeFileSync(output, stringify);
} else {
console.log("Usage: node mergeLine.js [inputFile] [outputFile]")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment