Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package finki.np;
  2.  
  3. /**
  4.  * Created by Coka on 23-Jan-17.
  5.  */
  6.  
  7. import java.io.InputStream;
  8. import java.time.Duration;
  9. import java.time.LocalTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14.  
  15. public class SubtitlesTest {
  16.     public static void main(String[] args) {
  17.         Subtitles subtitles = new Subtitles();
  18.         int n = subtitles.loadSubtitles(System.in);
  19.         System.out.println("+++++ ORIGINIAL SUBTITLES +++++");
  20.         subtitles.print();
  21.         int shift = n * 37;
  22.         shift = (shift % 2 == 1) ? -shift : shift;
  23.         System.out.println(String.format("SHIFT FOR %d ms", shift));
  24.         subtitles.shift(shift);
  25.         System.out.println("+++++ SHIFTED SUBTITLES +++++");
  26.         subtitles.print();
  27.     }
  28. }
  29.  
  30. // Вашиот код овде
  31.  
  32. class Subtitles {
  33.     List<Srt> srt;
  34.  
  35.     public Subtitles() {
  36.         srt = new ArrayList<>();
  37.     }
  38.  
  39.     public int loadSubtitles(InputStream inputStream) {
  40.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
  41.  
  42.         Scanner scanner = new Scanner(inputStream);
  43.         while (scanner.hasNextLine()) {
  44.             String seq = scanner.nextLine();
  45.             Integer seqNum = Integer.parseInt(seq);
  46.             String times = scanner.nextLine();
  47.             String partTimes[] = times.split(" --> ");
  48.  
  49.             LocalTime firstTime = LocalTime.parse(partTimes[0].replace(",", "."), formatter);
  50.             LocalTime secondTime = LocalTime.parse(partTimes[1].replace(",", "."), formatter);
  51.  
  52.             String text = "";
  53.             while (true) {
  54.                 String tmp;
  55.                 if (scanner.hasNextLine()) {
  56.                     tmp = scanner.nextLine();
  57.                 } else break;
  58.                 if (tmp.length() == 0)
  59.                     break;
  60.                 text += tmp + "\n";
  61.             }
  62.  
  63.             srt.add(new Srt(seqNum, firstTime, secondTime, text));
  64.         }
  65.  
  66.         return srt.size();
  67.     }
  68.  
  69.     public void print() {
  70.         srt.forEach(System.out::println);
  71.     }
  72.  
  73.     void shift(int ms) {
  74.         srt.forEach(srt1 -> srt1.shift(ms));
  75.     }
  76. }
  77.  
  78. class Srt {
  79.     int seqNum;
  80.     LocalTime startTime;
  81.     LocalTime endTime;
  82.     String text;
  83.     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss,SSS");
  84.  
  85.     public Srt(int seqNum, LocalTime startTime, LocalTime endTime, String text) {
  86.         this.seqNum = seqNum;
  87.         this.startTime = startTime;
  88.         this.endTime = endTime;
  89.         this.text = text;
  90.     }
  91.  
  92.     void shift(int ms) {
  93.         startTime = startTime.plus(Duration.ofMillis(ms));
  94.         endTime = endTime.plus(Duration.ofMillis(ms));
  95.     }
  96.  
  97.     @Override
  98.     public String toString() {
  99.         return seqNum + "\n"
  100.                 + startTime.format(formatter)
  101.                 + " --> "
  102.                 + endTime.format(formatter)
  103.                 + "\n"
  104.                 + text;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement