Skip to content

Instantly share code, notes, and snippets.

@withmorten
Last active August 21, 2016 13:58
Show Gist options
  • Save withmorten/11b22a271c84ced9231e80be5ae243d3 to your computer and use it in GitHub Desktop.
Save withmorten/11b22a271c84ced9231e80be5ae243d3 to your computer and use it in GitHub Desktop.
merges duplicate lines in a subtitle file that are meant to be one with proper timings
<?php
define('SRT_STATE_SUBNUMBER', 0);
define('SRT_STATE_TIME', 1);
define('SRT_STATE_TEXT', 2);
define('SRT_STATE_BLANK', 3);
function parseSrt($lines) {
$subs = array();
$state = SRT_STATE_SUBNUMBER;
$subNum = 0;
$subText = '';
$subTime = '';
foreach($lines as $line) {
switch($state) {
case SRT_STATE_SUBNUMBER:
$subNum = trim($line);
$state = SRT_STATE_TIME;
break;
case SRT_STATE_TIME:
$subTime = trim($line);
$state = SRT_STATE_TEXT;
break;
case SRT_STATE_TEXT:
if (trim($line) == '') {
$sub = new stdClass;
$sub->number = $subNum;
list($sub->startTime, $sub->stopTime) = explode(' --> ', $subTime);
$sub->text = $subText;
$subText = '';
$state = SRT_STATE_SUBNUMBER;
$subs[] = $sub;
} else {
$subText .= $line;
}
break;
}
}
return $subs;
}
$input = file($argv[1]);
$input_srt = parseSrt($input);
$input_lines = count($input_srt);
$sametext = 0;
$offset = 0;
for($i = 0; $i < $input_lines; $i++) {
if($i+1 < $input_lines) {
if($input_srt[$i]->text == $input_srt[$i+1]->text) {
$sametext = 1;
$offset++;
} else {
$sametext = 0;
}
}
if($sametext === 0) {
echo $input_srt[$i]->number."\r\n";
echo $input_srt[$i-$offset]->startTime." --> ".$input_srt[$i]->stopTime."\r\n";
echo $input_srt[$i]->text."\r\n";
$offset = 0;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment