Created
November 30, 2015 15:35
-
-
Save vankasteelj/4b2847bd82ffaab464c9 to your computer and use it in GitHub Desktop.
merge 2 srt files, using 1's timecodes and 2's text lines. nodejs.
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
/* merge2subs | |
* | |
* Resync subtitle with another sub's timecodes | |
* | |
* @file_tc is the file you extract timecodes from; | |
* @file_lines is the one you extract text from; | |
* | |
* both arguments are absolute path to SRT files | |
* | |
* @xpath is an absolute path to a directory, to save mixed.srt | |
*/ | |
var merge2subs = function(file_tc, file_lines, xpath) { | |
var readline = require('readline'), | |
srt = 'mixed.srt', | |
srtPath = xpath; | |
fs.writeFileSync(path.join(srtPath, srt), ''); //create or delete content; | |
var tc_sub = []; | |
var count = null; | |
var rl = readline.createInterface({ | |
input: fs.createReadStream(file_tc), | |
output: process.stdout, | |
terminal: false | |
}); | |
rl.on('line', function(line) { | |
if (!isNaN(line)) { | |
// sub count | |
count = parseInt(line); | |
tc_sub[count] = {text: []}; | |
} else { | |
// timecode or text | |
if (line.match(/\d{2}\:\d{2}\:\d{2}\,\d{3}/)) { | |
tc_sub[count].tc = line; | |
} else { | |
tc_sub[count].text.push(line); | |
} | |
} | |
}); | |
var lines_sub = []; | |
var counter = null; | |
var rle = readline.createInterface({ | |
input: fs.createReadStream(file_lines), | |
output: process.stdout, | |
terminal: false | |
}); | |
rle.on('line', function(line) { | |
if (!isNaN(line)) { | |
// sub count | |
counter = parseInt(line); | |
lines_sub[counter] = {text: []}; | |
} else { | |
// timecode or text | |
if (line.match(/\d{2}\:\d{2}\:\d{2}\,\d{3}/)) { | |
lines_sub[counter].tc = line; | |
} else { | |
lines_sub[counter].text.push(line); | |
} | |
} | |
}); | |
setTimeout(function() { | |
for (var i = 1; i < tc_sub.length; i++) { | |
//SRT formatting | |
var parsedLine = '' + i + '\n' + | |
tc_sub[i].tc + '\n' + | |
lines_sub[i].text.join('\n'); | |
fs.appendFileSync(path.join(srtPath, srt), '\n\n' + parsedLine, 'utf-8'); | |
} | |
}, 2000); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment