Skip to content

Commit

Permalink
Merge pull request #62 from Andrius521/master
Browse files Browse the repository at this point in the history
Improve *.ttml file format parsing
  • Loading branch information
mantas-done authored Jun 22, 2023
2 parents 78d0e36 + 2e9d6e2 commit 45809e8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function canParseFileContent($file_content)
public function fileContentToInternalFormat($file_content)
{
$dom = new \DOMDocument();
$dom->loadXML($file_content);
@$dom->loadXML($file_content);

$array = array();

Expand Down Expand Up @@ -120,12 +120,14 @@ protected static function ttmlTimeToInternal($ttml_time)
} elseif (substr($ttml_time, -1) === 's') {
return rtrim($ttml_time, 's');
} else {
$timeParts = explode(':', $ttml_time);
$hours = (int)$timeParts[0];
$minutes = (int)$timeParts[1];
$seconds = (int)$timeParts[2];
$totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds;
return $totalSeconds;
$time_parts = explode('.', $ttml_time);
$milliseconds = 0;
if (isset($time_parts[1])) {
$milliseconds = (float) ('0.' . $time_parts[1]);
}

list($hours, $minutes, $seconds) = array_map('intval', explode(':', $time_parts[0]));
return ($hours * 3600) + ($minutes * 60) + $seconds + $milliseconds;
}
}
}
21 changes: 21 additions & 0 deletions tests/files/ttml_with_duplicated_element_ids.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<tt xml:lang="en"
xmlns="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/ns/ttml#styling">
<head>
<metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<ttm:title>TTML Example With Duplicated Element Ids</ttm:title>
<ttm:copyright>Thierry Michel 2015</ttm:copyright>
</metadata>

<styling xmlns:tts="http://www.w3.org/ns/ttml#styling">
<style xml:id="s1" tts:color="red" tts:textAlign="center" />
</styling>
</head>
<body>
<div>
<p xml:id="c1" begin="00:00:00" end="00:00:01">First line.</p>
<p xml:id="c1" begin="00:00:01" end="00:00:02">Second line.</p>
<p xml:id="c3" begin="00:00:02" end="00:00:03">Third line.</p>
</div>
</body>
</tt>
22 changes: 22 additions & 0 deletions tests/files/ttml_with_mixed_timestamps.ttml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<tt xml:lang="en"
xmlns="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/ns/ttml#styling">
<head>
<metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<ttm:title>TTML Example Mixed Timestamp formats</ttm:title>
<ttm:copyright>Thierry Michel 2015</ttm:copyright>
</metadata>

<styling xmlns:tts="http://www.w3.org/ns/ttml#styling">
<style xml:id="s1" tts:color="red" tts:textAlign="center" />
</styling>
</head>

<body>
<div>
<p xml:id="c1" begin="00:00:00" end="00:00:10">First line.</p>
<p xml:id="c2" begin="00:00:11.200" end="00:00:13.300">Second line.</p>
<p xml:id="c3" begin="15s" end="18s">Third line.</p>
</div>
</body>
</tt>
24 changes: 24 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,28 @@ public function testParses2()
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testDifferentTimestampParse()
{
$ttml_path = './tests/files/ttml_with_mixed_timestamps.ttml';
$actual = Subtitles::loadFromFile($ttml_path)->getInternalFormat();
$expected = (new Subtitles())
->add(0, 10, 'First line.')
->add(11.2, 13.3, ['Second line.'])
->add(15, 18, ['Third line.'])
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testDuplicatedElementIdsParse()
{
$ttml_path = './tests/files/ttml_with_duplicated_element_ids.ttml';
$actual = Subtitles::loadFromFile($ttml_path)->getInternalFormat();
$expected = (new Subtitles())
->add(0, 1, 'First line.')
->add(1, 2, ['Second line.'])
->add(2, 3, ['Third line.'])
->getInternalFormat();
$this->assertInternalFormatsEqual($expected, $actual);
}

}

0 comments on commit 45809e8

Please sign in to comment.