Skip to content

Commit 818a3f3

Browse files
committed
Merge pull request #5 from gordielachance/master
new functions shift and sync
2 parents 620809d + eaaaa87 commit 818a3f3

File tree

2 files changed

+123
-5
lines changed

2 files changed

+123
-5
lines changed

src/SrtParser/srtFile.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,96 @@ public function changeFPS($old_fps, $new_fps){
411411
$old_start = $this->subs[$keys[$i]]->getStart();
412412
$old_stop = $this->subs[$keys[$i]]->getStop();
413413

414-
$new_start = round($old_start * ($new_fps / $old_fps));
415-
$new_stop = round($old_stop * ($new_fps / $old_fps));
414+
$new_start = $old_start * ($new_fps / $old_fps);
415+
$new_stop = $old_stop * ($new_fps / $old_fps);
416416

417417
$this->subs[$keys[$i]]->setStart($new_start);
418418
$this->subs[$keys[$i]]->setStop($new_stop);
419419
}
420420
}
421+
422+
/**
423+
* Shifts a range of subtitles a specified amount of time.
424+
* @param $time The time to use (ms), which can be positive or negative.
425+
* @param int $startIndex The subtitle index the range begins with.
426+
* @param int $endIndex The subtitle index the range ends with.
427+
*
428+
*/
429+
public function shift($time, $startIndex = false, $endIndex = false) {
430+
431+
if (!is_numeric($time)) return false;
432+
if ($time==0) return true;
433+
434+
$keys = array_keys($this->subs);
435+
$sub_count = sizeof($keys);
436+
437+
if (!$startIndex) $startIndex = 0;
438+
if (!$endIndex) $endIndex = $sub_count - 1;
439+
440+
$startSubtitle = $this->getSub($startIndex);
441+
$endSubtitle = $this->getSub($endIndex);
442+
443+
//check subtitles do exist
444+
if (!$startSubtitle || !$endSubtitle) return false;
445+
446+
for($i=$startIndex; $i < $endIndex; $i++){
447+
$subtitle = $this->getSub($i);
448+
$subtitle->shift($time);
449+
}
450+
451+
return true;
452+
}
453+
454+
/**
455+
* Auto syncs a range of subtitles given their first and last correct times.
456+
* The subtitles are first shifted to the first subtitle's correct time, and then proportionally
457+
* adjusted using the last subtitle's correct time.
458+
*
459+
* Based on gnome-subtitles (https://git.gnome.org/browse/gnome-subtitles/)
460+
*
461+
* @param int $startIndex The subtitle index to start the adjustment with.
462+
* @param int $startTime The correct start time for the first subtitle.
463+
* @param int $endIndex The subtitle index to end the adjustment with.
464+
* @param int $endTime The correct start time for the last subtitle.
465+
* @param bool $syncLast Whether to sync the last subtitle.
466+
* @return bool Whether the subtitles could be adjusted
467+
*/
468+
469+
public function sync($startIndex,$startTime,$endIndex,$endTime,$syncLast = true) {
470+
471+
$keys = array_keys($this->subs);
472+
$sub_count = sizeof($keys);
473+
474+
//set first and last subtitles index
475+
if (!$startIndex) $startIndex = 0;
476+
if (!$endIndex) $endIndex = $sub_count - 1;
477+
478+
//check subtitles do exist
479+
$startSubtitle = $this->getSub($startIndex);
480+
$endSubtitle = $this->getSub($endIndex);
481+
if (!$startSubtitle || !$endSubtitle) return false;
482+
483+
if (!($startTime < $endTime)) return false;
484+
485+
$shift = $startTime - $startSubtitle->getStart();
486+
$factor = ($endTime - $startTime) / ($endSubtitle->getStart() - $startSubtitle->getStart());
487+
488+
/* Shift subtitles to the start point */
489+
if ($shift) {
490+
$this->shift($shift,$startIndex,$endIndex);
491+
}
492+
493+
/* Sync timings with proportion */
494+
for ($index=$startIndex; $index <= $endIndex ; $index++) {
495+
$entry = $this->getSub($index);
496+
$entry->scale($startTime,$factor);
497+
}
498+
499+
return true;
500+
}
501+
502+
503+
421504

422505
/**
423506
* Builds file content (file_content[_notag])

src/SrtParser/srtFileEntry.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function setStopTC($_stop){
218218
* @param int $_start
219219
*/
220220
public function setStart($_start){
221-
$this->start = $_start;
221+
$this->start = round($_start);
222222
$this->startTC = self::ms2tc($_start);
223223
}
224224

@@ -228,7 +228,7 @@ public function setStart($_start){
228228
* @param int $_stop
229229
*/
230230
public function setStop($_stop){
231-
$this->stop = $_stop;
231+
$this->stop = round($_stop);
232232
$this->stopTC = self::ms2tc($_stop);
233233
}
234234

@@ -341,4 +341,39 @@ private function calcRS(){
341341

342342
$this->readingSpeed = ($this->strlen() * 1000) / ($this->durationMS-500);
343343
}
344-
}
344+
345+
public function scale($baseTime,$factor = 1){
346+
347+
if ($factor==1) return;
348+
349+
$new_start = $baseTime + (($this->getStart() - $baseTime) * $factor);
350+
$new_stop = $baseTime + (($this->getStop() - $baseTime) * $factor);
351+
352+
$this->setStart($new_start);
353+
$this->setStop($new_stop);
354+
355+
return true;
356+
}
357+
358+
/**
359+
* Set a delay (positive or negative)
360+
*
361+
* @param int $ms Delay in milliseconds
362+
*/
363+
364+
public function shift($time = 0){
365+
366+
if (!is_numeric($time)) return false;
367+
if ($time==0) return true;
368+
369+
$start = $this->getStart();
370+
$stop = $this->getStop();
371+
372+
$this->setStart($start + $time);
373+
$this->setStop($stop + $time);
374+
375+
return true;
376+
377+
}
378+
}
379+

0 commit comments

Comments
 (0)