source: trunk/as3/com/jeroenwijering/parsers/SRTParser.as @ 140

Revision 140, 1.2 KB checked in by jeroen, 4 years ago (diff)

fixed some accessibility bugs and the always-loading of yt.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', 'end' and 'text' parameters.
18        **/
19        public static function parseCaptions(dat:String):Array {
20                var arr:Array = new Array();
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                        if(obj['end']) { arr.push(obj); }
26                }
27                return arr;
28        };
29
30
31        /** Parse a single captions entry. **/
32        private static function parseCaption(dat:String):Object {
33                var obj:Object = new Object();
34                var arr:Array = dat.split("\r\n");
35                if(arr.length == 1) { arr = dat.split("\n"); }
36                try {
37                        var idx:Number = arr[1].indexOf(' --> ');
38                        obj['begin'] = Strings.seconds(arr[1].substr(0,idx));
39                        obj['end'] = Strings.seconds(arr[1].substr(idx+5));
40                        obj['text'] = arr[2];
41                        if(arr[3]) { obj['text'] += '<br />'+arr[3]; }
42                } catch (err:Error) {}
43                return obj;
44        };
45
46
47}
48
49
50}
Note: See TracBrowser for help on using the repository browser.