diff --git a/src/Code/Converters/VttConverter.php b/src/Code/Converters/VttConverter.php index 8eeded8..1558aa0 100644 --- a/src/Code/Converters/VttConverter.php +++ b/src/Code/Converters/VttConverter.php @@ -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; @@ -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"; } diff --git a/src/Subtitles.php b/src/Subtitles.php index 3f91395..14dd6bc 100644 --- a/src/Subtitles.php +++ b/src/Subtitles.php @@ -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; diff --git a/tests/formats/VttTest.php b/tests/formats/VttTest.php index 648bccb..ce30cfc 100644 --- a/tests/formats/VttTest.php +++ b/tests/formats/VttTest.php @@ -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);