Skip to content

Commit

Permalink
Make TTML output similar to SubtitleEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Jun 26, 2023
1 parent 55f6a89 commit f92309f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 15 additions & 4 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,25 @@ public function fileContentToInternalFormat($file_content)
public function internalFormatToFileContent(array $internal_format)
{
$file_content = '<?xml version="1.0" encoding="utf-8"?>
<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" ttp:timeBase="media" xmlns:tts="http://www.w3.org/ns/ttml#style" xml:lang="en" xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" ttp:timeBase="media" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en" xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<head>
<metadata>
<ttm:title></ttm:title>
</metadata>
<styling>
<style id="s0" tts:backgroundColor="black" tts:fontStyle="normal" tts:fontSize="16" tts:fontFamily="sansSerif" tts:color="white" />
<style xml:id="s0" tts:backgroundColor="black" tts:fontStyle="normal" tts:fontSize="16px" tts:fontFamily="sansSerif" tts:color="white" />
</styling>
<layout>
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="start" xml:id="topLeft" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="start" xml:id="centerLeft" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="start" xml:id="bottomLeft" />
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="center" xml:id="topCenter" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="center" xml:id="centerСenter" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="center" xml:id="bottomCenter" />
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="end" xml:id="topRight" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="end" xml:id="centerRight" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="end" xml:id="bottomRight" />
</layout>
</head>
<body style="s0">
<div>
Expand All @@ -93,7 +104,7 @@ public function internalFormatToFileContent(array $internal_format)
$end = static::internalTimeToTtml($block['end']);
$lines = implode("<br />", $block['lines']);

$file_content .= " <p begin=\"{$start}s\" id=\"p{$k}\" end=\"{$end}s\">{$lines}</p>\n";
$file_content .= " <p begin=\"{$start}s\" xml:id=\"p{$k}\" end=\"{$end}s\">{$lines}</p>\n";
}

$file_content .= ' </div>
Expand Down Expand Up @@ -132,7 +143,7 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate = null)

protected static function internalTimeToTtml($internal_time)
{
return number_format($internal_time, 1, '.', '');
return round($internal_time, 3);
}

protected static function framesPerSecond($dom)
Expand Down
19 changes: 15 additions & 4 deletions tests/files/ttml.ttml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" ttp:timeBase="media" xmlns:tts="http://www.w3.org/ns/ttml#style" xml:lang="en" xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" ttp:timeBase="media" xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en" xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
<head>
<metadata>
<ttm:title></ttm:title>
</metadata>
<styling>
<style id="s0" tts:backgroundColor="black" tts:fontStyle="normal" tts:fontSize="16" tts:fontFamily="sansSerif" tts:color="white" />
<style xml:id="s0" tts:backgroundColor="black" tts:fontStyle="normal" tts:fontSize="16px" tts:fontFamily="sansSerif" tts:color="white" />
</styling>
<layout>
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="start" xml:id="topLeft" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="start" xml:id="centerLeft" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="start" xml:id="bottomLeft" />
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="center" xml:id="topCenter" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="center" xml:id="centerСenter" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="center" xml:id="bottomCenter" />
<region tts:extent="80% 40%" tts:origin="10% 10%" tts:displayAlign="before" tts:textAlign="end" xml:id="topRight" />
<region tts:extent="80% 40%" tts:origin="10% 30%" tts:displayAlign="center" tts:textAlign="end" xml:id="centerRight" />
<region tts:extent="80% 40%" tts:origin="10% 50%" tts:displayAlign="after" tts:textAlign="end" xml:id="bottomRight" />
</layout>
</head>
<body style="s0">
<div>
<p begin="137.4s" id="p0" end="140.4s">Senator, we're making<br />our final approach into Coruscant.</p>
<p begin="3740.5s" id="p1" end="3742.5s">Very good, Lieutenant.</p>
<p begin="137.4s" xml:id="p0" end="140.4s">Senator, we're making<br />our final approach into Coruscant.</p>
<p begin="3740.5s" xml:id="p1" end="3742.5s">Very good, Lieutenant.</p>
</div>
</body>
</tt>
7 changes: 7 additions & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public function testTimeParseWithFpsAndMultiplierGiven()
$this->assertInternalFormatsEqual($expected, $actual);
}

public function testOutputsAccurateTimestamp()
{
$actual = (new Subtitles())->add(0.3, 8.456, 'test')->content('ttml');
$this->assertStringContainsString('"0.3s"', $actual);
$this->assertStringContainsString('"8.456s"', $actual);
}

/**
* @dataProvider timeFormatProvider
*/
Expand Down

0 comments on commit f92309f

Please sign in to comment.