summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/eleven/utils/SrtParser.java
blob: 749f80d05b5bb9debb1db03bfc69379516c1ee79 (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
/*
 * Copyright (C) 2014 Cyanogen, Inc.
 */
package com.cyngn.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" + s);
                    }
                }

                entry.mLine = subtitleBuilder.toString();

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

                ret.add(entry);
            }
        } catch (IOException ioe) {
            // shouldn't happen
            Log.e(TAG, ioe.getMessage(), ioe);
        } catch (ArrayIndexOutOfBoundsException e) {
            // if the time is malformed
            Log.e(TAG, e.getMessage());
        } 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;
    }
}