Skip to content

Commit

Permalink
Add ability to pass parameters to exporter (fps option to scc)
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Feb 5, 2024
1 parent 77e0f26 commit da74e3d
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Code/Converters/AssConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '[Script Info]
; This is an Advanced Sub Station Alpha v4+ script.
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/ConverterContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format);
public function internalFormatToFileContent(array $internal_format, array $output_settings);

}
2 changes: 1 addition & 1 deletion src/Code/Converters/CsvConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$data = [['Start', 'End', 'Text']];
foreach ($internal_format as $k => $block) {
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/DfxpConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return (new TtmlConverter())->fileContentToInternalFormat($file_content, '');
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tt xmlns:tt="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:tts="http://www.w3.org/ns/ttml#styling" ttp:tickRate="10000000" ttp:timeBase="media" xmlns="http://www.w3.org/ns/ttml">
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/EbuStlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
throw new \Exception('not implemented');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/LrcConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';
foreach ($internal_format as $i => $block) {
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/SbvConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';

Expand Down
47 changes: 32 additions & 15 deletions src/Code/Converters/SccConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

class SccConverter implements ConverterContract
{
private static $fps = 29.97;
private static $valid_fpses = [
29.97,
23.976,
];

public function canParseFileContent($file_content)
{
Expand All @@ -28,14 +31,19 @@ public function canParseFileContent($file_content)
*/
public function fileContentToInternalFormat($file_content, $original_file_content)
{
$fps = 29.97;
if (!in_array($fps, self::$valid_fpses)) {
throw new \Exception('invalid fps ' . $fps);
}

preg_match_all('/^(\d{2}:\d{2}:\d{2}[;:]\d{2})\s+(.*)$/m', $file_content, $matches, PREG_SET_ORDER);
$parsed = [];
foreach ($matches as $match) {
$time = $match[1];
$data = $match[2];

$parsed[] = [
'time' => self::sccTimeToInternal($time, self::codesToBytes($data)),
'time' => self::sccTimeToInternal($time, self::codesToBytes($data), $fps),
'lines' => self::sccToLines($data),
];
}
Expand Down Expand Up @@ -70,8 +78,17 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format, array $output_settings)
{
if (isset($output_settings['fps'])) {
$fps = $output_settings['fps'];
} else {
$fps = 29.97;
}
if (!in_array($fps, self::$valid_fpses)) {
throw new \Exception('invalid fps ' . $fps);
}

$file_content = "Scenarist_SCC V1.0\r\n\r\n";

$last_end_time = 0;
Expand All @@ -80,7 +97,7 @@ public function internalFormatToFileContent(array $internal_format)
$lines = self::splitLongLines($scc['lines']); // max 32 characters per line, max 4 lines

$time_available = $scc['start'] - $last_end_time;
$frames_available = floor(self::$fps * $time_available);
$frames_available = floor($fps * $time_available);
$blocks_available = ($frames_available - $frames_available % 2) / 2;
if ($blocks_available < 8) { // 16 - 94ae 94ae 9420 9420 and 942f 942f (start, end)
unset($internal_format[$k]); // to little time to show something
Expand All @@ -93,16 +110,16 @@ public function internalFormatToFileContent(array $internal_format)
$codes = implode(' ', $codes_array);
$full_codes = "94ae 94ae 9420 9420 $codes 942f 942f";
$frames_to_send = substr_count($full_codes, ' ') + 1;
$time_to_send = $frames_to_send / self::$fps;
$file_content .= self::internalTimeToScc($scc['start'] - $time_to_send, 0) . "\t" . $full_codes . "\r\n\r\n";
$time_to_send = $frames_to_send / $fps;
$file_content .= self::internalTimeToScc($scc['start'] - $time_to_send, 0, $fps) . "\t" . $full_codes . "\r\n\r\n";
if ($last_internal_format !== null && ($frames_to_send + 4) < $frames_available) {
self::internalTimeToScc($last_internal_format['end'], 0) . "\t" . '942c 942c' . "\r\n\r\n";
self::internalTimeToScc($last_internal_format['end'], 0, $fps) . "\t" . '942c 942c' . "\r\n\r\n";
}
$last_end_time = $scc['start'];
$last_internal_format = $scc;
}
if (isset($scc)) { // stop last caption
$file_content .= self::internalTimeToScc($scc['end'] - (4 / self::$fps), 0) . "\t" . '942c 942c' . "\r\n\r\n";
$file_content .= self::internalTimeToScc($scc['end'] - (4 / $fps), 0, $fps) . "\t" . '942c 942c' . "\r\n\r\n";
}
return $file_content;
}
Expand All @@ -117,12 +134,12 @@ public function internalFormatToFileContent(array $internal_format)
*
* @return float
*/
public static function sccTimeToInternal($scc_time, $text_bytes)
public static function sccTimeToInternal($scc_time, $text_bytes, $fps)
{
$tmp = str_replace(';', ':', $scc_time);
$parts = explode(':', $tmp);
$time = $parts[0] * 3600 + $parts[1] * 60 + $parts[2] + $parts[3] / self::$fps;
$time += ($text_bytes / 2) / self::$fps;
$time = $parts[0] * 3600 + $parts[1] * 60 + $parts[2] + $parts[3] / $fps;
$time += ($text_bytes / 2) / $fps;

if (strstr($scc_time, ';') !== false) {
// drop frame
Expand All @@ -141,14 +158,14 @@ public static function sccTimeToInternal($scc_time, $text_bytes)
*
* @return string
*/
public static function internalTimeToScc($internal_time, $text_bytes)
public static function internalTimeToScc($internal_time, $text_bytes, $fps)
{
$time = $internal_time - ($text_bytes / 2) / self::$fps;
$time = $internal_time - ($text_bytes / 2) / $fps;
$parts = explode('.', $time);
$whole = (int) $parts[0];
$decimal = isset($parts[1]) ? (float)('0.' . $parts[1]) : 0.0;
$frame = round($decimal * self::$fps);
$frame = min($frame, floor(self::$fps)); // max 29
$frame = round($decimal * $fps);
$frame = min($frame, floor($fps)); // max 29

$srt_time = gmdate("H:i:s", floor($whole)) . ';' . sprintf("%02d", $frame);

Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/SmiConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '<SAMI>
<HEAD>
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/SrtConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/StlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$stl = '';
foreach ($internal_format as $row) {
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/SubMicroDvdConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/SubViewerConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$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#styling" xml:lang="en" xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/TxtConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function fillStartAndEndTimes(array $internal_format)
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '';

Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/TxtQuickTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
* @param array $internal_format Internal format
* @return string Converted file content
*/
public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = '{QTtext} {font:Tahoma}
{plain} {size:20}
Expand Down
2 changes: 1 addition & 1 deletion src/Code/Converters/VttConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function fileContentToInternalFormat($file_content, $original_file_conten
return $internal_format;
}

public function internalFormatToFileContent(array $internal_format)
public function internalFormatToFileContent(array $internal_format , array $options)
{
$file_content = "WEBVTT\r\n\r\n";

Expand Down
21 changes: 13 additions & 8 deletions src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,27 @@ public static function convert($from_file_path, $to_file_path, $options = [])
{
$output_format = null;
if (isset($options['output_format'])) {
$output_format = $options['output_format'];
// do nothing
}
if (isset($options['fps'])) {
// do nothing
}
$strict = true;
if (isset($options['strict']) && $options['strict'] == false) {
$strict = (bool)$options['strict'];
unset($options['strict']);
}
static::loadFromFile($from_file_path, $strict)->save($to_file_path, $output_format);
static::loadFromFile($from_file_path, $strict)->save($to_file_path, $options);
}

public function save($path, $format = null)
public function save($path, $options = [])
{
$file_extension = Helpers::fileExtension($path);
if ($format === null) {
$format = $file_extension;
$format = $file_extension;
if (isset($options['output_format'])) {
$format = $options['output_format'];
}
$content = $this->content($format);
$content = $this->content($format, $options);

file_put_contents($path, $content);

Expand Down Expand Up @@ -145,10 +150,10 @@ public function shiftTimeGradually($seconds, $from = 0, $till = null)
return $this;
}

public function content($format)
public function content($format, $options = [])
{
$converter = Helpers::getConverterByFormat($format);
$content = $converter->internalFormatToFileContent($this->internal_format);
$content = $converter->internalFormatToFileContent($this->internal_format, $options);

return $content;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/formats/SccTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,37 @@ public function testLastSubtitleEndTimeCorrection()

public function testConvertsNonDropFrameTime()
{
$actual = SccConverter::sccTimeToInternal('00:59:56:12', 0);
$actual = SccConverter::sccTimeToInternal('00:59:56:12', 0, 29.97);
$this->assertEqualsWithDelta(3600.0, $actual, 0.01);
}

public function testConvertsNonDropFrameTimeWithText()
{
$actual = SccConverter::sccTimeToInternal('00:59:56:12', 30);
$actual = SccConverter::sccTimeToInternal('00:59:56:12', 30, 29.97);
$this->assertEqualsWithDelta(3600.5, $actual, 0.01);
}

public function testConvertsDropFrameTime()
{
$actual = SccConverter::sccTimeToInternal('01:00:00;00', 0);
$actual = SccConverter::sccTimeToInternal('01:00:00;00', 0, 29.97);
$this->assertEquals(3600.0, $actual);
}

public function testConvertsDropFrameTimeWithText()
{
$actual = SccConverter::sccTimeToInternal('01:00:00;00', 30);
$actual = SccConverter::sccTimeToInternal('01:00:00;00', 30, 29.97);
$this->assertEqualsWithDelta(3600.5, $actual, 0.001);
}

public function testInternalTimeToScc()
{
$actual = SccConverter::internalTimeToScc(3600, 0);
$actual = SccConverter::internalTimeToScc(3600, 0, 29.97);
$this->assertEquals('01:00:00;00', $actual, 0.001);
}

public function testInternalTimeToSccTimeWithText()
{
$actual = SccConverter::internalTimeToScc(3600, 30);
$actual = SccConverter::internalTimeToScc(3600, 30, 29.97);
$this->assertEquals('00:59:59;15', $actual, 0.001);
}

Expand Down

0 comments on commit da74e3d

Please sign in to comment.