Skip to content

Commit

Permalink
Add fps to micro dvd
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Mar 29, 2024
1 parent 0fc4713 commit 6f802fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Code/Converters/SubMicroDvdConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ public function canParseFileContent($file_content)

public function fileContentToInternalFormat($file_content, $original_file_content)
{
$fps = self::$fps;

$pattern = "/\{(\d+)\}\{(\d+)\}(\{.*\})?(.+)/";
preg_match_all($pattern, $file_content, $blocks, PREG_SET_ORDER);

$internal_format = [];
foreach ($blocks as $block) {
foreach ($blocks as $k => $block) {
if ($k === 0 && is_numeric($block[4])) {
$fps = $block[4];
continue;
}

$internal_format[] = [
'start' => static::timeToInternal($block[1]),
'end' => static::timeToInternal($block[2]),
'start' => static::timeToInternal($block[1], $fps),
'end' => static::timeToInternal($block[2], $fps),
'lines' => explode("|", $block[4]),
];
}
Expand Down Expand Up @@ -60,9 +66,9 @@ public function internalFormatToFileContent(array $internal_format , array $opti
*
* @return float
*/
protected static function timeToInternal($sub_time)
protected static function timeToInternal($sub_time, $fps)
{
return $sub_time / self::$fps;
return $sub_time / $fps;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/formats/SubMicroDvdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public function testParsesLinesWithoutDotAtTheEnd()
$expected = (new Subtitles())->add(0, 4.17, 'Subtitle line 1')->add(6.26, 12.51, 'Subtitle line 2')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testUsesSpecifiedFps()
{
$content = <<<TEXT
{1}{1}25
{25}{50}Subtitle line 2
TEXT;
$actual = Subtitles::loadFromString($content)->getInternalFormat();
$expected = (new Subtitles())->add(1, 2, 'Subtitle line 2')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit 6f802fd

Please sign in to comment.