Subtitle Adjuster

raspi 28.07.04 03:34

Tällä voi lisätä sekunteja SRT-tiedostoon tai sitten frameja MicroDVD-tiedostoon.

 Tekstiversio  Arvo: 2 (2 ääntä)  Äänestä: +  -
/*
  Subtitle Adjuster 0.03
  Pekka Järvinen 2004-2005

  Tällä voi lisätä sekunteja SRT-tiedostoon tai sitten frameja MicroDVD-tiedostoon

  supported:
  srt      01:02:03,000 --> 01:02:04,000
  microdvd {123}{234}foo|bar

  compile:
  mcs subadj.cs

  run:
  mono subadj.exe

  todo:
  - parempi virheidenkäsittely

  historia:
  0.03: Lisätty sekuntien miinustaminen SRT-muotoon (ugly hack)
  0.02: bugeja korjattu ja microdvd-muodon frameja voi nyt miinustaa
  0.01: ensimmäinen versio
*/


using System;
using System.IO;
using System.Text.RegularExpressions;

class SubtitleAdjuster {
  static int Main(string[] args) {
    if (args.Length != 2) {
      Console.WriteLine("Usagezz: subadj.exe [-]<seconds|frames> <filename> > newfile");
      Console.WriteLine("Example: subadj.exe 120 foobar.srt > foobar_fixed.srt");
      Console.WriteLine("Example: subadj.exe -123 xyzzy.sub > xyzzy_fixed.sub");
      return 1;
    }

    bool UseMinus = false;
    Regex SecMod = new Regex(@"^([\-])?(\d+)$", RegexOptions.IgnoreCase);
    Match SecMatch = SecMod.Match(args[0]);

    if (SecMatch.Success) {
      GroupCollection gc = SecMatch.Groups;
      long SecondsMod = long.Parse(gc[2].Value);
      if(gc[1].Value == "-") UseMinus = true;
      string filename = args[1];

      if(File.Exists(filename)) {
        HandleSubFile(filename, SecondsMod, UseMinus);
      } else {
        Console.WriteLine("Error: file not found: '{0}'", filename);
      }

    } else {
      Console.WriteLine("Error: Check parameters");
    }

    return 1;
  } // /func

  static void HandleSubFile (string filename, long add, bool UseMinus) {
    StreamReader SR = new StreamReader(filename, System.Text.Encoding.Default);
    string S;

    S = SR.ReadLine(); // luetaan rivi

    // SRT:
    Regex srtre = new Regex(@"^(\d+):(\d+):(\d+),(\d+) --> (\d+):(\d+):(\d+),(\d+)$", RegexOptions.IgnoreCase);
    // MicroDVD: {0}{1}foo
    Regex mdvdre = new Regex(@"^\{(\d+)\}\{(\d+)\}(.*)$", RegexOptions.IgnoreCase);

    while (S != null) {
      Match srtm = srtre.Match(S);
      Match mdvdm = mdvdre.Match(S);

      // SRT-tyylinen timestamppi löytyi
      if (srtm.Success) {
        GroupCollection gc = srtm.Groups;

        int    h1 = Convert.ToInt32(gc[1].Value); // tunnit
        int    m1 = Convert.ToInt32(gc[2].Value); // minuutit
        int    s1 = Convert.ToInt32(gc[3].Value); // sekunnit
        double n1 = Convert.ToInt32(gc[4].Value); // millisekunnit

        int    h2 = Convert.ToInt32(gc[5].Value); // tunnit
        int    m2 = Convert.ToInt32(gc[6].Value); // minuutit
        int    s2 = Convert.ToInt32(gc[7].Value); // sekunnit
        double n2 = Convert.ToInt32(gc[8].Value); // millisekunnit

        if(!UseMinus) {
          // Muotoillaan SRT-tyyliseksi ja lisätään sekunteja
          S = String.Format("{0},{1:000} --> {2},{3:000}", AddSecsSRT(h1, m1, s1, add), n1, AddSecsSRT(h2, m2, s2, add), n2);
        } else {
          // Muotoillaan SRT-tyyliseksi ja poistetaan sekunteja
          S = String.Format("{0},{1:000} --> {2},{3:000}", RemSecsSRT(h1, m1, s1, add), n1, RemSecsSRT(h2, m2, s2, add), n2);
        } // /else

      } // /if

      // MicroDVD-tyylinen framestamppi löytyi
      if (mdvdm.Success) {
        GroupCollection gc = mdvdm.Groups;

        double sf = Convert.ToInt32(gc[1].Value); // start frame
        double ef = Convert.ToInt32(gc[2].Value); // end frame

        // Muotoillaan MicroDVD-muotoon
        if(!UseMinus) {
          S = String.Format("{{{0}}}{{{1}}}{2}", (sf + add), (ef + add), gc[3].Value);
        } else {
          S = String.Format("{{{0}}}{{{1}}}{2}", (sf - add), (ef - add), gc[3].Value);
        } // /else
      } // /if

      Console.WriteLine(S); // tulostetaan

      S = SR.ReadLine(); // luetaan rivi
    } // /while

    SR.Close(); // suljetaan streami

  } // /func

  static string AddSecsSRT (int h, int m, int s, long add) {
    System.DateTime aika = new System.DateTime(1970, 1, 1, h, m, s, 0).AddSeconds(add);
    return String.Format("{0:00}:{1:00}:{2:00}", aika.Hour, aika.Minute, aika.Second);
  } // /func

  static string RemSecsSRT (int h, int m, int s, long del) {
        del = 0 - del;
        System.DateTime aika = new System.DateTime(1970, 1, 1, h, m, s, 0).AddSeconds(del);
    return String.Format("{0:00}:{1:00}:{2:00}", aika.Hour, aika.Minute, aika.Second);
  } // /func

} // /class
 

raspi 03:35 28.7.04 
Kuha ei sitten halunnut hyväksyä sulkuja ja #-merkkiä nimeen.