OSDN Git Service

Eleven: Cleanup all the whitespace
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / utils / SrtParser.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.cyanogenmod.eleven.utils;
17
18 import android.text.TextUtils;
19 import android.util.Log;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
26
27 public class SrtParser {
28     private static final String TAG = SrtParser.class.getSimpleName();
29
30     public static class SrtEntry {
31         public long mStartTimeMs;
32         public long mEndTimeMs;
33         String mLine;
34     }
35
36     /**
37      * The SubRip file format should contain entries that follow the following format:
38      *
39      * 1. A numeric counter identifying each sequential subtitle
40      * 2. The time that the subtitle should appear on the screen, followed by --> and the time it
41      *    should disappear
42      * 3. Subtitle text itself on one or more lines
43      * 4. A blank line containing no text, indicating the end of this subtitle
44      *
45      * The timecode format should be hours:minutes:seconds,milliseconds with time units fixed to two
46      * zero-padded digits and fractions fixed to three zero-padded digits (00:00:00,000).
47      */
48     public static ArrayList<SrtEntry> getSrtEntries(File f) {
49         ArrayList<SrtEntry> ret = null;
50         FileReader reader = null;
51         BufferedReader br = null;
52
53         try {
54             reader = new FileReader(f);
55             br = new BufferedReader(reader);
56
57             String header;
58             // since we don't really care about the 1st line of each entry (the # val) then read
59             // and discard it
60             while ((header = br.readLine()) != null) {
61                 // discard subtitle number
62                 header = br.readLine();
63                 if (header == null) {
64                     break;
65                 }
66
67                 SrtEntry entry = new SrtEntry();
68
69                 String[] startEnd = header.split("-->");
70                 entry.mStartTimeMs = parseMs(startEnd[0]);
71                 entry.mEndTimeMs = parseMs(startEnd[1]);
72
73                 StringBuilder subtitleBuilder = new StringBuilder("");
74                 String s = br.readLine();
75
76                 if (!TextUtils.isEmpty(s)) {
77                     subtitleBuilder.append(s);
78
79                     while (!((s = br.readLine()) == null || s.trim().equals(""))) {
80                         subtitleBuilder.append("\n").append(s);
81                     }
82                 }
83
84                 entry.mLine = subtitleBuilder.toString();
85
86                 if (ret == null) {
87                     ret = new ArrayList<SrtEntry>();
88                 }
89
90                 ret.add(entry);
91             }
92         } catch (NumberFormatException nfe) {
93             // The file isn't a valid srt format
94             Log.e(TAG, nfe.getMessage(), nfe);
95             ret = null;
96         } catch (IOException ioe) {
97             // shouldn't happen
98             Log.e(TAG, ioe.getMessage(), ioe);
99             ret = null;
100         } catch (ArrayIndexOutOfBoundsException e) {
101             // if the time is malformed
102             Log.e(TAG, e.getMessage());
103             ret = null;
104         } finally {
105             if (br != null) {
106                 try {
107                     br.close();
108                 } catch (IOException e) {
109                     Log.e(TAG, e.getMessage());
110                 }
111             }
112
113             if (reader != null) {
114                 try {
115                     reader.close();
116                 } catch (IOException e) {
117                     Log.e(TAG, e.getMessage());
118                 }
119             }
120         }
121
122         return ret;
123     }
124
125     private static long parseMs(String in) {
126         String[] timeArray = in.split(":");
127         long hours = Long.parseLong(timeArray[0].trim());
128         long minutes = Long.parseLong(timeArray[1].trim());
129
130         String[] secondTimeArray = timeArray[2].split(",");
131
132         long seconds = Long.parseLong(secondTimeArray[0].trim());
133         long millies = Long.parseLong(secondTimeArray[1].trim());
134
135         return hours * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000 + millies;
136     }
137 }