Skip to content

Commit

Permalink
Fix ttml multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Mar 29, 2024
1 parent 1db5f59 commit f40aa3f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,17 @@ private static function DCSubtitles(string $file_content, $fps)
$internal_format = [];
$subtitles = $xml->xpath('//Subtitle');
foreach ($subtitles as $subtitle) {
$lines = [];
foreach ($subtitle->Text as $line) {
$tmp_lines = self::getLinesFromTextWithBr((string)$line->asXML());
foreach ($tmp_lines as $tmp_line) {
$lines[] = $tmp_line;
}
}
$internal_format[] = array(
'start' => self::ttmlTimeToInternal((string)$subtitle['TimeIn'], $fps),
'end' => self::ttmlTimeToInternal((string)$subtitle['TimeOut'], $fps),
'lines' => self::getLinesFromTextWithBr((string)$subtitle->Text->asXML()),
'lines' => $lines,
);
}

Expand Down Expand Up @@ -292,10 +299,17 @@ private static function subtitleXml(string $file_content, $fps)
$internal_format = [];

foreach ($xml->Paragraph as $paragraph) {
$lines = [];
foreach ($paragraph->Text as $line) {
$tmp_lines = self::getLinesFromTextWithBr((string)$line->asXML());
foreach ($tmp_lines as $tmp_line) {
$lines[] = $tmp_line;
}
}
$subtitle = [
'start' => (int)$paragraph->StartMilliseconds / 1000,
'end' => (int)$paragraph->EndMilliseconds / 1000,
'lines' => self::getLinesFromTextWithBr($paragraph->Text->asXML()),
'lines' => $lines,
];
$internal_format[] = $subtitle;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,28 @@ public function testConvertFromXml9()
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testMultipleLines()
{
$text = <<<X
<?xml version="1.0" encoding="UTF-8"?>
<DCSubtitle Version="1.0">
<SubtitleID>b74d4623-4dd7-486f-831f-24a453894c7d</SubtitleID>
<MovieTitle>CONSEGUENZEen</MovieTitle>
<ReelNumber>Unico</ReelNumber>
<Language>English</Language>
<Font Color="FFFFFFFF" Effect="border" EffectColor="FF000000" Size="42" Weight="normal">
<Font Italic="yes">
<Subtitle SpotNumber="2" TimeIn="00:00:01:000" TimeOut="00:00:02:000" FadeUpTime="0" FadeDownTime="0">
<Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="14.0">The worst thing for a man</Text>
<Text Direction="horizontal" HAlign="center" HPosition="0.0" VAlign="bottom" VPosition="6.0">who spends a lot of time alone</Text>
</Subtitle>
</Font>
</Font>
</DCSubtitle>
X;
$actual = Subtitles::loadFromString($text)->getInternalFormat();
$expected = (new Subtitles())->add(1, 2, ['The worst thing for a man', 'who spends a lot of time alone'])->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit f40aa3f

Please sign in to comment.