@@ -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])
0 commit comments