Skip to content

Commit

Permalink
Add new ttml format
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Aug 31, 2023
1 parent b2a4744 commit 03dd6b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,28 @@ public function fileContentToInternalFormat($file_content)
$div_begin = $element->getAttribute('begin');
$div_end = $element->getAttribute('end');
foreach ($element->getElementsByTagName('p') as $pElement) {
$begin = $pElement->hasAttribute('begin') ? $pElement->getAttribute('begin') : $div_begin;
$begin = null;
if ($pElement->hasAttribute('begin')) {
$begin = $pElement->getAttribute('begin');
} elseif ($pElement->getAttribute('t')) {
$begin = $pElement->getAttribute('t');
} elseif ($div_begin) {
$begin = $div_begin;
}
$begin = static::ttmlTimeToInternal($begin, $fps);
$end = $pElement->hasAttribute('end') ? $pElement->getAttribute('end') : $div_end;

$end = null;
if ($pElement->hasAttribute('end')) {
$end = $pElement->getAttribute('end');
} elseif ($div_end) {
$end = $div_end;
}
if ($end) {
$end = static::ttmlTimeToInternal($end, $fps);
} elseif ($pElement->hasAttribute('dur') && $pElement->getAttribute('dur')) {
$end = $begin + static::ttmlTimeToInternal($pElement->getAttribute('dur'), $fps);
} elseif ($pElement->hasAttribute('d') && $pElement->getAttribute('d')) {
$end = $begin + static::ttmlTimeToInternal($pElement->getAttribute('d'), $fps);
}
$lines = '';

Expand Down Expand Up @@ -169,6 +184,8 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate)
$totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds + $frames / $frame_rate;

return $totalSeconds;
} elseif (is_numeric($ttml_time)) {
return $ttml_time / 1000;
} else {
$time_parts = explode('.', $ttml_time);
$milliseconds = 0;
Expand Down Expand Up @@ -302,9 +319,12 @@ private static function subtitleXml2(string $file_content)

private static function getLinesFromTextWithBr(string $text)
{

$text = preg_replace('/<br\s*\/?>/', '<br>', $text); // normalize <br>*/
$lines = preg_replace('/<tt:br*\/?>/', '<br>', $text); // normalize <br>*/
$lines = explode('<br>', $lines);
$lines = str_replace('<br>', "\n", $lines);
$lines = preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $lines); // remove zero width space characters
$lines = explode("\n", $lines);
$lines = array_map('strip_tags', $lines);
$lines = array_map('trim', $lines);

Expand Down
17 changes: 17 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static function timeFormatProvider()
['00:00:10', 10, null],
['00:00:5.100', 5.1, null],
['55s', 55, null],
['8500', 8.5, null],
];
}

Expand Down Expand Up @@ -442,4 +443,20 @@ public function testConvertFromXml8()
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testConvertFromXml9()
{
$text = <<<X
<?xml version="1.0" encoding="utf-8" ?>
<body>
<p t="9159" d="768" wp="2" ws="1"><s p="2">​</s><s>​</s><s p="3">​ ​チャン チャン​ ​</s><s p="2">​
​</s><s p="4">​ ​쟌 쟌​ ​</s><s p="2">​</s></p>
</body>
X;
$actual = Subtitles::loadFromString($text)->getInternalFormat();
$expected = (new Subtitles())
->add(9.159, 9.927, ['チャン チャン', '쟌 쟌'])
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}
}

0 comments on commit 03dd6b7

Please sign in to comment.