-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56bf440
commit 45c8c5b
Showing
4 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php namespace Done\Subtitles; | ||
|
||
// qt.txt | ||
class TxtConverter implements ConverterContract { | ||
|
||
private static $fps = 23; | ||
|
||
public function fileContentToInternalFormat($file_content) | ||
{ | ||
$internal_format = []; | ||
|
||
$blocks = explode("\n\n", trim($file_content)); | ||
foreach ($blocks as $block) { | ||
preg_match('/(?<start>\[.{11}\])\n(?<text>[\s\S]+?)(?=\n\[)\n(?<end>\[.{11}\])/m', $block, $matches); | ||
|
||
// if block doesn't contain text | ||
if (empty($matches)) { | ||
continue; | ||
} | ||
|
||
$internal_format[] = [ | ||
'start' => static::timeToInternal($matches['start'], self::$fps), | ||
'end' => static::timeToInternal($matches['end'], self::$fps), | ||
'lines' => explode("\n", $matches['text']), | ||
]; | ||
} | ||
|
||
return $internal_format; | ||
} | ||
|
||
/** | ||
* Convert library's "internal format" (array) to file's content | ||
* | ||
* @param array $internal_format Internal format | ||
* @return string Converted file content | ||
*/ | ||
public function internalFormatToFileContent(array $internal_format) | ||
{ | ||
$file_content = '{QTtext} {font:Tahoma} | ||
{plain} {size:20} | ||
{timeScale:30} | ||
{width:160} {height:32} | ||
{timestamps:absolute} {language:0} | ||
'; | ||
|
||
foreach ($internal_format as $block) { | ||
$start = static::fromInternalTime($block['start'], self::$fps); | ||
$end = static::fromInternalTime($block['end'], self::$fps); | ||
$lines = implode("\r\n", $block['lines']); | ||
|
||
$file_content .= $start . "\r\n"; | ||
$file_content .= $lines . "\r\n"; | ||
$file_content .= $end . "\r\n"; | ||
$file_content .= "\r\n"; | ||
} | ||
|
||
return $file_content; | ||
} | ||
|
||
// ------------------------------ private -------------------------------------------------------------------------- | ||
|
||
protected static function timeToInternal($srt_time, $fps) | ||
{ | ||
$parsed = date_parse("1970-01-01 $srt_time UTC"); | ||
$time = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'] + $parsed['fraction'] / $fps * 100; | ||
|
||
return $time; | ||
} | ||
|
||
protected static function fromInternalTime($internal_time, $fps) | ||
{ | ||
$parts = explode('.', $internal_time); | ||
$whole = $parts[0]; // 1 | ||
$decimal = isset($parts[1]) ? substr($parts[1], 0, 2) : 0; | ||
|
||
$frame = round($decimal / 100 * 24); | ||
|
||
$srt_time = gmdate("H:i:s", floor($whole)) . '.' . str_pad($frame, 2, '0', STR_PAD_LEFT); | ||
|
||
return "[$srt_time]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
use Done\Subtitles\Subtitles; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TxtTest extends TestCase { | ||
|
||
use AdditionalAssertions; | ||
|
||
private $qttxt = "{QTtext} {font:Tahoma} | ||
{plain} {size:20} | ||
{timeScale:30} | ||
{width:160} {height:32} | ||
{timestamps:absolute} {language:0} | ||
[00:02:17.11] | ||
Senator, we're making | ||
our final approach into Coruscant. | ||
[00:02:20.09] | ||
[01:02:20.11] | ||
Very good, Lieutenant. | ||
[01:02:22.12] | ||
"; | ||
|
||
public function testConvertingToFormat() | ||
{ | ||
$actual = (new Subtitles()) | ||
->add(137.44, 140.375, ['Senator, we\'re making', 'our final approach into Coruscant.']) | ||
->add(3740.476, 3742.501, ['Very good, Lieutenant.']) | ||
->content('txt'); | ||
|
||
$this->assertEquals($this->qttxt, $actual); | ||
} | ||
|
||
public function testConvertingToInternalFormat() | ||
{ | ||
$actual = Subtitles::load($this->qttxt, 'txt')->getInternalFormat(); | ||
|
||
$expected = (new Subtitles()) | ||
->add(137.44, 140.375, ['Senator, we\'re making', 'our final approach into Coruscant.']) | ||
->add(3740.476, 3742.501, ['Very good, Lieutenant.']) | ||
->getInternalFormat(); | ||
|
||
$this->assertInternalFormatsEqual($expected, $actual, 0.07); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters