Loading

fixsrt.js

  1. // tslint:disable-next-line:max-line-length
  2. const timestampRegex = /^(?:(\d{2,})(:))?([0-5][0-9])(:)([0-5][0-9])([,.])(\d{3})( --> )(?:(\d{2,})(:))?([0-5][0-9])(:)([0-5][0-9])([,.])(\d{3})/;
  3.  
  4. function fixSrt(input) {
  5.     const trim = (str) => str.trim()
  6.       .replace(/^[^\S\n]+/gm, '')
  7.       .replace(/\u0000/g, '\uFFFD')
  8.       .replace(/\r\n/g, '\n')
  9.       .replace(/\r/g, '\n')
  10.       .replace(/\n{3,}/g, '\n\n');
  11.     const origin = trim(input)
  12.       .split('\n');
  13.     let id = 0;
  14.     let idCache = '';
  15.     let state = 'root';
  16.     const processLine = line => {
  17.       switch (state) {
  18.         case 'note':
  19.           /* falls through */
  20.         case 'title':
  21.           if (line === '') {
  22.             state = 'root';
  23.           }
  24.           return [];
  25.         case 'root':
  26.           if (line.match(/^WEBVTT/)) {
  27.             state = 'title';
  28.             return [];
  29.           } else if (line.match(/^NOTE/)) {
  30.             state = 'note';
  31.             return [];
  32.           } else if (line === '') return [line];
  33.           else if (!line.match(/-->/)) {
  34.             state = 'timeline';
  35.             idCache = line;
  36.             return [];
  37.           }
  38.           /* falls through */
  39.         case 'timeline':
  40.           const match = timestampRegex.exec(line);
  41.           if (match) {
  42.             idCache = '';
  43.             id++;
  44.             state = 'text';
  45.             return [
  46.               id + '',
  47.               // tslint:disable-next-line:max-line-length
  48.               `${match[1] || '00'}:${match[3]}:${match[5]},${match[7]} --> ${match[9] || '00'}:${match[11]}:${match[13]},${match[15]}`
  49.             ];
  50.           } else {
  51.             // 坏的字幕块
  52.             state = 'root';
  53.             // 原样输出
  54.             const lastLine = idCache;
  55.             idCache = '';
  56.             return lastLine ? [lastLine, line] : [line];
  57.             // 忽略这个字幕块
  58.             // return [];
  59.           }
  60.         case 'text':
  61.           if (line === '') {
  62.             state = 'root';
  63.           }
  64.           return [line];
  65.       }
  66.     };
  67.     const result = [];
  68.     origin.forEach(line => result.push(...processLine(line)));
  69.     // 没写完的字幕块
  70.     // 原样输出
  71.     if (idCache) result.push(idCache);
  72.     return trim(result.join('\n')) + '\n';
  73.   }

Comments

Hello there,

My name is Aly and I would like to know if you would have any interest to have your website here at qwqq.pw promoted as a resource on our blog alychidesign.com ?

We are in the midst of updating our broken link resources to include current and up to date resources for our readers. Our resource links are manually approved allowing us to mark a link as a do-follow link as well
.
If you may be interested please in being included as a resource on our blog, please let me know.

Thanks,
Aly

Anonymous • 17 Jan 2019, 03:37:28 UTC