From 43e8416b08b6c508ebcde7aa27867e24ad980e68 Mon Sep 17 00:00:00 2001 From: Mantas Date: Sat, 12 Aug 2023 12:42:16 +0300 Subject: [PATCH] Parse without end timestamp --- src/Code/Converters/TxtQuickTimeConverter.php | 39 ++++++++++++++----- tests/formats/TxtQuickTimeTest.php | 19 ++++++++- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Code/Converters/TxtQuickTimeConverter.php b/src/Code/Converters/TxtQuickTimeConverter.php index 6f98b57..9524fea 100644 --- a/src/Code/Converters/TxtQuickTimeConverter.php +++ b/src/Code/Converters/TxtQuickTimeConverter.php @@ -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('/(?\[.{11}\])\n(?[\s\S]+?)(?=\n\[)\n(?\[.{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; } diff --git a/tests/formats/TxtQuickTimeTest.php b/tests/formats/TxtQuickTimeTest.php index 1554e7b..e85b8e7 100644 --- a/tests/formats/TxtQuickTimeTest.php +++ b/tests/formats/TxtQuickTimeTest.php @@ -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.']) @@ -57,4 +56,22 @@ public function testConvertingToInternalFormat() $this->assertInternalFormatsEqual($expected, $actual, 0.07); } + public function testParsesWithNoEndTimestamp() + { + $text = <<getInternalFormat(); + $expected = (new Subtitles())->add(0, 12, 'a')->add(12, 13, 'b')->getInternalFormat(); + $this->assertInternalFormatsEqual($expected, $actual); + } } \ No newline at end of file