Created
November 1, 2014 20:45
-
-
Save fijiwebdesign/3c089d277cdc6bfc12fa to your computer and use it in GitHub Desktop.
Fix the times for each subtitle in a srt subtitle file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds the given seconds to the subtitle file | |
* @author [email protected] | |
*/ | |
if ($argc < 2) { | |
echo "Usage: $argv[0] filename seconds [speed] | |
filename - name of subtitle file to read | |
seconds - amount of seconds to add to the times. If subtracting use negative values. eg: \"-5\" | |
speed - optionally you can add 1 second every [speed] seconds | |
slow - optionally you can minus 1 second every [speed] seconds | |
"; | |
return; | |
} | |
$file = $argv[1]; // subs file | |
$secs = $argv[2]; // time in seconds to add | |
$speed = $argv[3]; // add 1 second each [speed] seconds | |
$slow = $argv[4]; // minus 1 second each [speed] seconds | |
$file2 = preg_replace('/(\.[a-z]+)$/i', ".$secs$1", $file); // file to write subs to | |
echo "Opening subtitle file $file.\n"; | |
echo "Copying subtitle file: $file2.\n"; | |
echo "Adding $secs seconds to each subtitle.\n"; | |
$fp = fopen($file, 'r'); // sub file stream | |
$fp2 = fopen($file2, 'w'); // tmp file stream | |
while($line = fgets($fp)) { // read line from sub file | |
$line = addTimeToLine($line); | |
fputs($fp2, $line); // write line to tmp file | |
} | |
function addTimeToLine($line) | |
{ | |
global $secs, $speed, $slow; | |
$add = 0; | |
if (preg_match_all("/[0-9]+:[0-9]+:[0-9]+/", $line, $matches)) { | |
foreach($matches[0] as $time) { | |
if ($speed) { | |
// @todo implement | |
} | |
$time2 = date('H:i:s', (strtotime($time) + $secs + $add)); | |
$line = str_replace($time, $time2, $line); | |
} | |
} | |
return $line; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eg: To increase times by 5 seconds since subtitles are appearing 5 seconds before person speaks.