source: plugins/captions/com/jeroenwijering/parsers/SRTParser.as @ 317

Revision 317, 1.4 KB checked in by jeroen, 4 years ago (diff)

added support for >2 lines for SRT files in captions.swf

Line 
1/**
2* Parse an Subrip caption file and return an array of captions.
3**/
4package com.jeroenwijering.parsers {
5
6
7import com.jeroenwijering.utils.Strings;
8
9
10public class SRTParser {
11
12
13        /**
14        * Parse the captions textblob into an array.
15        *
16        * @param dat    The loaded captions text, which must be in SubRip (.srt) format.
17        * @return               An array with captions. Each caption is an object with 'begin' and 'text' parameters.
18        **/
19        public static function parseCaptions(dat:String):Array {
20                var arr:Array = new Array({begin:0,text:''});
21                var lst:Array = dat.split("\r\n\r\n");
22                if(lst.length == 1) { lst = dat.split("\n\n"); }
23                for(var i:Number=0; i<lst.length; i++) {
24                        var obj:Object = SRTParser.parseCaption(lst[i]);
25                        arr.push(obj);
26                        if(obj['end']) {
27                                arr.push({begin:obj['end'],text:''});
28                                delete obj['end'];
29                        }
30                }
31                return arr;
32        };
33
34
35        /** Parse a single captions entry. **/
36        private static function parseCaption(dat:String):Object {
37                var obj:Object = new Object();
38                var arr:Array = dat.split("\r\n");
39                if(arr.length == 1) { arr = dat.split("\n"); }
40                try {
41                        var idx:Number = arr[1].indexOf(' --> ');
42                        obj['begin'] = Strings.seconds(arr[1].substr(0,idx));
43                        obj['end'] = Strings.seconds(arr[1].substr(idx+5));
44                        obj['text'] = arr[2];
45                        for (var i:Number = 3; i < arr.length; i++) {
46                                obj['text'] += '<br />'+arr[i];
47                        }
48                } catch (err:Error) {}
49                return obj;
50        };
51
52
53}
54
55
56}
Note: See TracBrowser for help on using the repository browser.