This repository was archived by the owner on Aug 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphpEditSubtitles.php
More file actions
383 lines (327 loc) · 10.1 KB
/
Copy pathphpEditSubtitles.php
File metadata and controls
383 lines (327 loc) · 10.1 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
class phpEditSubtitles {
private $version = '1.1';
/** @var string */
private $file = '';
/** @var string */
private $type = 'srt';
/** @var string */
private $originalType = 'srt';
/** @var array */
private $subtitles = array();
/*
* Add a row to array $subtitles
* @param: int $order
* @param: string $timeIni
* @param: string $timeEnd
* @param: string $subtitle
* @param: bool $moveTime (default: true)
*/
public function addSubtitle($order,$timeIni,$timeEnd,$subtitle,$moveTime = true) {
// verify time consistence
$this->verifyTimeConsistence($order,$timeIni,$timeEnd);
// create the array
$arrParams[] = array(
'order' => $order,
'time_ini' => $timeIni,
'time_end' => $timeEnd,
'subtitle' => utf8_decode($subtitle)."\r\n",
);
// if I want to move the time of the consecuents rows it must to be true
if($moveTime) $this->verifyTimeMovement($order,$timeIni,$timeEnd);
// add row
array_splice( $this->subtitles, ($order-1), 0, $arrParams );
}
/*
* Delete row from array $subtitles
* @param: int $order
*/
public function deleteSubtitle($order) {
$subtitles = $this->getSubtitles();
unset($subtitles[($order-1)]);
$this->setSubtitles(array_values($subtitles));
}
/*
* Edit row on array $subtitles
* @param: int $order
* @param: string $timeIni
* @param: string $timeEnd
* @param: string $subtitle
* @param: bool $moveTime (default: true)
*/
public function editSubtitle($order,$timeIni,$timeEnd,$subtitle,$moveTime = true) {
// verify time consistence
$this->verifyTimeConsistence($order,$timeIni,$timeEnd);
// create the array
$arrParams[] = array(
'order' => $order,
'time_ini' => $timeIni,
'time_end' => $timeEnd,
'subtitle' => utf8_decode($subtitle)."\r\n",
);
// if I want to move the time of the consecuents rows it must to be true
if($moveTime) $this->verifyTimeMovement($order,$timeIni,$timeEnd);
// replace row
array_splice( $this->subtitles, ($order-1), 1, $arrParams );
}
/*
* List array $subtitles
* @return: array
*/
public function listSubtitles() {
return $this->getSubtitles();
}
/*
* Save file
* @param: string $newFile
*/
public function saveFile($newFile = '') {
if(empty($newFile)) $newFile = time().'_edited_subtitles.'.$this->getType();
else $newFile = $newFile.'.'.$this->getType();
@file_put_contents($newFile,$this->dumpSubtitles());
}
/*
* Dump array $subtitles into a string
* @return: string $dump
*/
private function dumpSubtitles() {
$dump = '';
for($i=0;$i<count($this->subtitles);$i++) {
if($this->type == 'srt') $dump .= ($i+1)."\r";
$this->subtitles[$i]['time_ini'] = $this->convertType($this->subtitles[$i]['time_ini']);
$this->subtitles[$i]['time_end'] = $this->convertType($this->subtitles[$i]['time_end']);
$dump .= $this->subtitles[$i]['time_ini'].' --> '.$this->subtitles[$i]['time_end']."\r";
$dump .= $this->subtitles[$i]['subtitle']."\r\n";
}
return $dump;
}
/*
* Verify if have to move time of the consecuents rows
* @param: int $order
* @param: string $timeIni
* @param: string $timeEnd
*/
private function verifyTimeMovement($order,$timeIni,$timeEnd) {
// get the time ini/end
$currTimeIni = $this->timeToMilliseconds($this->subtitles[($order-1)]['time_ini']);
$currTimeEnd = $this->timeToMilliseconds($this->subtitles[($order-1)]['time_end']);
$currTimeIniNew = $this->timeToMilliseconds($timeIni);
$currTimeEndNew = $this->timeToMilliseconds($timeEnd);
$diffTimeIni = $currTimeIniNew - $currTimeIni;
$diffTimeEnd = $currTimeEndNew - $currTimeEnd;
$this->moveTime($order,$diffTimeIni,$diffTimeEnd);
}
/*
* Convert time string into milliseconds
* @param: string $time
* @return: int $milliseconds
*/
private function timeToMilliseconds($time) {
$arr = explode(":",$time);
if(strpos(".",$arr[2]) !== false) $arr2 = explode(".",$arr[2]);
else $arr2 = explode(",",$arr[2]);
$hs = $arr[0];
$mi = $arr[1];
$sc = $arr2[0];
$ml = $arr2[1];
$milliseconds = ($hs*3600*1000)+($mi*60*1000)+($sc*1000)+$ml;
return $milliseconds;
}
/*
* Convert time milliseconds to format hs:mi:ss,mil
* @param: int $milliseconds
* @return: string $time
*/
private function millisecondsToTime($milliseconds) {
$sc = floor($milliseconds / 1000);
$mi = floor($sc / 60);
$hs = floor($mi / 60);
$milliseconds = $milliseconds % 1000;
$sc = $sc % 60;
$mi = $mi % 60;
if($this->type == 'vtt') $format = '%02u:%02u:%02u.%03u';
else $format = '%02u:%02u:%02u,%03u';
$time = sprintf($format, $hs, $mi, $sc, $milliseconds);
return $time;
}
/*
* Verify time consistence
* @param: int $order
* @param: string $timeIni
* @param: string $timeEnd
*/
private function verifyTimeConsistence($order,$timeIni,$timeEnd) {
// get the time ini/end
$currTimeIni = $this->timeToMilliseconds($timeIni);
$currTimeEnd = $this->timeToMilliseconds($timeEnd);
// verify time consistence
if($currTimeIni > $currTimeEnd) {
die('Inconsistence of time');
}
// get the total amount of time
$arrMountOfTime = $this->getAmountOfTime($order);
// verify if current time initial is smaller than the amount of time from current time until start
if($currTimeIni < $arrMountOfTime['toStart']) {
die('Time start exceded');
}
// verify if current time finel is bigger than the amount of time from current time until the end
if($currTimeEnd > $arrMountOfTime['toEnd']) {
die('Time end exceded');
}
}
/*
* Move time for all rows
* @param: int $order
* @param: int $diffIni
* @param: int $diffEnd
*/
private function moveTime($order,$diffIni,$diffEnd) {
$arrSub = array();
// move the time for consecuents rows greater than current time
for($i=0;$i<count($this->subtitles);$i++) {
if($i > ($order-1)) {
$eachTimeIni = $this->timeToMilliseconds($this->subtitles[$i]['time_ini']);
$eachTimeEnd = $this->timeToMilliseconds($this->subtitles[$i]['time_end']);
$timeIni = $this->millisecondsToTime($eachTimeIni + $diffEnd);
$timeEnd = $this->millisecondsToTime($eachTimeEnd + $diffEnd);
$this->subtitles[$i]['time_ini'] = $timeIni;
$this->subtitles[$i]['time_end'] = $timeEnd;
}
}
// get the current time initial before change
$currTimeIni = $this->timeToMilliseconds($this->subtitles[($order-1)]['time_ini'])+$diffIni;
// move the time for consecuents rows smaller than current time
if($diffIni < 0) {
for($i=($order-1);$i>-1;$i--) {
if($i < ($order-1)) {
$eachTimeIni = $this->timeToMilliseconds($this->subtitles[$i]['time_ini']);
$eachTimeEnd = $this->timeToMilliseconds($this->subtitles[$i]['time_end']);
$eachDiff = $eachTimeEnd - $eachTimeIni;
if($currTimeIni < $eachTimeEnd) {
$eachNewTimeIni = (($eachTimeIni + $diffIni)<0?0:($eachTimeIni + $diffIni));
$eachNewTimeEnd = (($eachTimeEnd + $diffIni)<0?0:($eachTimeEnd + $diffIni));
$timeIni = $this->millisecondsToTime($eachNewTimeIni);
$timeEnd = $this->millisecondsToTime($eachNewTimeEnd);
$this->subtitles[$i]['time_ini'] = $timeIni;
$this->subtitles[$i]['time_end'] = $timeEnd;
$currTimeIni = $timeIni;
}
}
}
}
}
/*
* Get amount of time from current position until start and end
* @param: int $order
* @return array
*/
private function getAmountOfTime($order) {
$millisecondsToStart = 0;
for($i=($order-2);$i>-1;$i--) {
$eachTimeIni = $this->timeToMilliseconds($this->subtitles[$i]['time_ini']);
$eachTimeEnd = $this->timeToMilliseconds($this->subtitles[$i]['time_end']);
$millisecondsToStart += ($eachTimeEnd-$eachTimeIni);
}
$millisecondsToEnd = 0;
for($i=($order);$i<count($this->subtitles);$i++) {
$eachTimeIni = $this->timeToMilliseconds($this->subtitles[$i]['time_ini']);
$eachTimeEnd = $this->timeToMilliseconds($this->subtitles[$i]['time_end']);
$millisecondsToEnd += ($eachTimeEnd-$eachTimeIni);
}
return array('toStart' => $millisecondsToStart, 'toEnd' => $millisecondsToEnd);
}
/*
* Read file
*/
public function readFile() {
$handle = @fopen($this->getFile(),"r");
$this->detectFileType($this->getFile());
$arrSubtitle = array();
$i = 0;
$found = false;
$started = false;
if ($handle) {
while ($buffer = fgets($handle, 4096)) {
// verifico si empieza el bloque de subtitulo
$arr = $this->findSubtitleBlock($buffer);
$found = $arr['found'];
if($found) {
$arrSubtitle[$i]['order'] = ($i+1);
$arrSubtitle[$i]['time_ini'] = $arr['time_ini'];
$arrSubtitle[$i]['time_end'] = $arr['time_end'];
$arrSubtitle[$i]['subtitle'] = '';
$started = true;
} else {
if($started) {
if($buffer != "\r\n") {
$arrSubtitle[$i]['subtitle'] .= $buffer;
} else {
$started = false;
$i++;
}
}
}
}
fclose($handle);
$this->setSubtitles($arrSubtitle);
}
}
/*
* Find block of subtitles
* @param: string $string
* @return: array
*/
public function findSubtitleBlock($string) {
$arr['found'] = false;
$pattern = ' --> ';
$pos = strpos($string, $pattern);
if ($pos !== false && strlen($string) == 31) {
$arr['found'] = true;
$arr['time_ini'] = substr($string,0,12);
$arr['time_end'] = substr($string,17,12);
}
return $arr;
}
/*
* Convert time from originalType to new type (srt -> vtt or vtt -> srt)
*/
private function convertType($time) {
if($this->originalType != $this->getType()) return $this->millisecondsToTime($this->timeToMilliseconds($time));
else return $time;
}
/*
* Detect type of original file
*/
private function detectFileType($file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
switch($ext){
case "srt":
case "vtt":
$type = $ext;
break;
default:
$type = 'srt';
break;
}
$this->originalType = $type;
}
/* Getters and Setters */
public function setFile($file) {
$this->file = $file;
}
public function getFile() {
return $this->file;
}
public function setType($type) {
$this->type = $type;
}
public function getType() {
return $this->type;
}
public function setSubtitles($subtitles) {
$this->subtitles = $subtitles;
}
public function getSubtitles() {
return $this->subtitles;
}
}