Skip to content

Commit 933ab27

Browse files
committed
Fix one more ttml format
1 parent 6cd3155 commit 933ab27

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Code/Converters/TtmlConverter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,25 @@ private static function subtitleXml2(string $file_content)
326326

327327
$internal_format = [];
328328

329+
$i = 0;
329330
foreach ($xml->text as $text) {
330331
$attributes = $text->attributes();
332+
$end = null;
333+
if ($attributes['dur'] !== null) {
334+
$end = (float) $attributes['start'] + (float) $attributes['dur'];
335+
}
331336
$internal_format[] = array(
332-
'start' => (string) $attributes['start'],
333-
'end' => (float)((string) $attributes['start'] + (string) $attributes['dur']),
337+
'start' => (string)$attributes['start'],
338+
'end' => $end,
334339
'lines' => self::getLinesFromTextWithBr(str_replace("\n", "<br>", $text->asXML()))
335340
);
341+
if ($i !== 0 && ($internal_format[$i - 1]['end']) === null) {
342+
$internal_format[$i - 1]['end'] = (float)$attributes['start'];
343+
}
344+
$i++;
345+
}
346+
if ($i !== 0 && $internal_format[$i - 1]['end'] === null) {
347+
$internal_format[$i - 1]['end'] = (float)$attributes['start'] + 1;
336348
}
337349

338350
return $internal_format;

tests/formats/TtmlTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,20 @@ public function testConvertFromXml7()
417417
$this->assertInternalFormatsEqual($expected, $actual);
418418
}
419419

420+
public function testConvertFromXml7a()
421+
{
422+
$text = <<<X
423+
<?xml version="1.0" encoding="utf-8" ?><transcript><text start="0.22">Creating an online shop
424+
is now easier than ever.</text><text start="3.88">With a minimum investment of time and
425+
money,</text></transcript>
426+
X;
427+
$actual = Subtitles::loadFromString($text)->getInternalFormat();
428+
$expected = (new Subtitles())->add(0.22, 3.88, ['Creating an online shop', 'is now easier than ever.'])
429+
->add(3.88, 4.88, ['With a minimum investment of time and', 'money,'])
430+
->getInternalFormat();
431+
$this->assertInternalFormatsEqual($expected, $actual);
432+
}
433+
420434
public function testConvertFromXml8()
421435
{
422436
$text = <<<X

0 commit comments

Comments
 (0)