Skip to content

Instantly share code, notes, and snippets.

@donatj
Last active July 10, 2020 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donatj/170403c5105cdf11539f8e0037856706 to your computer and use it in GitHub Desktop.
Save donatj/170403c5105cdf11539f8e0037856706 to your computer and use it in GitHub Desktop.
Crappy .srt helper
#!/usr/bin/env php
<?php
date_default_timezone_set('UTC');
$stderr = fopen('php://stderr', 'wb');
$shift = 0.0;
switch( $argv[1] ?? '' ) {
case 'shift':
if( $argc !== 4 ) {
helpExit('invalid argument count');
}
if( preg_match('/^(?<time1>[-+]?\d\d:\d\d:\d\d)(?:,(?<frac1>\d\d\d))?$/', $argv[2], $regs) ) {
$shift = parseTimeString($regs['time1'], $regs['frac1'] ?? '000');
} else {
helpExit('invalid shift argument');
}
$string = @file_get_contents($argv[3]);
if( $string === false ) {
helpExit('failed to read file "' . $argv[3] . '"');
}
break;
case 'reindex':
$string = '';
for( $i = 2; $i <= $argc - 1; $i++ ) {
$string .= @file_get_contents($argv[$i]);
if( $string === false ) {
helpExit('failed to read file "' . $argv[$i] . '"');
}
$string .= "\n\n";
}
break;
default:
helpExit();
}
$string = preg_replace('/(?<!\r)\n/s', "\r\n", $string);
preg_match_all('/\d+\r\n
(?<time1>\d\d:\d\d:\d\d),(?<frac1>\d\d\d)\s
-->\s
(?<time2>\d\d:\d\d:\d\d),(?<frac2>\d\d\d)\r\n
(?<text>.+?)(?:\r\n\r|$)/sx', $string, $result, PREG_PATTERN_ORDER);
if(preg_last_error() !== PREG_NO_ERROR) {
output("regex error\n");
exit(1);
}
if(empty($result[0])) {
output("no subtitles found\n");
exit(2);
}
$subs = [];
foreach( $result['text'] as $i => $text ) {
$start = parseTimeString($result['time1'][$i], $result['frac1'][$i]);
$finish = parseTimeString($result['time2'][$i], $result['frac2'][$i]);
$start += $shift;
$finish += $shift;
$subs[] = [$start, $finish, trim($result['text'][$i])];
}
usort($subs, function(array $a, array $b) {
return $a <=> $b;
});
foreach($subs as $i => $sub) {
echo sprintf("%d\r\n%s --> %s\r\n%s\r\n\r\n",
$i + 1,
createTimeString($sub[0]),
createTimeString($sub[1]),
$sub[2]
);
}
if( $shift ) {
output("\nshifted by " . createTimeString($shift) . "\n");
}
function parseTimeString( string $time, int $frac ) : float {
$sign = 1;
if( ($time[0] ?? '') === '-' ) {
$sign = -1;
}
$time = ltrim($time, '-+');
return $sign * (strtotime("January 1 1970 {$time}") + ($frac / 1000));
}
function createTimeString( float $secs ) : string {
$pre = '';
if( $secs < 0 ) {
$pre = '-';
}
$secs = (float)abs($secs);
$base = (int)$secs;
$frac = round(($secs - $base) * 1000);
return sprintf('%s%s,%03d', $pre, gmdate('H:i:s', $base), $frac);
}
function helpExit( string $error = '' ) {
global $argv;
if( $error ) {
output(sprintf("error: %s\n\n", $error));
}
output(sprintf(<<<HELP
%s <command> [<args>]
shift [+-]00:00:00[,000] <file>
time shift an srt file by the given seconds
reindex <file ...>
reindex one or more srt files
HELP
, $argv[0]));
die(1);
}
function output( string $str ) {
global $stderr;
fwrite($stderr, $str);
}
function drop( ...$args ) {
foreach( $args as $arg ) {
var_export($arg);
echo "\n===\n";
}
exit(3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment