Skip to content

Commit

Permalink
Fix one more ttml format
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Jan 16, 2024
1 parent 6cd3155 commit 933ab27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,25 @@ private static function subtitleXml2(string $file_content)

$internal_format = [];

$i = 0;
foreach ($xml->text as $text) {
$attributes = $text->attributes();
$end = null;
if ($attributes['dur'] !== null) {
$end = (float) $attributes['start'] + (float) $attributes['dur'];
}
$internal_format[] = array(
'start' => (string) $attributes['start'],
'end' => (float)((string) $attributes['start'] + (string) $attributes['dur']),
'start' => (string)$attributes['start'],
'end' => $end,
'lines' => self::getLinesFromTextWithBr(str_replace("\n", "<br>", $text->asXML()))
);
if ($i !== 0 && ($internal_format[$i - 1]['end']) === null) {
$internal_format[$i - 1]['end'] = (float)$attributes['start'];
}
$i++;
}
if ($i !== 0 && $internal_format[$i - 1]['end'] === null) {
$internal_format[$i - 1]['end'] = (float)$attributes['start'] + 1;
}

return $internal_format;
Expand Down
14 changes: 14 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ public function testConvertFromXml7()
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testConvertFromXml7a()
{
$text = <<<X
<?xml version="1.0" encoding="utf-8" ?><transcript><text start="0.22">Creating an online shop
is now easier than ever.</text><text start="3.88">With a minimum investment of time and
money,</text></transcript>
X;
$actual = Subtitles::loadFromString($text)->getInternalFormat();
$expected = (new Subtitles())->add(0.22, 3.88, ['Creating an online shop', 'is now easier than ever.'])
->add(3.88, 4.88, ['With a minimum investment of time and', 'money,'])
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testConvertFromXml8()
{
$text = <<<X
Expand Down

0 comments on commit 933ab27

Please sign in to comment.