Skip to content

Commit

Permalink
Add STL bigger timestamp support
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Mar 14, 2024
1 parent f7a0748 commit 34ed2f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
39 changes: 12 additions & 27 deletions src/Code/Converters/StlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
}

$internal_format[] = [
'start' => static::convertFromSrtTime(static::getStartLine($line), $frames_per_seconds),
'end' => static::convertFromSrtTime(static::getEndLine($line), $frames_per_seconds),
'start' => TxtConverter::timeToInternal(static::getStartLine($line), $frames_per_seconds),
'end' => TxtConverter::timeToInternal(static::getEndLine($line), $frames_per_seconds),
'lines' => static::getLines($line),
];
}
Expand Down Expand Up @@ -81,24 +81,6 @@ protected static function getEndLine($line)
return $end_string;
}

protected static function convertFromSrtTime($srt_time, $frames_per_seconds)
{
$parts = explode(':', $srt_time);
$frames = array_pop($parts);

$tmp_time = implode(':', $parts); // '21:30:10'
$only_seconds = strtotime("1970-01-01 $tmp_time UTC");

if ($frames > $frames_per_seconds - 1) {
$frames = $frames_per_seconds - 1;
}
$milliseconds = $frames / $frames_per_seconds;

$seconds = $only_seconds + $milliseconds;

return $seconds;
}

protected static function returnFramesFromTime($srt_time)
{
$parts = explode(':', $srt_time);
Expand All @@ -115,19 +97,22 @@ protected static function doesLineHaveTimestamp($line)
}

// stl counts frames at the end (25 - 30 frames)
protected static function toStlTime($seconds)
protected static function toStlTime($internal_time)
{
if ($seconds >= 86400) {
throw new \Exception('conversion function doesnt support more than 1 day, edit the code ' . $seconds);
if ($internal_time >= (3600 * 100)) {
throw new \Exception('conversion function doesnt support more than 99 hours, edit the code ' . $internal_time);
}

$milliseconds = $seconds - (int)$seconds;
$milliseconds = $internal_time - (int)$internal_time;
$frames_unpadded = floor(25 * $milliseconds); // 25 frames
$frames = str_pad($frames_unpadded, 2, '0', STR_PAD_LEFT);

$stl_time = gmdate("H:i:s:$frames", (int)$seconds);
$hours = floor($internal_time / 3600);
$minutes = floor(((int)$internal_time % 3600) / 60);
$remaining_seconds = (int)$internal_time % 60;

return sprintf("%02d:%02d:%02d:%02d", $hours, $minutes, $remaining_seconds, $frames_unpadded);


return $stl_time;
}

protected static function toStlLines($lines)
Expand Down
18 changes: 16 additions & 2 deletions tests/formats/StlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,26 @@ public function testParsesFilesWithComments()
$this->assertTrue(true);
}

public function testTimesBiggerThan24HoursThrowException()
public function testTimesBiggerThan24Hours()
{
$text = <<<TEXT
99:00:00:00 , 99:00:01:00 , a
TEXT;

$actual = Subtitles::loadFromString($text)->getInternalFormat();
$expected = (new Subtitles())->add(99 * 3600, 99 * 3600 + 1, 'a')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);

$actual = (new Subtitles())->add(99 * 3600, 99 * 3600 + 1, 'a')->content('stl');
$this->assertStringContainsString('99:00:01:00', $actual);
}

public function testTimesBiggerThan99HoursThrowException()
{
$this->expectException(\Exception::class);

$subtitles = new Subtitles();
$subtitles->add(0, 3600 * 24 * 10, 'text');
$subtitles->add(3600 * 123 - 1, 3600 * 123, 'text');
$subtitles->content('stl');
}

Expand Down

0 comments on commit 34ed2f7

Please sign in to comment.