Skip to content

Commit

Permalink
Improve Ttml timestamp parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-done committed Jan 16, 2024
1 parent 29e4317 commit 93c9273
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Code/Converters/TtmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,14 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate)
throw new UserException("Timestamps were not found in this file (TtmlConverter)");
}

if (substr($ttml_time, -1) === 't') { // if last symbol is "t"
// parses 340400000t
if (substr($ttml_time, -1) === 't') { // 340400000t
return substr($ttml_time, 0, -1) / 10000000;
} elseif (substr($ttml_time, -1) === 's') {
} elseif (substr($ttml_time, -1) === 's') { // 1234s
return rtrim($ttml_time, 's');
} elseif (substr($ttml_time, -1) === 'f' && $frame_rate) {
} elseif (substr($ttml_time, -1) === 'f' && $frame_rate) { // 24f
$seconds = rtrim($ttml_time, 'f');
return $seconds / $frame_rate;
} elseif (preg_match('/(\d{2}):(\d{2}):(\d{2}):(\d{3})/', $ttml_time, $matches)) {
} elseif (preg_match('/(\d{2}):(\d{2}):(\d{2}):(\d{3})/', $ttml_time, $matches)) { // 00:00:00:000
$hours = intval($matches[1]);
$minutes = intval($matches[2]);
$seconds = intval($matches[3]);
Expand All @@ -178,7 +177,7 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate)
$totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds + ($milliseconds / 1000);

return $totalSeconds;
} elseif (preg_match('/(\d{2}):(\d{2}):(\d{2}):(\d{2})/', $ttml_time, $matches)) {
} elseif (preg_match('/(\d{2}):(\d{2}):(\d{2}):(\d{2})/', $ttml_time, $matches)) { // 00:00:00:00
$hours = intval($matches[1]);
$minutes = intval($matches[2]);
$seconds = intval($matches[3]);
Expand All @@ -187,7 +186,7 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate)
$totalSeconds = ($hours * 3600) + ($minutes * 60) + $seconds + $frames / $frame_rate;

return $totalSeconds;
} elseif (is_numeric($ttml_time)) {
} elseif (is_numeric($ttml_time)) { // 12345
return $ttml_time / 1000;
} else {
$time_parts = explode('.', $ttml_time);
Expand All @@ -196,7 +195,20 @@ public static function ttmlTimeToInternal($ttml_time, $frame_rate)
$milliseconds = (float) ('0.' . $time_parts[1]);
}

list($hours, $minutes, $seconds) = array_map('intval', explode(':', $time_parts[0]));
$values = array_map('intval', explode(':', $time_parts[0]));
$hours = 0;
$minutes = 0;

$count = count($values);
$seconds = $values[$count - 1];

if (isset($values[$count - 2])) {
$minutes = $values[$count - 2];
}
if (isset($values[$count - 3])) {
$hours = $values[$count - 3];
}

return ($hours * 3600) + ($minutes * 60) + $seconds + $milliseconds;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/formats/TtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static function timeFormatProvider()
['00:00:5.100', 5.1, null],
['55s', 55, null],
['8500', 8.5, null],
['03:45.702', 225.702, null],
];
}

Expand Down

0 comments on commit 93c9273

Please sign in to comment.