zaterdag 5 oktober 2013

Parsing subtitle srt files and add seconds Java

Changing the time of subtitle file .srt. the structure for the srt file is:
1    A numeric counter identifying each sequential subtitle
2    The time that the subtitle should appear on the screen, followed by " --> " and the time it should disappear
3    Subtitle text itself on one or more lines
4    A blank line containing no text indicating the end of this subtitle[10]
For storing one item in a javaobject the class TextStruc is used. An Arraylist of string is used for storing the text lines. The constructor splits the date fields and stores them in mFromTime and mToTime. The function addSeconds returns a string with the extra seconds.
package org.sysgarden;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//
// 1
// 00:00:00,500 --> 00:00:02,207
// Once upon a time ...
// [newline]
// 2 
//
public class TextStruct {
    private static SimpleDateFormat mSdf = new SimpleDateFormat("hh:mm:ss,SSS"); 
    private ArrayList mTextLines = null;
    private int  mDiffSeconds= 0;
    private Date mFromTime   = null;
    private Date mToTime     = null;

    TextStruct(ArrayList textLines){
      mTextLines=textLines;
      String[] splited = mTextLines.get(1).split(" ");
      try {
       mFromTime=mSdf.parse(splited[0]);
       mToTime=mSdf.parse(splited[2]);
      } catch( Exception e){
       e.printStackTrace();
      }
    }

    public String addSeconds(int aDiffSeconds){
      mDiffSeconds = aDiffSeconds;
      String newline=System.getProperty("line.separator");
      String s="";
      for (int i=0;i< mTextLines.size(); i++ ){
        if (i==1) {
          s+=mSdf.format(addTime(mFromTime)) + " --> " + mSdf.format(addTime(mToTime)) +newline;
        } else {
   s+=mTextLines.get(i) + newline;
        }
      }
      return s;
    }
    
    private Date addTime(Date d){
      Calendar cal=Calendar.getInstance();
      cal.setTime(d);
      cal.add(Calendar.SECOND, mDiffSeconds);
      return cal.getTime();
    }
  }
The second class is for reading the srt file and parsing it.
package org.sysgarden;

import java.io.*;
import java.util.ArrayList;

//
// java SubTitle "movie.srt" + 12
// java SubTitle "movie.srt" - 12
// 
public class SubTitle {
 public static void main (String[] args){
  if (args==null || args.length != 3) {
   System.out.println("Number argument does not match:3!=" + args.length );
   printUsage();
   System.exit(0);
  }
  SubTitle subTitle=new SubTitle(args[1], args[2]);
  subTitle.readFile(args[0]);
  subTitle.print();
 } 
 public static void printUsage (){
    System.out.println("use:java org.sysgarden.SubTitle filename +|- seconds");
 }
 
 private ArrayList items=new ArrayList();
 public static int diff_seconds=1;

 public SubTitle(String pm, String sec){
  int plusminus=1;
  int seconds=0;
  if ("-".equals(pm)){
    plusminus=-1;  
  }
  try {
    seconds=Integer.parseInt(sec);
  } catch(Exception e ){
    printUsage();
    e.getMessage();
    System.exit(0);
  }
   diff_seconds=plusminus*seconds;
   System.out.println("diff="+diff_seconds);
 }

 public void readFile(String filename) {
  try {
   // Open the file that is the first 
   FileInputStream fstream = new FileInputStream(filename);
   // Get the object of DataInputStream
   DataInputStream in = new DataInputStream(fstream);
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String strLine="";
   ArrayList lines=new ArrayList();
   //Read File Line By Line
   while ((strLine = br.readLine()) != null)   {
    // Print the content on the console
    System.out.println (strLine);
    if (isEmpty(strLine)){
     items.add(new TextStruct(lines));
     lines= new ArrayList();
    } else {
     lines.add(strLine);
    }
   }
   if (lines.size() > 1)
     items.add(new TextStruct(lines));
   //Close the input stream
   br.close();
   in.close();
  } catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
  }
 }

 public void print() {
   for (int i=0; i < items.size(); i++){
     System.out.println (items.get(i).addSeconds(diff_seconds));
   }  
 }

 private boolean isEmpty(String s){
  if(s == null)          return true;
  if(s.trim().isEmpty()) return true;
  return false;
 }
}

Geen opmerkingen:

Een reactie posten