Skip to content

Commit

Permalink
Fix vtt
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Oct 16, 2023
1 parent 975b5af commit 1c2ec04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Code/Converters/TxtConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private static function hasTime($line)
return preg_match(self::$time_regexp, $line) === 1;
}

private static function hasText($line)
public static function hasText($line)
{
return preg_match(self::$any_letter_regex, $line) === 1;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Code/Converters/VttConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Done\Subtitles\Code\Converters;

use Done\Subtitles\Code\Helpers;
use Done\Subtitles\Code\UserException;

class VttConverter implements ConverterContract
Expand Down Expand Up @@ -32,7 +33,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
continue;
}

if ($parts['start'] && $parts['end']) {
if ($parts['start'] && $parts['end'] && Helpers::strContains($line, '-->')) {
$i++;
$internal_format[$i]['start'] = self::vttTimeToInternal($parts['start']);
$internal_format[$i]['end'] = self::vttTimeToInternal($parts['end']);
Expand All @@ -53,8 +54,8 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
unset($internal_format[$i - 1]['lines'][$count - 1]);
}
}
} elseif ($parts['text'] !== null) {
$text_line = $parts['text'];
} elseif (TxtConverter::hasText($line)) {
$text_line = $line;
// speaker
$speaker = null;
if (preg_match('/<v(?: (.*?))?>((?:.*?)<\/v>)/', $text_line, $matches)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/formats/VttTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,19 @@ public function testParsesIncorrectTimestampWithComma()
->getInternalFormat();
$this->assertEquals($expected, $actual);
}

public function testTextIsNotRecognizedAsTimestamp()
{
$given = <<< TEXT
WEBVTT
00:04.308 --> 00:06.670
08:00 next Thursday, or 09:00
TEXT;
$actual = (new Subtitles())->loadFromString($given)->getInternalFormat();
$expected = (new Subtitles())
->add(4.308, 6.670, '08:00 next Thursday, or 09:00')
->getInternalFormat();
$this->assertEquals($expected, $actual);
}
}

0 comments on commit 1c2ec04

Please sign in to comment.