Skip to content

Commit

Permalink
Correctly parse vtt comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Mar 29, 2024
1 parent 21e2842 commit 1db5f59
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Code/Converters/VttConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function canParseFileContent($file_content)

public function fileContentToInternalFormat($file_content, $original_file_content)
{
$lines = mb_split("\n", $file_content);
$content = self::removeComments($file_content);

$lines = mb_split("\n", $content);
$colon_count = 1;
$internal_format = [];
$i = -1;
Expand Down Expand Up @@ -155,4 +157,26 @@ protected static function internalTimeToVtt($internal_time)

return $srt_time;
}

protected static function removeComments($content)
{
$lines = mb_split("\n", $content);
$lines = array_map('trim', $lines);
$new_lines = [];
$is_comment = false;
foreach ($lines as $line) {
if ($is_comment && strlen($line)) {
continue;
}
if (strpos($line, 'NOTE ') === 0) {
$is_comment = true;
continue;
}
$is_comment = false;
$new_lines[] = $line;
}

$new_content = implode("\n", $new_lines);
return $new_content;
}
}
27 changes: 27 additions & 0 deletions tests/formats/VttTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,31 @@ public function testClientFileNoArrow()
->getInternalFormat();
$this->assertEquals($expected, $actual);
}

public function testComment()
{
$input_vtt_file_content = <<< TEXT
WEBVTT
00:00:00.000 --> 00:00:01.000
a
NOTE comment
comment
00:00:01.000 --> 00:00:02.000
b
NOTE comment
00:00:02.000 --> 00:00:03.000
c
TEXT;

$actual = Subtitles::loadFromString($input_vtt_file_content)->getInternalFormat();
$expected = (new Subtitles())->add(0, 1, 'a')->add(1, 2, 'b')->add(2, 3, 'c')->getInternalFormat();

$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit 1db5f59

Please sign in to comment.