Content with Style

Web Technique

PHP Script: Timeshift in Subtitle Files

by Pascal Opitz on September 16 2006, 10:30

Have you ever tried to watch a movie with a .srt file, and the subtitle was more or less out of time? You can use this little PHP script through commandline and fix it by providing an offset.

This obviously is just a quick script, nothing fancy, but i thought I might still share it. Save the file as “subtitle_timeshift.php” and execute it with PHP in the commandline. It will then save your file with the prefix “parse_” so you can check if it works or not.

Enjoy!


<?
if(empty($argv[1]) || empty($argv[2]))
  die('Syntax: php subtitle_timeshift.php filename seconds'. "n");

if(!file_exists($argv[1]))
  die('file not found' . "n");

$fc=file($argv[1]);
$f = fopen('parse_' . $argv[1], 'w+');

foreach($fc as $ln)
{
  if(strpos($ln, ' --> ') !== false)
  {
    $parts = explode(' --> ', $ln);
    $start = substr($parts[0], 0, 8);
    $end = substr($parts[1], 0, 8);

    $start = strtotime("2000-01-01 " . $start) + intval($argv[2]);
    $end = strtotime("2000-01-01 " . $end) + intval($argv[2]);

    $start = date("H:i:s", $start) . substr($parts[0], 8);
    $end = date("H:i:s", $end) . substr($parts[1], 8);

    $ln = $start . ' --> ' . $end;
  }

  fputs($f, $ln);
}

fclose($f);

?>

Comments

  • Cool script, saved me a lot of time… ;-)

    two things to mention:
    $_REQUEST ist only available in the cgi version, not in the cli version of php

    and there is small coding-error. The $end variable ist not correctly set after the offset calculations: $parts [1] should be used, not $parts [0] to construct the value for $end. And then the $ln does not need another ”\n”!

    Regards
    Tom

    by tom on September 16 2006, 09:20 #

  • thnx, taken that into account

    by Pascal Opitz on September 17 2006, 10:57 #