Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fs from 'fs'
- var array = fs.readFileSync('./source.srt').toString().split("\n");
- interface subTitle {
- subNum : string;
- subTime : string;
- subTexts : Array<String>
- }
- let subItems:Array<subTitle> = [];
- let preIndex = 0;
- for (let index=0; index<array.length; index++) {
- let childs: Array<String> = [], passPush=false;
- if (array[index] == "\r") {
- for (let subIndex=preIndex+2; ; subIndex++) {
- if (array[subIndex] == "\r") break;
- childs.push(array[subIndex])
- }
- for (let subIndex=0; subIndex<childs.length; subIndex++) {
- let parsedText=childs[subIndex];
- try {
- const start = childs[subIndex].indexOf('[');
- const end = childs[subIndex].indexOf(']');
- // if (end+2 == childs[subIndex].length) {
- // childs[subIndex] = " ";
- // continue;
- // }
- if (start != end) parsedText = childs[subIndex].substring(0,start) + childs[subIndex].substring((end+2 != childs[subIndex].length) ? end+2 : end+1, childs[subIndex].length);
- childs[subIndex] = parsedText
- }
- catch (err) {
- console.log(`[Line Parsing Error] : ${err}`)
- }
- }
- if (!passPush) subItems.push({subNum : array[preIndex], subTime : array[preIndex+1], subTexts: childs})
- preIndex = index+1;
- }
- }
- fs.writeFile('result.srt', '', (err) => {
- if (err) console.log (`[File Create Error] : ${err}`)
- });
- let output = "", newIndex=1;
- for (let index=0; index<subItems.length; index++) {
- if (subItems[index].subTexts.toString().length<2) continue;
- let subText = "";
- for (let subIndex=0; subIndex<subItems[index].subTexts.length; subIndex++) {
- if (subItems[index].subTexts[subIndex].length < 3) continue;
- subText+=subItems[index].subTexts[subIndex];
- if (subIndex!=subItems[index].subTexts.length-1) subText+="\n"
- }
- output+=String(newIndex++) + "\n" + subItems[index].subTime + "\n" + subText + "\n\n";
- }
- fs.writeFile('result.srt', output, (err) => {
- if (err) console.log (`[File Write Error] : ${err}`)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement