1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.StringTokenizer; public class ChangeSrtTime { public static void main(String[] args) { File file = new File("C:\\The.departed.2of2.en.srt"); String startTime, endTime; String result = ""; int tunePara = 13; //parameter for tuning ArrayList comp = new ArrayList(); //debug use boolean isDebug = false; //set debug mode try { // Create a FileReader and then wrap it with BufferedReader. FileReader file_reader = new FileReader (file); BufferedReader buf_reader = new BufferedReader (file_reader); // Read each line of the file do { String line = buf_reader.readLine(); if (line == null){ break; }else if (line.startsWith("0") && (line.indexOf(":") == 2)){ //e.g. 00:00:02,798 --> 00:00:06,296 if(isDebug){ comp.add("org: " + line); } StringTokenizer st = new StringTokenizer(line, "-->"); if (st.hasMoreTokens()){ startTime = st.nextToken().trim(); try { DateFormat formatter = new SimpleDateFormat("HH:mm:ss,SSS"); Date date = (Date)formatter.parse(startTime); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.SECOND, tunePara); result = formatter.format(cal.getTime()); } catch (ParseException e) {} } if(st.hasMoreTokens()) { endTime = st.nextToken().trim(); try { DateFormat formatter = new SimpleDateFormat("HH:mm:ss,SSS"); Date date = (Date)formatter.parse(endTime); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.SECOND, tunePara); result += " --> " + formatter.format(cal.getTime()); if(isDebug){ comp.add("new: " + result);} } catch (ParseException e) {} } System.out.println(result); }else{ System.out.println(line); } } while (true); buf_reader.close (); } catch (IOException e) { System.out.println ("IO exception =" + e ); } if (isDebug){ System.out.println("nnnn ==========debug console======== "); Iterator iterator = comp.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("n ==========END========= "); } } } |