From 87b674a02d942eb7733743b5c71c9a65e8915153 Mon Sep 17 00:00:00 2001 From: vnali Date: Fri, 25 Aug 2023 07:08:28 +0000 Subject: [PATCH 1/2] Add speakers to internal format --- README.md | 10 +- src/Code/Converters/SrtConverter.php | 60 +++++++++- src/Code/Converters/VttConverter.php | 60 ++++++++-- src/Subtitles.php | 32 +++++- test_helpers/AdditionalAssertionsTrait.php | 25 +++++ tests/files/srt_to_vtt.srt | 19 ++++ tests/files/srt_to_vtt.vtt | 17 +++ tests/files/srt_with_name.srt | 27 +++++ tests/files/vtt2.vtt | 5 + tests/files/vtt_complex.vtt | 24 ++++ tests/files/vtt_to_srt.srt | 19 ++++ tests/files/vtt_to_srt.vtt | 19 ++++ tests/formats/SrtTest.php | 44 ++++++++ tests/formats/VttTest.php | 123 ++++++++++++++++++++- 14 files changed, 463 insertions(+), 21 deletions(-) create mode 100644 tests/files/srt_to_vtt.srt create mode 100644 tests/files/srt_to_vtt.vtt create mode 100644 tests/files/srt_with_name.srt create mode 100644 tests/files/vtt2.vtt create mode 100644 tests/files/vtt_complex.vtt create mode 100644 tests/files/vtt_to_srt.srt create mode 100644 tests/files/vtt_to_srt.vtt diff --git a/README.md b/README.md index ce3c226..13cc83a 100644 --- a/README.md +++ b/README.md @@ -90,10 +90,18 @@ Add subtitles ```php $subtitles->add(0, 5, 'some text'); // from 0, till 5 seconds +// Add text with speakers +// Only VTT and SRT file formats apply speaker to the lines +// For SRT output is like speaker1: some text +// For VRR output is like some text +$subtitles->add(0, 5, [ + 'speaker1' => 'some text', +]); + // Add multiline text $subtitles->add(0, 5, [ 'first line', - 'second line', + 'speaker2' => 'second line', ]); // Add styles to VTT file format diff --git a/src/Code/Converters/SrtConverter.php b/src/Code/Converters/SrtConverter.php index f52e324..2dae8c5 100644 --- a/src/Code/Converters/SrtConverter.php +++ b/src/Code/Converters/SrtConverter.php @@ -35,7 +35,7 @@ public function fileContentToInternalFormat($file_content) $internal_format[$i]['start'] = self::srtTimeToInternal($parts['start'], $next_line); $internal_format[$i]['end'] = self::srtTimeToInternal($parts['end'], $next_line); $internal_format[$i]['lines'] = []; - + $internal_format[$i]['speakers'] = []; // remove number before timestamp if (isset($internal_format[$i - 1])) { @@ -49,7 +49,9 @@ public function fileContentToInternalFormat($file_content) } elseif ($parts['start'] && !$parts['end'] && strpos($line, '-->') !== false) { throw new UserException("Something is wrong with timestamps on this line: " . $line); } elseif ($parts['text']) { - $internal_format[$i]['lines'][] = strip_tags($parts['text']); + list($linePart, $speakerPart) = self::fixLine($parts['text']); + $internal_format[$i]['lines'][] = $linePart; + $internal_format[$i]['speakers'][] = $speakerPart; } if ($parts['start'] && $parts['end']) { @@ -59,7 +61,30 @@ public function fileContentToInternalFormat($file_content) $internal_format = []; // skip words in front of srt subtitle (invalid subtitles) } } - + // Cleanup speakers, for example ['speaker1', null, null] became ['speaker1'] + foreach ($internal_format as $key => $internal_format_parts) { + if (isset($internal_format_parts['speakers'])) { + $hasSpeaker = false; + $emptySpeakersKeys = []; + // Check speakers if they are null + foreach ($internal_format_parts['speakers'] as $speakerKey => $speaker) { + if ($speaker) { + $emptySpeakersKeys = []; + $hasSpeaker = true; + } else { + $emptySpeakersKeys[] = $speakerKey; + } + } + // Remove speakers key for that time if all speakers are empty or remove empty ones + if (!$hasSpeaker) { + unset($internal_format[$key]['speakers']); + } elseif ($emptySpeakersKeys) { + foreach ($emptySpeakersKeys as $emptySpeakerKey) { + unset($internal_format[$key]['speakers'][$emptySpeakerKey]); + } + } + } + } return $internal_format; } @@ -77,7 +102,19 @@ public function internalFormatToFileContent(array $internal_format) $nr = $k + 1; $start = static::internalTimeToSrt($block['start']); $end = static::internalTimeToSrt($block['end']); - $lines = implode("\r\n", $block['lines']); + $lines_array = []; + foreach ($block['lines'] as $key => $line) { + $speaker = ''; + // if speakers is set + if (isset($block['speakers'][$key]) && $block['speakers'][$key]) { + // create speaker: + $speaker = $block['speakers'][$key] . ': '; + $lines_array[] = $speaker . $line; + } else { + $lines_array[] = $line; + } + } + $lines = implode("\r\n", $lines_array); $file_content .= $nr . "\r\n"; $file_content .= $start . ' --> ' . $end . "\r\n"; @@ -132,4 +169,19 @@ public static function internalTimeToSrt($internal_time) return sprintf("%02d:%02d:%02d,%03d", $hours, $minutes, $remaining_seconds, $milliseconds); } + + protected static function fixLine($line) + { + $speaker = null; + // Check if line is in {speaker: line} format + $hasSpeaker = preg_match('/(.*): (.*)/', $line, $matches); + if ($hasSpeaker) { + $speaker = $matches[1]; + $line = $matches[2]; + } + + // html + $line = strip_tags($line); + return array($line, $speaker); + } } diff --git a/src/Code/Converters/VttConverter.php b/src/Code/Converters/VttConverter.php index 7356b5b..d52916a 100644 --- a/src/Code/Converters/VttConverter.php +++ b/src/Code/Converters/VttConverter.php @@ -37,6 +37,7 @@ public function fileContentToInternalFormat($file_content) $internal_format[$i]['start'] = self::vttTimeToInternal($parts['start']); $internal_format[$i]['end'] = self::vttTimeToInternal($parts['end']); $internal_format[$i]['lines'] = []; + $internal_format[$i]['speakers'] = []; // styles preg_match('/((?:\d{1,2}:){1,2}\d{2}\.\d{1,3})\s+-->\s+((?:\d{1,2}:){1,2}\d{2}\.\d{1,3}) *(.*)/', $line, $matches); @@ -54,23 +55,59 @@ public function fileContentToInternalFormat($file_content) } } } elseif ($parts['text']) { - $internal_format[$i]['lines'][] = self::fixLine($parts['text']); + list($linePart, $speakerPart) = self::fixLine($parts['text']); + $internal_format[$i]['lines'][] = $linePart; + $internal_format[$i]['speakers'][] = $speakerPart; } $last_line_was_empty = trim($line) === ''; } - + // cleanup speakers, for example ['speaker1', null, null] to ['speaker1'] + foreach ($internal_format as $key => $internal_format_parts) { + if (isset($internal_format_parts['speakers'])) { + $hasSpeaker = false; + $emptySpeakersKeys = []; + // Check speakers if they are null + foreach ($internal_format_parts['speakers'] as $speakerKey => $speaker) { + if ($speaker) { + $emptySpeakersKeys = []; + $hasSpeaker = true; + } else { + $emptySpeakersKeys[] = $speakerKey; + } + } + // Remove speakers key for that time if all speakers are empty or remove empty ones + if (!$hasSpeaker) { + unset($internal_format[$key]['speakers']); + } elseif ($emptySpeakersKeys) { + foreach ($emptySpeakersKeys as $emptySpeakerKey) { + unset($internal_format[$key]['speakers'][$emptySpeakerKey]); + } + } + } + } return $internal_format; } public function internalFormatToFileContent(array $internal_format) { $file_content = "WEBVTT\r\n\r\n"; - foreach ($internal_format as $k => $block) { $start = static::internalTimeToVtt($block['start']); $end = static::internalTimeToVtt($block['end']); - $lines = implode("\r\n", $block['lines']); + $lines_array = []; + foreach ($block['lines'] as $key => $line) { + $speaker = ''; + // if speakers is set + if (isset($block['speakers'][$key]) && $block['speakers'][$key]) { + // create Line + $speaker = ''; + $lines_array[] = $speaker . $line . ''; + } else { + $lines_array[] = $line; + } + } + $lines = implode("\r\n", $lines_array); $vtt_cue_settings = ''; if (isset($block['vtt_cue_settings'])) { @@ -92,9 +129,9 @@ protected static function vttTimeToInternal($vtt_time) { $corrected_time = str_replace(',', '.', $vtt_time); $parts = explode('.', $corrected_time); - + // parts[0] could be mm:ss or hh:mm:ss format -> always use hh:mm:ss - $parts[0] = substr_count($parts[0], ':') == 2 ? $parts[0] : '00:'.$parts[0]; + $parts[0] = substr_count($parts[0], ':') == 2 ? $parts[0] : '00:' . $parts[0]; $only_seconds = strtotime("1970-01-01 {$parts[0]} UTC"); $milliseconds = (float)('0.' . $parts[1]); @@ -117,15 +154,20 @@ protected static function internalTimeToVtt($internal_time) protected static function fixLine($line) { - // speaker + $speaker = null; + // Remove if (substr($line, 0, 3) == '', ': ', $line); + // Get speaker + $speaker = substr($line, 0, strpos($line, '>')); + // Remove speaker> + $line = str_replace($speaker . '>', '', $line); } // html $line = strip_tags($line); - return $line; + return array($line, $speaker); } } diff --git a/src/Subtitles.php b/src/Subtitles.php index 0eed655..b2977ce 100644 --- a/src/Subtitles.php +++ b/src/Subtitles.php @@ -74,13 +74,40 @@ public function add($start, $end, $text, $settings = []) $internal_format = [ 'start' => $start, 'end' => $end, - 'lines' => is_array($text) ? $text : [$text], + 'lines' => is_array($text) ? array_values($text) : [$text], // array_values removes possible speakers from keys ]; + $speakers = []; + // Check if $text is an array containing speakers + if (is_array($text) && !array_is_list($text)) { + foreach ($text as $key => $textItem) { + $speakers[] = $key; + } + // Cleanup speakers[], remove all empty items at the end of array so ['speaker1', null, null] became ['speaker1'] + $empty_speakers_keys = []; + foreach ($speakers as $key => $speaker) { + if ($speaker && !is_int($speaker)) { + $empty_speakers_keys = []; + } else { + $empty_speakers_keys[] = $key; + } + } + if ($empty_speakers_keys) { + foreach ($empty_speakers_keys as $empty_speaker_key) { + unset($speakers[$empty_speaker_key]); + } + } + } + if (isset($settings['vtt_cue_settings']) && $settings['vtt_cue_settings']) { $internal_format['vtt_cue_settings'] = $settings['vtt_cue_settings']; } + // Finally push speakers to internal format if there is any + if ($speakers) { + $internal_format['speakers'] = $speakers; + } + $this->internal_format[] = $internal_format; $this->sortInternalFormat(); @@ -283,7 +310,6 @@ public static function loadFromString($string, $format = null) $internal_format[$i] = $row; $i++; } - } // fix up to a 60 seconds time overlap @@ -338,4 +364,4 @@ public static function loadFromString($string, $format = null) return $converter; } -} \ No newline at end of file +} diff --git a/test_helpers/AdditionalAssertionsTrait.php b/test_helpers/AdditionalAssertionsTrait.php index ce1fe4b..6b63b49 100644 --- a/test_helpers/AdditionalAssertionsTrait.php +++ b/test_helpers/AdditionalAssertionsTrait.php @@ -18,6 +18,31 @@ public function assertInternalFormatsEqual($expected, $actual, $allowable_error foreach ($expected[$k]['lines'] as $line_k => $line) { $this->assertEquals($line, $actual[$k]['lines'][$line_k]); } + + // We should check if speakers is supported for format first. + if (isset($expected[$k]['speakers']) || isset($actual[$k]['speakers'])) { + // Check if speakers key is available for both expected and actual + $this->assertArrayHasKey('speakers', $expected[$k], "Expected Array doesn't contains 'speakers' as key"); + $this->assertArrayHasKey('speakers', $actual[$k], "Actual Array doesn't contains 'speakers' as key"); + // Compare expected and actual + if (isset($expected[$k]['speakers']) && isset($actual[$k]['speakers'])) { + $this->assertEquals(count($expected[$k]['speakers']), count($actual[$k]['speakers']), 'Speaker count is different'); + foreach ($expected[$k]['speakers'] as $speaker_k => $speaker) { + $this->assertEquals($speaker, $actual[$k]['speakers'][$speaker_k]); + } + } + } + + // We should check if vtt_cue_settings is supported for format first. + if (isset($expected[$k]['vtt_cue_settings']) || isset($actual[$k]['vtt_cue_settings'])) { + // Check if vtt_cue_settings key is available for both expected and actual + $this->assertArrayHasKey('vtt_cue_settings', $expected[$k], "Expected Array doesn't contains 'vtt_cue_settings' as key"); + $this->assertArrayHasKey('vtt_cue_settings', $actual[$k], "Actual Array doesn't contains 'vtt_cue_settings' as key"); + // Compare expected and actual + if (isset($expected[$k]['vtt_cue_settings']) && isset($actual[$k]['vtt_cue_settings'])) { + $this->assertEquals($expected[$k]['vtt_cue_settings'], $actual[$k]['vtt_cue_settings'], 'vtt_cue_settings is different'); + } + } } } diff --git a/tests/files/srt_to_vtt.srt b/tests/files/srt_to_vtt.srt new file mode 100644 index 0000000..16bd448 --- /dev/null +++ b/tests/files/srt_to_vtt.srt @@ -0,0 +1,19 @@ +1 +00:02:17,400 --> 00:02:20,400 +line 1 +speaker2: line 2 + +2 +01:02:20,500 --> 01:02:22,500 +speaker1: line 3 +line 4 + +3 +02:02:20,500 --> 02:02:22,500 +speaker2: line 5 +speaker1: line 6 + +4 +03:02:20,500 --> 03:02:22,500 +line 7 +line 8 \ No newline at end of file diff --git a/tests/files/srt_to_vtt.vtt b/tests/files/srt_to_vtt.vtt new file mode 100644 index 0000000..bcd2089 --- /dev/null +++ b/tests/files/srt_to_vtt.vtt @@ -0,0 +1,17 @@ +WEBVTT + +00:02:17.400 --> 00:02:20.400 +line 1 +line 2 + +01:02:20.500 --> 01:02:22.500 +line 3 +line 4 + +02:02:20.500 --> 02:02:22.500 +line 5 +line 6 + +03:02:20.500 --> 03:02:22.500 +line 7 +line 8 \ No newline at end of file diff --git a/tests/files/srt_with_name.srt b/tests/files/srt_with_name.srt new file mode 100644 index 0000000..84fb4c8 --- /dev/null +++ b/tests/files/srt_with_name.srt @@ -0,0 +1,27 @@ +1 +00:00:09,000 --> 00:00:11,000 +speaker1: Line 1 + +2 +00:00:12,000 --> 00:00:13,000 +Line 2 + +3 +00:00:14,000 --> 00:00:15,000 +speaker1: Line 3 +speaker2: Line 4 + +4 +00:00:16,000 --> 00:00:17,000 +Line 5 +Line 6 + +5 +00:00:18,000 --> 00:00:19,000 +speaker1: Line 7 +Line 8 + +6 +00:00:20,000 --> 00:00:21,000 +Line 9 +speaker2: Line 10 \ No newline at end of file diff --git a/tests/files/vtt2.vtt b/tests/files/vtt2.vtt new file mode 100644 index 0000000..1f651ad --- /dev/null +++ b/tests/files/vtt2.vtt @@ -0,0 +1,5 @@ +WEBVTT + +Cue +00:00:00.400 --> 00:00:00.900 something +a diff --git a/tests/files/vtt_complex.vtt b/tests/files/vtt_complex.vtt new file mode 100644 index 0000000..8f03447 --- /dev/null +++ b/tests/files/vtt_complex.vtt @@ -0,0 +1,24 @@ +WEBVTT + +00:00:09.000 --> 00:00:11.000 +Line 1 + +00:00:12.000 --> 00:00:13.000 +Line 2 + +Cue +00:00:14.000 --> 00:00:15.000 +Line 3 +Line 4 + +00:00:16.000 --> 00:00:17.000 +Line 5 +Line 6 + +00:00:18.000 --> 00:00:19.000 line:0 position:20% size:60% align:start +Line 7 +Line 8 + +00:00:20.000 --> 00:00:21.000 +Line 9 +Line 10 \ No newline at end of file diff --git a/tests/files/vtt_to_srt.srt b/tests/files/vtt_to_srt.srt new file mode 100644 index 0000000..16bd448 --- /dev/null +++ b/tests/files/vtt_to_srt.srt @@ -0,0 +1,19 @@ +1 +00:02:17,400 --> 00:02:20,400 +line 1 +speaker2: line 2 + +2 +01:02:20,500 --> 01:02:22,500 +speaker1: line 3 +line 4 + +3 +02:02:20,500 --> 02:02:22,500 +speaker2: line 5 +speaker1: line 6 + +4 +03:02:20,500 --> 03:02:22,500 +line 7 +line 8 \ No newline at end of file diff --git a/tests/files/vtt_to_srt.vtt b/tests/files/vtt_to_srt.vtt new file mode 100644 index 0000000..7a9a591 --- /dev/null +++ b/tests/files/vtt_to_srt.vtt @@ -0,0 +1,19 @@ +WEBVTT + +When vtt convert to srt, speaker format should be changed and cue should be removed +00:02:17.400 --> 00:02:20.400 +line 1 +line 2 + +01:02:20.500 --> 01:02:22.500 +line 3 +line 4 + +Cue +02:02:20.500 --> 02:02:22.500 +line 5 +line 6 + +03:02:20.500 --> 03:02:22.500 line:0 position:20% size:60% align:start +line 7 +line 8 \ No newline at end of file diff --git a/tests/formats/SrtTest.php b/tests/formats/SrtTest.php index e2bc883..5a9cc50 100644 --- a/tests/formats/SrtTest.php +++ b/tests/formats/SrtTest.php @@ -40,6 +40,50 @@ public function testFileToInternalFormat() $this->assertInternalFormatsEqual(self::generatedSubtitles()->getInternalFormat(), $actual_internal_format); } + public function testFileToInternalFormatComplex() + { + $srt_path = './tests/files/srt_with_name.srt'; + $expected_internal_format = [ + [ + 'start' => 9, + 'end' => 11, + 'lines' => ['Line 1'], + 'speakers' => ['speaker1'], + ], + [ + 'start' => 12, + 'end' => 13, + 'lines' => ['Line 2'], + ], + [ + 'start' => 14, + 'end' => 15, + 'lines' => ['Line 3', 'Line 4'], + 'speakers' => ['speaker1', 'speaker2'], + ], + [ + 'start' => 16, + 'end' => 17, + 'lines' => ['Line 5', 'Line 6'], + ], + [ + 'start' => 18, + 'end' => 19, + 'lines' => ['Line 7', 'Line 8'], + 'speakers' => ['speaker1'], // It should not be ['speaker1', ''] + ], + [ + 'start' => 20, + 'end' => 21, + 'lines' => ['Line 9', 'Line 10'], + 'speakers' => ['', 'speaker2'], + ] + ]; + + $actual_internal_format = Subtitles::loadFromFile($srt_path)->getInternalFormat(); + $this->assertInternalFormatsEqual($expected_internal_format, $actual_internal_format); + } + public function testConvertToFile() { $actual_file_content = self::generatedSubtitles()->content('srt'); diff --git a/tests/formats/VttTest.php b/tests/formats/VttTest.php index 0c67148..e99ddc1 100644 --- a/tests/formats/VttTest.php +++ b/tests/formats/VttTest.php @@ -8,7 +8,8 @@ use PHPUnit\Framework\TestCase; use Helpers\AdditionalAssertionsTrait; -class VttTest extends TestCase { +class VttTest extends TestCase +{ use AdditionalAssertionsTrait; @@ -34,7 +35,16 @@ public function testConvertFromVttToSrt() $expected = (new Subtitles())->loadFromFile($vtt_path)->content('srt'); $actual = file_get_contents($srt_path); + $this->assertStringEqualsStringIgnoringLineEndings($expected, $actual); + } + + public function testConvertFromVttToSrtComplex() + { + $vtt_path = './tests/files/vtt_to_srt.vtt'; + $srt_path = './tests/files/vtt_to_srt.srt'; + $expected = (new Subtitles())->loadFromFile($vtt_path)->content('srt'); + $actual = file_get_contents($srt_path); $this->assertStringEqualsStringIgnoringLineEndings($expected, $actual); } @@ -49,20 +59,92 @@ public function testConvertFromSrtToVtt() $this->assertStringEqualsStringIgnoringLineEndings($expected, $actual); } + public function testConvertFromSrtToVttComplex() + { + $srt_path = './tests/files/srt_to_vtt.srt'; + $vtt_path = './tests/files/srt_to_vtt.vtt'; + + $expected = file_get_contents($vtt_path); + $actual = (new Subtitles())->loadFromFile($srt_path)->content('vtt'); + + $this->assertStringEqualsStringIgnoringLineEndings($expected, $actual); + } + public function testFileToInternalFormat() { $vtt_path = './tests/files/vtt_with_name.vtt'; $expected_internal_format = [[ 'start' => 9, 'end' => 11, - 'lines' => ['Roger Bingham: We are in New York City'], + 'lines' => ['We are in New York City'], + 'speakers' => ['Roger Bingham'], ]]; $actual_internal_format = Subtitles::loadFromFile($vtt_path)->getInternalFormat(); + $this->assertInternalFormatsEqual($expected_internal_format, $actual_internal_format); + } + + public function testFileToInternalFormatComplex() + { + $vtt_path = './tests/files/vtt_complex.vtt'; + $expected_internal_format = [ + [ + 'start' => 9, + 'end' => 11, + 'lines' => ['Line 1'], + 'speakers' => ['speaker1'], + ], + [ + 'start' => 12, + 'end' => 13, + 'lines' => ['Line 2'], + ], + [ + 'start' => 14, + 'end' => 15, + 'lines' => ['Line 3', 'Line 4'], + 'speakers' => ['speaker1', 'speaker2'], + ], + [ + 'start' => 16, + 'end' => 17, + 'lines' => ['Line 5', 'Line 6'], + ], + [ + 'start' => 18, + 'end' => 19, + 'lines' => ['Line 7', 'Line 8'], + // It should not be ['speaker1', ''] + 'speakers' => ['speaker1'], + 'vtt_cue_settings' => 'line:0 position:20% size:60% align:start' + ], + [ + 'start' => 20, + 'end' => 21, + 'lines' => ['Line 9', 'Line 10'], + 'speakers' => ['', 'speaker2'], + ] + ]; + $actual_internal_format = Subtitles::loadFromFile($vtt_path)->getInternalFormat(); $this->assertInternalFormatsEqual($expected_internal_format, $actual_internal_format); } + public function testConvertingFileFromVttToVttDoesNotChangeItContent() + { + $vtt_path = './tests/files/vtt_complex.vtt'; + $temporary_vtt_path = './tests/files/tmp/vtt_complex.vtt'; + + @unlink($temporary_vtt_path); + + Subtitles::convert($vtt_path, $temporary_vtt_path); + $vtt_internal_format = Subtitles::loadFromFile($vtt_path)->getInternalFormat(); + $vtt_tmp_internal_format = Subtitles::loadFromFile($temporary_vtt_path)->getInternalFormat(); + + $this->assertInternalFormatsEqual($vtt_internal_format, $vtt_tmp_internal_format); + unlink($temporary_vtt_path); + } + public function testParsesFileWithCue() { $input_vtt_file_content = <<< TEXT @@ -85,7 +167,40 @@ public function testParsesFileWithCue() $actual = Subtitles::loadFromString($input_vtt_file_content)->getInternalFormat(); $expected = (new Subtitles())->add(0, 1, 'a')->add(1, 2, ['b', 'b'])->add(2, 3, 'c')->getInternalFormat(); + $this->assertInternalFormatsEqual($expected, $actual); + } + + public function testParsesFileWithCueComplex() + { + $input_vtt_file_content = <<< TEXT +WEBVTT +00:00:00.400 --> 00:00:00.900 something +a + +some text allowed in vtt that is not shown +00:00:01.000 --> 00:00:02.000 +b +c + +something +00:00:02.000 --> 00:00:03.000 something +d + e + +00:00:03.000 --> 00:00:04.000 +f +g + +00:00:04.000 --> 00:00:05.000 +h +i + +TEXT; + + $vtt_path = './tests/files/vtt2.vtt'; + $actual = Subtitles::loadFromString($input_vtt_file_content)->getInternalFormat(); + $expected = Subtitles::loadFromFile($vtt_path)->add(1, 2, ['speaker1' => 'b', 'c'])->add(2, 3, ['d', 'speaker2' => 'e'], ['vtt_cue_settings' => 'something'])->add(3, 4, ['f', 'g'])->add(4, 5, ['speaker2' => 'h', 'speaker1' => 'i'])->getInternalFormat(); $this->assertInternalFormatsEqual($expected, $actual); } @@ -111,7 +226,7 @@ public function testParsesFileWithMissingText() $this->assertInternalFormatsEqual($expected, $actual); } - public function testFileContainingMultipleNewLinesBetweenBlocks() + public function testFileContainingMultipleNewLinesBetweenBlocks() { $given = <<< TEXT WEBVTT @@ -300,4 +415,4 @@ public function testParsesIncorrectTimestampWithComma() ->getInternalFormat(); $this->assertEquals($expected, $actual); } -} \ No newline at end of file +} From 557e4760b6117054f1bdc03baf7453e089013115 Mon Sep 17 00:00:00 2001 From: vnali Date: Fri, 25 Aug 2023 07:19:23 +0000 Subject: [PATCH 2/2] Cleanup --- src/Code/Converters/SrtConverter.php | 14 +++++++------- src/Code/Converters/VttConverter.php | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Code/Converters/SrtConverter.php b/src/Code/Converters/SrtConverter.php index 2dae8c5..4a59b58 100644 --- a/src/Code/Converters/SrtConverter.php +++ b/src/Code/Converters/SrtConverter.php @@ -65,22 +65,22 @@ public function fileContentToInternalFormat($file_content) foreach ($internal_format as $key => $internal_format_parts) { if (isset($internal_format_parts['speakers'])) { $hasSpeaker = false; - $emptySpeakersKeys = []; + $empty_speakers_keys = []; // Check speakers if they are null - foreach ($internal_format_parts['speakers'] as $speakerKey => $speaker) { + foreach ($internal_format_parts['speakers'] as $speaker_key => $speaker) { if ($speaker) { - $emptySpeakersKeys = []; + $empty_speakers_keys = []; $hasSpeaker = true; } else { - $emptySpeakersKeys[] = $speakerKey; + $empty_speakers_keys[] = $speaker_key; } } // Remove speakers key for that time if all speakers are empty or remove empty ones if (!$hasSpeaker) { unset($internal_format[$key]['speakers']); - } elseif ($emptySpeakersKeys) { - foreach ($emptySpeakersKeys as $emptySpeakerKey) { - unset($internal_format[$key]['speakers'][$emptySpeakerKey]); + } elseif ($empty_speakers_keys) { + foreach ($empty_speakers_keys as $empty_speaker_key) { + unset($internal_format[$key]['speakers'][$empty_speaker_key]); } } } diff --git a/src/Code/Converters/VttConverter.php b/src/Code/Converters/VttConverter.php index d52916a..3381c42 100644 --- a/src/Code/Converters/VttConverter.php +++ b/src/Code/Converters/VttConverter.php @@ -66,22 +66,22 @@ public function fileContentToInternalFormat($file_content) foreach ($internal_format as $key => $internal_format_parts) { if (isset($internal_format_parts['speakers'])) { $hasSpeaker = false; - $emptySpeakersKeys = []; + $empty_speakers_keys = []; // Check speakers if they are null - foreach ($internal_format_parts['speakers'] as $speakerKey => $speaker) { + foreach ($internal_format_parts['speakers'] as $speaker_key => $speaker) { if ($speaker) { - $emptySpeakersKeys = []; + $empty_speakers_keys = []; $hasSpeaker = true; } else { - $emptySpeakersKeys[] = $speakerKey; + $empty_speakers_keys[] = $speaker_key; } } // Remove speakers key for that time if all speakers are empty or remove empty ones if (!$hasSpeaker) { unset($internal_format[$key]['speakers']); - } elseif ($emptySpeakersKeys) { - foreach ($emptySpeakersKeys as $emptySpeakerKey) { - unset($internal_format[$key]['speakers'][$emptySpeakerKey]); + } elseif ($empty_speakers_keys) { + foreach ($empty_speakers_keys as $empty_speaker_key) { + unset($internal_format[$key]['speakers'][$empty_speaker_key]); } } }