4ff92f0021fc1b96577f0bdc258e907b13543ec1
[atutor.git] / mods / atutor_opencaps / opencaps / conversion_service / include / classes / ccformats / cc_SubRipSrt_format.php
1 <?php\r
2 /**\r
3  * SubRipSrt Class\r
4  */\r
5 class SubRipSrt extends CaptionFormat\r
6 {\r
7         private $textStyles = array();\r
8 \r
9         /**\r
10          * Imports a caption string into a CaptionCollection \r
11          *\r
12          * @param String $theCCString the caption file as string\r
13          * @return CaptionCollection $myCcCollection A CaptionCollection Object\r
14          */\r
15         public function importCC($theCCString) \r
16         {\r
17                 $ccImport = '';\r
18 \r
19         // clean malformed patterns creted when saving files on win notepad\r
20         $toSearch = array(chr(13).chr(10));\r
21         $toReplace = array(chr(10));\r
22         $theCCString = str_replace($toSearch,$toReplace,$theCCString);\r
23                 \r
24         // split each caption by \n\n\r
25         $allCaptions=split(chr(10).chr(10),$theCCString);\r
26                 \r
27         // create a collection object\r
28         $myCollection = new CaptionCollection();\r
29         \r
30         $txtStyles = Array();\r
31         \r
32         $counter=0;\r
33         \r
34         foreach($allCaptions as $singleCaption)\r
35         {\r
36                 \r
37                 //echo '<br/>'.chr(10).''.$counter;\r
38                 \r
39                 // split each line of the single caption\r
40                 $captionParts=split(chr(10),$singleCaption);\r
41                 \r
42                 // add captions if minimal time and caption are set\r
43                 if (isset($captionParts[1]) && isset($captionParts[2]))\r
44                 {\r
45                         $counter++;\r
46                         $timeMark = '';\r
47                         $captionLines = '';\r
48                         \r
49                         // fix milisecond separator "," by "."\r
50                         $captionParts[1] = str_replace(',','.',$captionParts[1]);       \r
51                         \r
52                         // get time marks on line 2\r
53                         $timeMark = split('-->',$captionParts[1]);\r
54         \r
55                         // get time in and out\r
56                         $timeIn = trim($timeMark[0]);\r
57                         $timeOut = trim($timeMark[1]);\r
58                         \r
59                                 $captionLines = $captionParts[2]; // add caption line 1\r
60                                 \r
61                         // if caption has two lines\r
62                         if (count($captionParts)==4)\r
63                         {       \r
64                                 $captionLines .= ''.chr(10).$captionParts[3]; // add a new line + caption line 2\r
65                         }\r
66                         \r
67                         // Create a caption Object\r
68                         $theNewCaption = new Caption($timeIn,$timeOut,$captionLines,$txtStyles);\r
69                         \r
70                                 // add caption to CaptionCollection\r
71                                 $myCollection->addCaption($theNewCaption); \r
72         \r
73                         \r
74                         //echo ''.chr(10).'IN: '.$timeIn.'****'.' OUT:'.$timeOut;\r
75                         //echo ''.chr(10).''.$captionLines;\r
76                 }\r
77                 \r
78         } // end foreach\r
79         \r
80             return $myCollection;\r
81                  \r
82         } // end importCC()\r
83 \r
84         /**\r
85          * Exports a CaptionCollection object into a string\r
86          *\r
87          * @param CaptionCollection $theCollection A CaptionCollection Object\r
88          * @return String $captionString The caption as a String\r
89          */\r
90         public function exportCC($theCollection)\r
91         {\r
92                 \r
93                 $ccExport = '';\r
94         \r
95                 $myCollection = $theCollection->getCollection();\r
96   \r
97                 // fix time Srt Time Format\r
98                 $toSearch = array('.');\r
99         $toReplace = array(',');\r
100         \r
101                 $srtCounter = 0;\r
102                 \r
103                 foreach ($myCollection as $captionObj)\r
104                 {\r
105                         \r
106                         $srtCounter++;\r
107                         \r
108                         // fix QT time to SRT format, replace "." by ","\r
109                         $srtInTime = $captionObj->getInTime();\r
110                         $srtInTime  = str_replace($toSearch,$toReplace,$srtInTime);\r
111                         \r
112                         $srtOutTime = $captionObj->getOutTime();\r
113                         $srtOutTime  = str_replace($toSearch,$toReplace,$srtOutTime);\r
114                         \r
115                         $srtCaption = $captionObj->getCaption();\r
116                         $srtCaption = str_replace('<BR/>',chr(10),$srtCaption);\r
117                         $srtCaption = str_replace('<br/>',chr(10),$srtCaption);\r
118                          \r
119                         $ccExport .= "$srtCounter\n".$srtInTime." --> ".$srtOutTime."\n".$srtCaption."\n\n";\r
120                         \r
121                 } // end foreach\r
122                 \r
123                 // Fix if there are more than the two empty line separator (standard)\r
124                 $ccExport = str_replace(chr(10).chr(10).chr(10),chr(10).chr(10),$ccExport);\r
125                 \r
126                 return $ccExport;\r
127                 \r
128         } // end  exportCC()\r
129         \r
130         /**\r
131          * Verify if the caption file is a QText caption file \r
132         */\r
133         public function checkFormat($theCCString)\r
134         {\r
135                 $isValid = false;\r
136                 $patternCheck = "/({(QTtext)})/"; // RegExp to look for QText \r
137                 preg_match_all($patternCheck,$theCCString,$patternFound);\r
138                 \r
139                 if(count($patternFound)>0)\r
140                 {\r
141                         $isValid = true;\r
142                 }\r
143 \r
144                 return $isValid;\r
145                 \r
146         } // end  checkFormat() \r
147 \r
148         /*\r
149          * Here functions to re-define\r
150          */\r
151         public function getName()\r
152         {\r
153                 return 'SubRip - Srt';\r
154         }\r
155         \r
156         public function getAbout()\r
157         {\r
158                 return '???';\r
159         }\r
160         \r
161         public function getVersion()\r
162         {\r
163                 return '???';\r
164         }\r
165                 \r
166         public function getFileExtension()\r
167         {\r
168                 return 'srt';\r
169         }\r
170         \r
171         public function getIdPattern()\r
172         {\r
173                 $idPattern = '/([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) (-->) ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})/';\r
174                 //$idPattern = '/([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})/';\r
175                 //$idPattern = '';\r
176                 //$idPattern .= '/'; // start pattern\r
177                 \r
178                 //$idPattern .= '([0-9]{2}\n)'; \r
179                  \r
180                 //$idPattern .= '/'; // end pattern\r
181 \r
182                 return $idPattern;\r
183         }\r
184         \r
185         public function allowsTextStyles()\r
186         {\r
187                 return '0';\r
188         }       \r
189         \r
190         public function template()\r
191         {\r
192                 $ccTemplate = '\r
193 1\r
194 00:00:42,360 --> 00:00:48,360\r
195 With this device we can\r
196 give anything an attitude.\r
197 \r
198 2\r
199 ';\r
200                 \r
201                 return $ccTemplate;\r
202         }\r
203 }  // end SubRipSrt \r
204 ?>