-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubtitle.xml.class.php
167 lines (137 loc) · 4.55 KB
/
subtitle.xml.class.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Parsing XML format (for jwplayer).
*/
//http://www.w3.org/TR/2010/REC-ttaf1-dfxp-20101118/
//http://documentation.hwdmediashare.co.uk/wiki/Making_video_accessible
//http://www.codemiles.com/php/php-srt-to-xml-subtitle-converter-t1347.html
die('to implement');
class XmlSubtitle extends Subtitle{
protected function _parse(&$f){
}
public function get(){
}
public function decodeTime($v){
}
public function encodeTime($t){
}
public static function getVersion(){
//return '$Id: $';
}
}
?>
<?php
/**
http://www.codemiles.com/php/php-srt-to-xml-subtitle-converter-t1347.html
<script type="text/javascript">
var s1 = new SWFObject("player.swf","ply","328","200","9","#FFFFFF");
s1.addParam("allowfullscreen","true");
s1.addParam("allowscriptaccess","always");
s1.addParam("flashvars","file=bunny.flv&captions=subtitles/bunny.xml");
s1.write("container");
</script>
*/
// script to convert multi-line srt caption files to new-format (02-05-08) tt XML caption files
$use_cdata_tags = true;
$debug_output = true;
// the directory to write the new files in
// it must exist, be writeable, and be outside of the directory that is being scanned
$new_directory = '../temp/';
/////////////////////////////////// no user configuration below this \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// get filename or scan directory if it's a directory
$filename = (isset($_GET["filename"])) ? strval($_GET["filename"]) : "./";
// read each file into an array
$it = new RecursiveDirectoryIterator("$filename");
foreach(new RecursiveIteratorIterator($it) as $file)
{
// debug('Filename', $file); exit;
// debug('Ext', substr(strtolower($file), (strlen($file) - 4), 4));// exit;
// debug - only use test file
// if($file == '.\multi-line_test_file.srt')
// check for .srt extension
if(substr(strtolower($file), (strlen($file) - 4), 4) == '.srt')
{
$ttxml = '';
$full_line = '';
if($file_array = file($file))
{
// write tt , head, and div elements for the new file
$ttxml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$ttxml .= "<tt xml:lang='en' xmlns='http://www.w3.org/2006/10/ttaf1' xmlns:tts='http://www.w3.org/2006/10/ttaf1#style'>\n";
$ttxml .= " <head>\n";
$ttxml .= " </head>\n";
$ttxml .= " <body>\n";
$ttxml .= " <div xml:id=\"captions\">\n";
foreach($file_array as $line)
{
$line = rtrim($line);
// debug('Line', $line);
// get begin and end
// 00 : 00 : 32 , 000 --> 00 : 00 : 37 , 000
if(preg_match('/(\d\d):(\d\d):(\d\d),(\d\d\d) --> (\d\d):(\d\d):(\d\d),(\d\d\d)/', $line, $match))
{
$begin = $match[1] . ":" . $match[2] . ":" . $match[3] . "." . $match[4];
$end = $match[5] . ":" . $match[6] . ":" . $match[7] . "." . $match[8];
$full_line = '';
}
// if the next line is not blank, get the text
elseif($line != '')
{
if($full_line != '')
{
$full_line .= '<br />' . $line;
}
else
{
$full_line .= $line;
}
}
// if the next line is blank, write the paragraph
if($line == '')
{
// write new paragraph
// <p begin="00:08:01.50" end="00:08:07.50">Nothing is going on.</p>
if($use_cdata_tags)
{
$ttxml .= " <p begin=\"" . $begin . "\" end=\"" . $end . "\"><![CDATA[" . $full_line . "]]></p>\n";
}
else
{
$ttxml .= " <p begin=\"" . $begin . "\" end=\"" . $end . "\">" . $full_line . "</p>\n";
}
// debug('Text', $line);
// debug('ttxml', $ttxml); exit;
$full_line = '';
}
}
// write ending tags
$ttxml .= " </div>\n";
$ttxml .= " </body>\n";
$ttxml .= "</tt>\n";
// write new file
$new_file = $new_directory . substr($file, 0, (strlen($file) - 4)) . '.xml';
$fh = fopen($new_file, 'w') or die('Can\'t open: ' . $new_file);
fwrite($fh, $ttxml) or die('Can\'t write to: ' . $new_file);
fclose($fh);
}
}
}
function debug($title, $value)
{
global $debug_output;
if ($debug_output)
{
print "<pre>";
if (is_array($value))
{
print $title . ":\n";
print_r($value);
}
else
{
print $title . ": " . $value;
}
print "</pre>\n";
}
}
?>