#!/usr/bin/env node

/*jshint node:true */

"use strict";
var program = require('commander');
var fs = require("fs");
var Promise = require('promise');
var readFile = Promise.denodeify(fs.readFile);
var writeFile = Promise.denodeify(fs.writeFile);

var DEFAULT_DELAY = 1000;
process.title = "subber";
program._name = "subber";

program
  .version('1.0')
  .usage("[options] <file ...>")
  .option('-f, --forward [name]', 'forward delay value')
  .option('-r, --rewind [name]', 'rewind delay value')
  .option('-o, --output [name]', 'the output filename')
  .parse(process.argv);

var hasAction = program.forward || program.rewind;

if (!program.input || program.input === true || !hasAction) {
  program.help();
  return;
}

var outputFile = program.output || program.input.split(".srt").join('2.srt');
var delay = parseInt(program.forward || program.rewind || DEFAULT_DELAY, 10);

if (program.rewind) {
  delay *= -1;
}




function ts2ms(ts){
  return ( parseInt(ts[0], 10) * 3600 +
    parseInt(ts[1], 10) * 60   +
    parseInt(ts[2], 10)) * 1000 +
    parseInt(ts[3], 10);
}
function format(val){
  return val > 9 ? ""+val : "0"+val;
}

function ms2ts(ms){
  var scale = [3600*1000, 60*1000, 1000, 1 ];
  var ts = scale.map( function(val){
    var unit = parseInt(ms / val, 10);
    ms = ms % val;
    return unit;
  }).map(format);
  return ts.slice(0,3).join(":") + "," + ts[ts.length-1];
}

function parse (srt) {
  // srt  = srt.substr(0, 300);
  return srt.replace(/(\d{2}):(\d{2}):(\d{2}),(\d{3})/g, function(){
     var ms = ts2ms( [].slice.call(arguments, 1 , 5));
     ms += delay;
     return ms2ts(ms);
  });
}
program.args.forEach(function(inputFile){
  readFile(inputFile, {encoding: "utf8"})
    .then( parse )
    .then( function( newSrt ) {
      return writeFile(outputFile, newSrt );
    })
    .then(function(){
      console.log(inputFile, '... done');
    });
});
@@ -406,20 +406,21 @@
 orward %5B
-name
+delay
 %5D', 'for
@@ -427,32 +427,38 @@
 ward delay value
+ in ms
 ')%0A  .option('-r
@@ -468,21 +468,23 @@
 -rewind 
-%5Bname
+ %5Bdelay
 %5D', 'rew
@@ -498,16 +498,22 @@
 ay value
+ in ms
 ')%0A  .op
@@ -531,16 +531,17 @@
 -output 
+ 
 %5Bname%5D',
@@ -593,24 +593,13 @@
 );%0A%0A
-var hasAction = 
+if (!
 prog
@@ -602,35 +602,36 @@
 program.forward 
-%7C%7C 
+&& !
 program.rewind;%0A
@@ -632,69 +632,9 @@
 wind
-;%0A%0Aif (!program.input %7C%7C program.input === true %7C%7C !hasAction
+ 
 ) %7B%0A
@@ -863,16 +863,156 @@
 1;%0A%7D%0A%0A%0A%0A
+/**%0A * Transform a strt timestamp to ms%0A * @param  %7BString%7D   ts a srt timestamp hh:mm:ss,ms%0A * @return %7Bint%7D      number of millisecond%0A */
 %0Afunctio
@@ -1162,69 +1162,356 @@
 ;%0A%7D%0A
-function format(val)%7B%0A  return val %3E 9 ? %22%22+val : %220%22+val;%0A%7D%0A
+%0A/**%0A * Format a number on 2 digit%0A * @param  %7Bint%7D      val  a number to format%0A * @return %7BString%7D   the formated number%0A */%0Afunction format(val)%7B%0A  return val %3E 9 ? %22%22+val : %220%22+val;%0A%7D%0A%0A/**%0A * Transform a number of millisecond into a srt timestamp%0A * @param  %7Bint%7D      number of millisecond%0A * @return %7BString%7D   a srt timestamp hh:mm:ss,ms%0A */
 %0Afun
@@ -1768,62 +1768,214 @@
 %0A%7D%0A%0A
-function parse (srt) %7B%0A  // srt  = srt.substr(0, 300);
+/**%0A * Resync a str file with a delay%0A * @param  %7Bint%7D    delay a delay in ms%0A * @param  %7BString%7D srt   a str file content%0A * @return %7BString%7D       the resync srt content%0A */%0Afunction resync (delay, srt) %7B
 %0A  r
@@ -2144,16 +2144,17 @@
   %7D);%0A%7D%0A
+%0A
 program.
@@ -2240,21 +2240,34 @@
  .then( 
-parse
+resync.bind(delay)
  )%0A    .

Add comments