Skip to content

Commit

Permalink
Merge pull request #79 from vnali/master
Browse files Browse the repository at this point in the history
Added vtt cue settings to internal format
  • Loading branch information
mantas-done authored Aug 2, 2023
2 parents 55fd696 + efeb9d0 commit d1d9335
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/Code/Converters/VttConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ public function fileContentToInternalFormat($file_content)
$blocks = preg_split("/\n{{$block_lines}}/", $file_content);
$internal_format = [];
foreach ($blocks as $block) {
$found = preg_match('/((?:\d{2}:){1,2}\d{2}\.\d{1,3})\s+-->\s+((?:\d{2}:){1,2}\d{2}\.\d{1,3}).*?\n([\s\S]+)/s', $block, $matches);
$found = preg_match('/((?:\d{2}:){1,2}\d{2}\.\d{1,3})\s+-->\s+((?:\d{2}:){1,2}\d{2}\.\d{1,3})(.*?)\n([\s\S]+)/s', $block, $matches);
if ($found === 0) {
continue;
}

$trimmed_lines = preg_replace("/\n+/", "\n", trim($matches[3]));
$trimmed_lines = preg_replace("/\n+/", "\n", trim($matches[4]));
$lines = explode("\n", $trimmed_lines);
$lines_array = array_map(static::fixLine(), $lines);
$internal_format[] = [
$format = [
'start' => static::vttTimeToInternal($matches[1]),
'end' => static::vttTimeToInternal($matches[2]),
'lines' => $lines_array,
];
if (ltrim($matches[3])) {
$format['vtt_cue_settings'] = ltrim($matches[3]);
}
$internal_format[] = $format;
}

return $internal_format;
Expand All @@ -44,7 +48,11 @@ public function internalFormatToFileContent(array $internal_format)
$end = static::internalTimeToVtt($block['end']);
$lines = implode("\r\n", $block['lines']);

$file_content .= $start . ' --> ' . $end . "\r\n";
$vtt_cue_settings = '';
if (isset($block['vtt_cue_settings'])) {
$vtt_cue_settings = ' ' . $block['vtt_cue_settings'];
}
$file_content .= $start . ' --> ' . $end . $vtt_cue_settings . "\r\n";
$file_content .= $lines . "\r\n";
$file_content .= "\r\n";
}
Expand Down
9 changes: 7 additions & 2 deletions src/Subtitles.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,21 @@ public function save($path, $format = null)
return $this;
}

public function add($start, $end, $text)
public function add($start, $end, $text, $settings = [])
{
// @TODO validation
// @TODO check subtitles to not overlap
$this->internal_format[] = [
$internal_format = [
'start' => $start,
'end' => $end,
'lines' => is_array($text) ? $text : [$text],
];

if (isset($settings['vtt_cue_settings']) && $settings['vtt_cue_settings']) {
$internal_format['vtt_cue_settings'] = $settings['vtt_cue_settings'];
}

$this->internal_format[] = $internal_format;
$this->sortInternalFormat();

return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/formats/VttTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testParsesFileWithStyles()
$actual = (new Subtitles())->loadFromString($given)->getInternalFormat();

$expected = (new Subtitles())
->add(0, 10, 'Hello world.')
->add(0, 10, 'Hello world.', ['vtt_cue_settings' => 'position:50% line:15% align:middle'])
->getInternalFormat();

$this->assertEquals($expected, $actual);
Expand Down

0 comments on commit d1d9335

Please sign in to comment.