diff --git a/src/Code/Converters/TtmlConverter.php b/src/Code/Converters/TtmlConverter.php index 8955646..b44fa26 100644 --- a/src/Code/Converters/TtmlConverter.php +++ b/src/Code/Converters/TtmlConverter.php @@ -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 = ''; @@ -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; @@ -302,9 +319,12 @@ private static function subtitleXml2(string $file_content) private static function getLinesFromTextWithBr(string $text) { + $text = preg_replace('//', '
', $text); // normalize
*/ $lines = preg_replace('//', '
', $text); // normalize
*/ - $lines = explode('
', $lines); + $lines = str_replace('
', "\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); diff --git a/tests/formats/TtmlTest.php b/tests/formats/TtmlTest.php index 75cc41e..82785ea 100644 --- a/tests/formats/TtmlTest.php +++ b/tests/formats/TtmlTest.php @@ -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], ]; } @@ -442,4 +443,20 @@ public function testConvertFromXml8() ->getInternalFormat(); $this->assertInternalFormatsEqual($expected, $actual); } + + public function testConvertFromXml9() + { + $text = << + +

​ ​チャン チャン​ ​​ +​​ ​쟌 쟌​ ​

+ +X; + $actual = Subtitles::loadFromString($text)->getInternalFormat(); + $expected = (new Subtitles()) + ->add(9.159, 9.927, ['チャン チャン', '쟌 쟌']) + ->getInternalFormat(); + $this->assertInternalFormatsEqual($expected, $actual); + } } \ No newline at end of file