2 * Copyright (C) 2014 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.cyanogenmod.eleven.utils;
18 import android.text.TextUtils;
19 import android.util.Log;
21 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.ArrayList;
27 public class SrtParser {
28 private static final String TAG = SrtParser.class.getSimpleName();
30 public static class SrtEntry {
31 public long mStartTimeMs;
32 public long mEndTimeMs;
37 * The SubRip file format should contain entries that follow the following format:
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
42 * 3. Subtitle text itself on one or more lines
43 * 4. A blank line containing no text, indicating the end of this subtitle
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).
48 public static ArrayList<SrtEntry> getSrtEntries(File f) {
49 ArrayList<SrtEntry> ret = null;
50 FileReader reader = null;
51 BufferedReader br = null;
54 reader = new FileReader(f);
55 br = new BufferedReader(reader);
58 // since we don't really care about the 1st line of each entry (the # val) then read
60 while ((header = br.readLine()) != null) {
61 // discard subtitle number
62 header = br.readLine();
67 SrtEntry entry = new SrtEntry();
69 String[] startEnd = header.split("-->");
70 entry.mStartTimeMs = parseMs(startEnd[0]);
71 entry.mEndTimeMs = parseMs(startEnd[1]);
73 StringBuilder subtitleBuilder = new StringBuilder("");
74 String s = br.readLine();
76 if (!TextUtils.isEmpty(s)) {
77 subtitleBuilder.append(s);
79 while (!((s = br.readLine()) == null || s.trim().equals(""))) {
80 subtitleBuilder.append("\n" + s);
84 entry.mLine = subtitleBuilder.toString();
87 ret = new ArrayList<SrtEntry>();
92 } catch (IOException ioe) {
94 Log.e(TAG, ioe.getMessage(), ioe);
95 } catch (ArrayIndexOutOfBoundsException e) {
96 // if the time is malformed
97 Log.e(TAG, e.getMessage());
102 } catch (IOException e) {
103 Log.e(TAG, e.getMessage());
107 if (reader != null) {
110 } catch (IOException e) {
111 Log.e(TAG, e.getMessage());
119 private static long parseMs(String in) {
120 String[] timeArray = in.split(":");
121 long hours = Long.parseLong(timeArray[0].trim());
122 long minutes = Long.parseLong(timeArray[1].trim());
124 String[] secondTimeArray = timeArray[2].split(",");
126 long seconds = Long.parseLong(secondTimeArray[0].trim());
127 long millies = Long.parseLong(secondTimeArray[1].trim());
129 return hours * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000 + millies;