Skip to content

Commit

Permalink
Parse without end timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 12, 2023
1 parent 2d0a573 commit 43e8416
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
39 changes: 29 additions & 10 deletions src/Code/Converters/TxtQuickTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@ public function fileContentToInternalFormat($file_content)
{
$internal_format = [];

$started = false;
$blocks = explode("\n\n", trim($file_content));
foreach ($blocks as $block) {
preg_match('/(?<start>\[.{11}\])\n(?<text>[\s\S]+?)(?=\n\[)\n(?<end>\[.{11}\])/m', $block, $matches);

// if block doesn't contain text
if (empty($matches)) {
continue;
$lines = explode("\n", $block);
$tmp = [
'start' => null,
'end' => null,
'lines' => [],
];
foreach ($lines as $line) {
$parts = TxtConverter::getLineParts($line, 2, 1);
if ($started === false && $parts['start'] === null) {
continue;
}
$started = true;
if ($tmp['start'] === null && $parts['start']) {
$tmp['start'] = $parts['start'];
} elseif ($parts['text'] !== null) {
$tmp['lines'][] = $parts['text'];
} elseif ($tmp['end'] === null && $parts['start']) {
$tmp['end'] = $parts['start'];
}
}

$internal_format[] = [
'start' => static::timeToInternal($matches['start'], self::$fps),
'end' => static::timeToInternal($matches['end'], self::$fps),
'lines' => explode("\n", $matches['text']),
];
if (isset($tmp['lines'][0]) && trim($tmp['lines'][0])) {
$internal_format[] = [
'start' => static::timeToInternal($tmp['start'], self::$fps),
'end' => $tmp['end'] ? static::timeToInternal($tmp['end'], self::$fps) : null,
'lines' => $tmp['lines'],
];
}
}

$internal_format = TxtConverter::fillStartAndEndTimes($internal_format);

return $internal_format;
}

Expand Down
19 changes: 18 additions & 1 deletion tests/formats/TxtQuickTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function testConvertingToFormat()
public function testConvertingToInternalFormat()
{
$actual = Subtitles::loadFromString($this->qttxt)->getInternalFormat();

$expected = (new Subtitles())
->add(137.44, 140.375, ['Senator, we\'re making', 'our final approach into Coruscant.'])
->add(3740.476, 3742.501, ['Very good, Lieutenant.'])
Expand All @@ -57,4 +56,22 @@ public function testConvertingToInternalFormat()
$this->assertInternalFormatsEqual($expected, $actual, 0.07);
}

public function testParsesWithNoEndTimestamp()
{
$text = <<<X
{QTtext} {font:Tahoma}
{plain} {size:20}
{timeScale:30}
{width:160} {height:32}
{timestamps:absolute} {language:0}
[00:00:00.00]
a
[00:00:12.00]
b
X;
$actual = Subtitles::loadFromString($text)->getInternalFormat();
$expected = (new Subtitles())->add(0, 12, 'a')->add(12, 13, 'b')->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit 43e8416

Please sign in to comment.