PHP Script: Timeshift in Subtitle Files

by Pascal Opitz

published 9 August 2006

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)

?>

Comment

  1. 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
    — tom    16 September, 3:20pm    #
  2. thnx, taken that into account
    Pascal Opitz    17 September, 4:57pm    #

Have your say

If your comment doesn't show up straight away, please don't be offended - we're not censoring, we are just trying to keep out all the viagra and poker ads. Thanks to an explosion of spam all comments are held for manual green-lighting.

name Remember
email
http://
Message <?>

Quick links

Other people's articles that we think you might be interested in: