Created
June 9, 2020 17:32
-
-
Save NeverBehave/b96de45cb2407bcebe17f78c1b7baa1f to your computer and use it in GitHub Desktop.
[srt] merge line with same time code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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