Advertisement
Booster

Subtitle Change Timing in seconds

Dec 30th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package readWriteFromFile;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintStream;
  6. import java.io.UnsupportedEncodingException;
  7. import java.text.ParseException;
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. /**
  14.  * You can enter value for seconds positive or negative. Then read line by line
  15.  * the .srt file. It which must be in format 00:00:00,000 --> 00:00:00,000 Then
  16.  * extract timing as String and change value of seconds. Return result into 2
  17.  * elements ArrayList and replace the hole timing line. In future I can make it
  18.  * work with GUI, directly choosing your subs file to modify.
  19.  *
  20.  * @author Ilian
  21.  *
  22.  */
  23.  
  24. public class SubtitlesChangeTiming {
  25.  
  26.     private static final String INPUT_FILE = "src//readWriteFromFile//santi-godzilla.srt";
  27.     private static final String OUTPUT_FILE = "src//readWriteFromFile//output.srt";
  28.     private static final Pattern REGEX = Pattern.compile("\\d+:\\d+:\\d+,\\d+");
  29.     private static int secondToAdd = 1;
  30.  
  31.     public static int getSecondToAdd() {
  32.         return secondToAdd;
  33.     }
  34.  
  35.     public static void setSecondToAdd(int secondToAdd) {
  36.         SubtitlesChangeTiming.secondToAdd = secondToAdd;
  37.     }
  38.  
  39.     public static void main(String[] args) {
  40.  
  41.         Scanner input = null;
  42.  
  43.         Scanner fileInput = null;
  44.         PrintStream fileOutput = null;
  45.         File file = new File(INPUT_FILE);
  46.  
  47.         try {
  48.             System.out
  49.                     .println("Enter seconds positive for delay, negative for rush:");
  50.             input = new Scanner(System.in);
  51.             setSecondToAdd(input.nextInt());
  52.             fileInput = new Scanner(file, "windows-1251");
  53.             fileOutput = new PrintStream(OUTPUT_FILE, "windows-1251");
  54.             String line;
  55.             while (fileInput.hasNext()) {
  56.                 line = fileInput.nextLine();
  57.                 line = changeTiming(line);
  58.                 fileOutput.println(line);
  59.             }
  60.         } catch (FileNotFoundException fnf) {
  61.             System.out.println("File not found.");
  62.         } catch (UnsupportedEncodingException uee) {
  63.             System.out.println("Encoding not supported.");
  64.         } finally {
  65.             if (fileInput != null) {
  66.                 fileInput.close();
  67.             }
  68.             if (fileOutput != null) {
  69.                 fileOutput.close();
  70.             }
  71.             if (input != null) {
  72.                 input.close();
  73.             }
  74.         }
  75.         System.out.println("Subtitles successfully corrected!");
  76.  
  77.     }
  78.  
  79.     // Add second to the subTiming
  80.  
  81.     private static String changeSeconds(String time) throws ParseException {
  82.         StringBuilder result = new StringBuilder();
  83.         String strSeconds = time.substring(6, 8);
  84.         String strMinutes = time.substring(3, 5);
  85.         String strHours = time.substring(0, 2);
  86.         Integer seconds = Integer.parseInt(strSeconds);
  87.         Integer minutes = Integer.parseInt(strMinutes);
  88.         Integer hours = Integer.parseInt(strHours);
  89.         // check if someone is not over 59
  90.         seconds += secondToAdd;
  91.         if (seconds > 59) {
  92.             seconds -= 60;
  93.             minutes += 1;
  94.             if (minutes > 59) {
  95.                 minutes -= 60;
  96.                 hours += 1;
  97.             }
  98.         }
  99.         hours.toString();
  100.         minutes.toString();
  101.         seconds.toString();
  102.         result.append(String.format("%02d", hours));
  103.         result.append(":");
  104.         result.append(String.format("%02d", minutes));
  105.         result.append(":");
  106.         result.append(String.format("%02d", seconds));
  107.         result.append(",");
  108.         result.append(time.substring(9));
  109.  
  110.         return result.toString();
  111.     }
  112.  
  113.     private static String changeTiming(String line) {
  114.         Matcher matcher = REGEX.matcher(line);
  115.         ArrayList<String> timingList = new ArrayList<String>();
  116.         int counter = 0;
  117.         while (matcher.find()) {
  118.             try {
  119.                 timingList.add(changeSeconds(matcher.group(0)));
  120.                 if (counter == 1) {
  121.                     line = timingList.get(0) + " --> " + timingList.get(1);
  122.                 }
  123.                 counter++;
  124.             } catch (ParseException e) {
  125.                 e.printStackTrace();
  126.             }
  127.         }
  128.  
  129.         return line;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement