Cambiar SRT a xml con inicio y duración

Bueno, se me presentaron dos problemas:
1) pasar subtítulos SRT a xml y
2) pasar el inicio – fin de los subs por inicio y duración

Funciona así, uno pone el SRT en el index.html y devuelve un xml. Hubo un problema en windows por lo cual cambie el encoding.

Acá va el código html:

<html>
<body>

<form action="mots_xml_mac.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>

Acá va el código php:

<?php

// script to convert multi-line srt caption files to new-format (02-05-08) tt XML caption files
$use_cdata_tags = false;
$debug_output = true;

// the directory to write the new files in
// it must exist, be writeable, and be outside of the directory that is being scanned
$new_directory = '../temp/';

/////////////////////////////////// no user configuration below this \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// get filename or scan directory if it's a directory
//$filename = (isset($_GET["filename"])) ? strval($_GET["filename"]) : "./";
//$filename = "/var/www/html/c1.spgserver.com/srt";

// read each file into an array
//$it = new RecursiveDirectoryIterator("$filename");

//foreach(new RecursiveIteratorIterator($it) as $file)
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
$file=$_FILES["file"]["tmp_name"];
// debug('Filename', $file); exit;
// debug('Ext', substr(strtolower($file), (strlen($file) - 4), 4));// exit;

// debug - only use test file
// if($file == '.\multi-line_test_file.srt')

// check for .srt extension
// if(substr(strtolower($file), (strlen($file) - 4), 4) == '.srt')
// {
$ttxml = '';
$full_line = '';

if($file_array = file($file))
{
// write tt , head, and div elements for the new file
$ttxml .= "<?xml version="1.0" encoding="UTF-8"?>\n";
$ttxml .= "<tt xml:lang="es" xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling">\n";
$ttxml .= "<head>\n";
$ttxml .= "<styling>\n";
$ttxml .= "<style xml:id="1" style="2" tts:backgroundColor="black" tts:fontFamily="Arial" tts:fontSize="1c" tts:color="white" tts:textAlign="center" tts:fontStyle="Plain" />\n";
$ttxml .= "</styling>\n";
$ttxml .= "</head>\n";
$ttxml .= "<body>\n";
$ttxml .= "<div xml:lang="es">\n";
$fstline=true;
foreach($file_array as $line)
{
$line = rtrim($line);

// debug('Line', $line);

// get begin and end
// 00 : 00 : 32 , 000 --> 00 : 00 : 37 , 000
if(preg_match('/(\d\d):(\d\d):(\d\d),(\d\d\d) --> (\d\d):(\d\d):(\d\d),(\d\d\d)/', $line, $match))
{
$begin = $match[1] . ":" . $match[2] . ":" . $match[3] . "." . $match[4];
$end = $match[5] . ":" . $match[6] . ":" . $match[7] . "." . $match[8];
$begtime = ($match[1] *3600 * 1000) + ($match[2] *60 *1000) + ( $match[3] * 1000 ) + ($match[4] );
$endtime = ($match[5] *3600 * 1000) + ($match[6] *60 *1000) + ( $match[7] * 1000 ) + ($match[8] );
$durtime = $endtime-$begtime;
$hora = floor($durtime / 3600000);
$minutos = floor(($durtime - ($hora*3600000))/60000);
$segundos = floor(($durtime - ($minutos * 60000)) /1000);
$mili = floor(($durtime - ($segundos * 1000)));
$end= str_pad($hora,2,0,STR_PAD_LEFT) .":".str_pad($minutos,2,0,STR_PAD_LEFT).":".str_pad($segundos,2,0,STR_PAD_LEFT).".".str_pad($mili,3,0,STR_PAD_RIGHT);

$full_line = '';
if ($fstline) {
$ttxml .= "\t <p begin="00:00:00.000" dur="".$begin."" style="1"></p>\n";
$fstline=false;
}
}
// if the next line is not blank, get the text
elseif($line != '')
{
if($full_line != '')
{
$full_line .= '<br />' . $line;
}
else
{
$full_line .= $line;
}
}

// if the next line is blank, write the paragraph
if($line == '')
{
// write new paragraph
// <p begin="00:08:01.50" end="00:08:07.50">Nothing is going on.</p>
if($use_cdata_tags)
{
$ttxml .= " <p begin="" . $begin . "" end="" . $end . ""><![CDATA[" . $full_line . "]]></p>\n";
}
else
{
$ttxml .= " <p begin="" . $begin . "" dur="" . $end . "" style="1">" . $full_line . "</p>\n";
}

// debug('Text', $line);
// debug('ttxml', $ttxml); exit;

$full_line = '';
}
}

// write ending tags
$ttxml .= " </div>\n";
$ttxml .= " </body>\n";
$ttxml .= "</tt>\n";

// write new file
// $new_file = $new_directory . substr($file, 0, (strlen($file) - 4)) . '.xml';
// $fh = fopen($new_file, 'w') or die('Can\'t open: ' . $new_file);
//print_r($_FILES);

header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="'.substr($_FILES["file"]["name"],0,(strlen($file) - 4)).".xml".'"');
header('Content-Transfer-Encoding: binary');

// print_r($_FILES['name']);
// print (mb_detect_encoding($ttxml));exit;
//print_r(mb_detect_encoding($ttxml)); exit;
// if (mb_detect_encoding($ttxml)=="UTF-8") {
print($ttxml);
// } else {
// print(iconv('UTF-8', 'Windows-1252', $ttxml));
// }
// fwrite($fh, $ttxml) or die('Can\'t write to: ' . $new_file);
// fclose($fh);
// }
}
}
function debug($title, $value)
{
global $debug_output;

if ($debug_output)
{
print "<pre>";
if (is_array($value))
{
print $title . ":\n";
print_r($value);
}
else
{
print $title . ": " . $value;
}
print "</pre>\n";
}
}

Para que funcione en windows hay que hacer:

< //    print(iconv(‘UTF-8’, ‘Windows-1252’, $ttxml));

>     print(iconv(‘Windows-1252′,’UTF-8’ ,$ttxml));

Esto funciona con el subtitle workshotp y no me acuerdo el que funciona para windows.

Enjoy.

Tags: , , , , , ,

Comments are closed.