Advertisement
Guest User

영어 자막 코멘트 제거 스크립트

a guest
Dec 25th, 2021
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from 'fs'
  2. var array = fs.readFileSync('./source.srt').toString().split("\n");
  3.  
  4. interface subTitle {
  5.     subNum : string;
  6.     subTime : string;
  7.     subTexts : Array<String>
  8. }
  9.  
  10.  
  11. let subItems:Array<subTitle> = [];
  12. let preIndex = 0;
  13. for (let index=0; index<array.length; index++) {
  14.     let childs: Array<String> = [], passPush=false;
  15.    
  16.     if (array[index] == "\r") {
  17.         for (let subIndex=preIndex+2; ; subIndex++) {
  18.             if (array[subIndex] == "\r") break;
  19.             childs.push(array[subIndex])
  20.         }
  21.  
  22.         for (let subIndex=0; subIndex<childs.length; subIndex++) {
  23.             let parsedText=childs[subIndex];
  24.             try {
  25.                 const start = childs[subIndex].indexOf('[');
  26.                 const end = childs[subIndex].indexOf(']');
  27.                
  28.                 // if (end+2 == childs[subIndex].length) {
  29.                 //     childs[subIndex] = " ";
  30.                 //     continue;
  31.                 // }
  32.                 if (start != end) parsedText = childs[subIndex].substring(0,start) + childs[subIndex].substring((end+2 != childs[subIndex].length) ? end+2 : end+1, childs[subIndex].length);
  33.  
  34.                 childs[subIndex] = parsedText
  35.             }
  36.             catch (err) {
  37.                 console.log(`[Line Parsing Error] : ${err}`)
  38.             }
  39.         }
  40.  
  41.         if (!passPush) subItems.push({subNum : array[preIndex], subTime : array[preIndex+1], subTexts: childs})
  42.         preIndex = index+1;
  43.     }
  44. }
  45.  
  46. fs.writeFile('result.srt', '', (err) => {
  47.     if (err) console.log (`[File Create Error] : ${err}`)
  48. });
  49.  
  50. let output = "", newIndex=1;
  51. for (let index=0; index<subItems.length; index++) {
  52.     if (subItems[index].subTexts.toString().length<2) continue;
  53.  
  54.     let subText = "";
  55.     for (let subIndex=0; subIndex<subItems[index].subTexts.length; subIndex++) {
  56.         if (subItems[index].subTexts[subIndex].length < 3) continue;
  57.         subText+=subItems[index].subTexts[subIndex];
  58.         if (subIndex!=subItems[index].subTexts.length-1) subText+="\n"
  59.     }
  60.  
  61.     output+=String(newIndex++) + "\n" + subItems[index].subTime + "\n" + subText + "\n\n";
  62. }
  63.  
  64. fs.writeFile('result.srt', output, (err) => {
  65.     if (err) console.log (`[File Write Error] : ${err}`)
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement