Total members 11895 |It is currently Sat Dec 21, 2024 5:25 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





PHP srt to xml subtitle converter:
. Its is for playing flv videos online with subtitles. Flv players do not support .srt (divx subtitles) well. So this code will automatically convert the .srt file to an xml file.
Code:

<?php

// script to convert multi-line srt caption files to new-format (02-05-08) tt XML caption files
$use_cdata_tags = true;
$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"]) : "./";

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

foreach(
new RecursiveIteratorIterator($it) as $file)
{

// 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\"?>";
      $ttxml .= "<tt xml:lang='en' xmlns='http://www.w3.org/2006/10/ttaf1' xmlns:tts='http://www.w3.org/2006/10/ttaf1#style'>\n";
      $ttxml .= "  <head>\n";
      $ttxml .= "  </head>\n";
      $ttxml .= "  <body>\n";
      $ttxml .= "    <div xml:id=\"captions\">\n";

      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];
          $full_line = '';
        }
        // 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 . "\" end=\"" . $end . "\">" . $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);
      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";
  }
}

?>


A way to use the subtitle in jwplayer

Code:
    
<script type="text/javascript">
        var 
s1 = new SWFObject("player.swf","ply","328","200","9","#FFFFFF");
        
s1.addParam("allowfullscreen","true");
        
s1.addParam("allowscriptaccess","always");
        
s1.addParam("flashvars","file=bunny.flv&captions=subtitles/bunny.xml");
        
s1.write("container");
    
</script>


You will need swfobject.js for the code which can be found here:
Code:
http://blog.deconcept.com/swfobject/swfobject.zip


Next time I'll publish the code for multilangual subtitle selection for video streaming. Hope you like it.




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

thank you so much . :wave:

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

A million thanks for posting this inofrmaiotn.


Author:
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : PHP srt to xml subtitle converter
 C to C++ converter     -  
 PST Converter Pro     -  
 need help doing Universal Converter     -  
 Number system converter (C++)     -  
 infix to postfix converter     -  
 The Digital to Analogue converter DAC writer     -  



Topic Tags

PHP XML
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com