summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/eleven/utils/SrtParser.java
blob: bace32a31f697d908a4fffd43c56c10817ebddda (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2014 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.eleven.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class SrtParser {
    private static final String TAG = SrtParser.class.getSimpleName();

    public static class SrtEntry {
        public long mStartTimeMs;
        public long mEndTimeMs;
        String mLine;
    }

    /**
     * The SubRip file format should contain entries that follow the following format:
     *
     * 1. A numeric counter identifying each sequential subtitle
     * 2. The time that the subtitle should appear on the screen, followed by --> and the time it
     *    should disappear
     * 3. Subtitle text itself on one or more lines
     * 4. A blank line containing no text, indicating the end of this subtitle
     *
     * The timecode format should be hours:minutes:seconds,milliseconds with time units fixed to two
     * zero-padded digits and fractions fixed to three zero-padded digits (00:00:00,000).
     */
    public static ArrayList<SrtEntry> getSrtEntries(File f) {
        ArrayList<SrtEntry> ret = null;
        FileReader reader = null;
        BufferedReader br = null;

        try {
            reader = new FileReader(f);
            br = new BufferedReader(reader);

            String header;
            // since we don't really care about the 1st line of each entry (the # val) then read
            // and discard it
            while ((header = br.readLine()) != null) {
                // discard subtitle number
                header = br.readLine();
                if (header == null) {
                    break;
                }

                SrtEntry entry = new SrtEntry();

                String[] startEnd = header.split("-->");
                entry.mStartTimeMs = parseMs(startEnd[0]);
                entry.mEndTimeMs = parseMs(startEnd[1]);

                StringBuilder subtitleBuilder = new StringBuilder("");
                String s = br.readLine();

                if (!TextUtils.isEmpty(s)) {
                    subtitleBuilder.append(s);

                    while (!((s = br.readLine()) == null || s.trim().equals(""))) {
                        subtitleBuilder.append("\n").append(s);
                    }
                }

                entry.mLine = subtitleBuilder.toString();

                if (ret == null) {
                    ret = new ArrayList<SrtEntry>();
                }

                ret.add(entry);
            }
        } catch (NumberFormatException nfe) {
            // The file isn't a valid srt format
            Log.e(TAG, nfe.getMessage(), nfe);
            ret = null;
        } catch (IOException ioe) {
            // shouldn't happen
            Log.e(TAG, ioe.getMessage(), ioe);
            ret = null;
        } catch (ArrayIndexOutOfBoundsException e) {
            // if the time is malformed
            Log.e(TAG, e.getMessage());
            ret = null;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }
            }

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        }

        return ret;
    }

    private static long parseMs(String in) {
        String[] timeArray = in.split(":");
        long hours = Long.parseLong(timeArray[0].trim());
        long minutes = Long.parseLong(timeArray[1].trim());

        String[] secondTimeArray = timeArray[2].split(",");

        long seconds = Long.parseLong(secondTimeArray[0].trim());
        long millies = Long.parseLong(secondTimeArray[1].trim());

        return hours * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000 + millies;
    }
}