Skip to content

Commit

Permalink
Merge pull request #82 from Andrius521/master
Browse files Browse the repository at this point in the history
Improve lrc parsing and some fixes
  • Loading branch information
mantas-done authored Aug 14, 2023
2 parents 21f1e8e + 000f2c9 commit 2610635
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
29 changes: 18 additions & 11 deletions src/Code/Converters/LrcConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,44 @@

class LrcConverter implements ConverterContract
{
protected static $regexp = '/\[\s*(\d{2}:\d{2}(?:[:.]\d{1,3})?)\s*](.*)/s';
protected static $regexp = '/\[\s*(\d{2}:\d{2}(?:[:.]\d{1,3})?)\s*]/';
protected static $time_offset_regexp = '/\[offset:\s*\+?(-?\d+)\s*]/s';

public function canParseFileContent($file_content)
{
return preg_match(self::$regexp, $file_content) === 1;
return preg_match(self::$regexp . 's', $file_content) === 1;
}

public function fileContentToInternalFormat($file_content)
{
$timestamp_offset = self::timestampsOffset($file_content);
$lines = explode("\n", $file_content);
$internal_format = [];
$i = 0;
foreach ($lines as $line) {
$found = preg_match(self::$regexp, $line, $match);
$found = preg_match_all(self::$regexp, $line, $timestamps);
if ($found === 0) {
continue;
}

$internal_format[$i] = [
'start' => static::lrcTimeToInternal($match[1], $timestamp_offset),
'end' => null,
'lines' => [trim($match[2])]
];
$text = str_replace($timestamps[0], '', $line);

foreach ($timestamps[1] as $timestamp) {
$internal_format[] = [
'start' => static::lrcTimeToInternal($timestamp, $timestamp_offset),
'end' => null,
'lines' => [trim($text)]
];
}
}

usort($internal_format, function ($a, $b) {
return $a['start'] <=> $b['start'];
});

for ($i = 0; $i < count($internal_format); $i++) {
if (isset($internal_format[$i - 1]) && $internal_format[$i - 1]['end'] === null) {
$internal_format[$i - 1]['end'] = $internal_format[$i]['start'];
}

$i++;
}

//TODO: Currently last line's end time is start + 1sec, but it might be calculated differently
Expand Down
18 changes: 10 additions & 8 deletions src/Code/Converters/TxtConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ public static function fillStartAndEndTimes(array $internal_format)
// sort times
usort($internal_format, function ($a, $b) {
if ($a['start'] === $b['start']) {
return $a['end'] > $b['end'];
return $a['end'] <=> $b['end'];
}
return $a['start'] > $b['start'];
return $a['start'] <=> $b['start'];
});

// fill ends
Expand Down Expand Up @@ -359,12 +359,14 @@ private static function timestampsFromLine(string $line)
if (isset($timestamps[0][1])) {
$result['end'] = $timestamps[0][1];
}
$text_before_timestamp = substr($line, 0, strpos($line, $result['start']));
if ($result['start'] && self::hasText($text_before_timestamp)) {
$result = [
'start' => null,
'end' => null,
];
if ($result['start']) {
$text_before_timestamp = substr($line, 0, strpos($line, $result['start']));
if (self::hasText($text_before_timestamp)) {
$result = [
'start' => null,
'end' => null,
];
}
}

return $result;
Expand Down
19 changes: 19 additions & 0 deletions tests/formats/LrcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,23 @@ public function testParseLrcWithNegativeTimeOffset()

$this->assertEquals($expected, $actual);
}

public function testParseGroupedLines()
{
$given = <<< TEXT
[00:01.10] First
[00:02.20][00:05.00] [grouped]
[00:03:25] Third
TEXT;
$actual = (new Subtitles())->loadFromString($given)->getInternalFormat();

$expected = (new Subtitles())
->add(1.1, 2.2, 'First')
->add(2.2, 3.25, '[grouped]')
->add(3.25, 5, 'Third')
->add(5, 6, '[grouped]')
->getInternalFormat();

$this->assertEquals($expected, $actual);
}
}

0 comments on commit 2610635

Please sign in to comment.