Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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