<?php

// 
// (CC-BY) Honza Klokanek Sipek, http://eldar.cz/kangaroo/binarni-sxizofrenie 

if ($argv[1]==NULL) {
echo 
"csv to srt";
echo 
"converts csv with subtitles on stdin to srt subtitles on stdout\n";
echo 
"usage: php $argv[0] filename";
exit;
}


// convert std timecode to miliseconds
function tctomili($tc) {
$tc_a explode (":"$tc);
$ms $tc_a[0].":".$tc_a[1].":".$tc_a[2].",".($tc_a[3]*40);
return 
$ms;

}

ini_set('auto_detect_line_endings',TRUE);

// filename as argument
$file=$argv[1];

$row 1;
if ((
$handle fopen($file"r")) !== FALSE) {
    
    while ((
$data fgetcsv($handle1000",")) !== FALSE) {
        
$num count($data);
        echo (
"$row\n");
        
$row++;

        
$time_start tctomili($data[0]);
        
$time_stop tctomili($data[1]);
        
$subtitle trim(strtr($data[2], "\x0D"" "));
        echo(
$time_start." --> ".$time_stop."\n".$subtitle."\n\n");

    }
    
fclose($handle);
}

?>