SHARE
TWEET

Sync Subs

giovani-rubim Dec 14th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const lines = fs.readFileSync('subtitle.srt').toString('utf8').split('\n');
  3. const array = [];
  4. const buffer = [];
  5. const parseTime = str => {
  6.     let [h, m, s] = str.trim().split(':');
  7.     return h*36e5 + m*6e4 + parseFloat(s.replace(',', '.'))*1e3;
  8. };
  9. const z = {};
  10. for (let i=-2; i<=4; ++i) {
  11.     z[i] = '0'.repeat(Math.max(0, i));
  12. }
  13. const dumpTime = x => {
  14.     x = Math.max(Math.round(x), 0);
  15.     let s = x%6e4;
  16.     x = Math.round((x - s)/6e4);
  17.     let m = x%60;
  18.     x = Math.round((x - m)/60);
  19.     let h = x%60;
  20.     h = h.toString();
  21.     m = m.toString();
  22.     s = s.toString();
  23.     s = z[5 - s.length] + s;
  24.     let str = z[2-h.length] + h;
  25.     str += ':' + z[2-m.length] + m;
  26.     str += ':' + s.substr(0, 2);
  27.     str += ',' + s.substr(2);
  28.     return str;
  29. };
  30. const flush = () => {
  31.     let index = 0;
  32.     while (buffer[index].indexOf('-->') === -1 && index < buffer.length) {
  33.         ++ index;
  34.     }
  35.     if (index === buffer.length) {
  36.         buffer.length = 0;
  37.         return;
  38.     }
  39.     let [t0, t1] = buffer[index].split('-->');
  40.     array.push({
  41.         a: parseTime(t0),
  42.         b: parseTime(t1),
  43.         lines: buffer.slice(index+1)
  44.     });
  45.     buffer.length = 0;
  46. };
  47. let last = '';
  48. lines.forEach(line => {
  49.     line = line.trim();
  50.     if (line) {
  51.         buffer.push(line);
  52.     } else if (buffer.length) {
  53.         flush();
  54.     }
  55. });
  56.  
  57. const t1a = parseTime('00:19:02,391');
  58. const t1b = parseTime('00:19:03,200');
  59. const t2a = parseTime('01:50:12,031');
  60. const t2b = parseTime('01:51:57,5   00');
  61. const val_a = (t2b - t1b)/(t2a - t1a);
  62. const val_b = t1b - t1a*val_a;
  63.  
  64. // const val_a = 1;
  65. // const val_b = 1e3;
  66. console.log({t1a, t1b, t2a, t2b});
  67. console.log({val_a, val_b});
  68.  
  69. let output = '';
  70.  
  71. array.forEach((sub, i) => {
  72.     const {a, b, lines} = sub;
  73.     output += (i+1) + '\n';
  74.     output += dumpTime(sub.a*val_a + val_b) + ' --> ';
  75.     output += dumpTime(sub.b*val_a + val_b) + '\n';
  76.     output += lines.join('\n') + '\n\n';
  77. });
  78. fs.writeFileSync('output.srt', output);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top