Skip to content

Commit

Permalink
Fix small time overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Jul 28, 2023
1 parent bf65f0a commit ec6a786
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,27 @@ public static function loadFromString($string, $format = null)
unset($row);
unset($line);

// fix up to a second time overlap
foreach ($internal_format as $k => $row) {
if ($k === 0) {
continue;
}
$diff = $internal_format[$k - 1]['end'] - $row['start'];
if ($diff < 1 && $diff > 0) {
$internal_format[$k - 1]['end'] = $row['start'];
}
}
unset($row);

// check if time is increasing
$last_end_time = 0;
foreach ($internal_format as $row) {
if ($row['start'] < $last_end_time) {
throw new UserException('Start time is lower than the last end time: ' . SrtConverter::internalTimeToSrt($row['start']) . ' ' . $row['lines'][0]);
throw new UserException('Times are overlapping near text: ' . $row['lines'][0]);
}
$last_end_time = $row['end'];
if ($row['start'] > $row['end']) {
throw new UserException('Start time is bigger than end time: ' . SrtConverter::internalTimeToSrt($row['start']) . ' ' . SrtConverter::internalTimeToSrt($row['end']) . ' ' . $row['lines'][0]);
throw new UserException('Times are overlapping near text: ' . $row['lines'][0]);
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ public function testLastEndTimeIsSmallerThanCurrentStartTime()
b
');
}

public function testFixesUpToSecondTimeOverlap()
{
$actual = Subtitles::loadFromString('
1
00:00:01,000 --> 00:00:02,000
a
2
00:00:01,500 --> 00:00:04,000
b
')->getInternalFormat();
$expected = (new Subtitles())->add(1, 1.5, 'a')->add(1.5, 4, 'b')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit ec6a786

Please sign in to comment.