i was trying to burn a DVD with some xvid avi videos and their srt-subtitles on my mac. Since i have access to DVD Studio Pro i thought i’ll give it a try. Although importing the videos worked just fine i had Problems with the Subtitle format. All subs where in the common SRT Format (SubRip), but DVD Studio Pro only supports the STL -format (from Spruce Technologies).

So i was looking for a converter that converts „srt 2 stl“ and ran across this thread in a forum: http://www.d-addicts.com/forum/viewtopic_33089.htm . It points to a piece of software called „Subtitler“ so i downloaded and tried it but didnt succeed. 

When i realized that both formats where just plain text and still didnt find a working solution for my Macintosh i fired up my favorite Editor (TextMate) and wrote some lines of PHP to convert the SRT titles. http://lern.hfbk-hamburg.de/dvdtutorial/6_4_untertitel.html was a good starting point in understanding the STL Subtitle Format, which is pretty Simple.

$FontName = Arial
$FontSize = 65
//Die erste Szene
00:00:12:04, 00:00:14:12, Hier passiert etwas.
00:00:16:14, 00:00:19:08, Man mag es kaum glauben.


PHP code to convert SRT Subtitles to STL

//Load the SRT Subtitles into one String
$srtInput = file_get_contents("subtitle-input-file.srt");
//Regular Expression to parse SRT File
//It Assumes the Following Structure per "Subtitle"
656                                 //Number of the Subtitle -> Discarded in our Target Format But used in our RegExp
00:40:10,300 --> 00:40:11,800       //Timecode Start --> End  | HH:MM:SS,sss
Ich bin hier, um Ihnen              //One or...
die Wahrheit zu sagen.              //multiple Lines,
                                    //ending with an empty line

// And yes you are right, everything could be done with just one regular Expression that takes care of the hole converting stuff. But not many like to read and understand RegExp's so i splitted it up and did some looping.
$regExp = "#[0-9]{1,5}\n([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2}),([0-9]{3})\n((.+\n)*)#";
//Squeeze out the Subs
$results = preg_match_all($regExp,$srtInput, $matches);
//This Array will store the STL Output Lines
$subtitles = array();
//Loop through all Subs
for ($i=0; $i < count($matches[1]) ; $i++) {
    //Tidy up the Text
    $cleanText = trim($matches[5][$i]);
    //Remove line Break
    $cleanText = str_replace("\n", ' ', $cleanText);
    //Replace Italic Tags with "-"
    $regExp = "#</?i>#";
    $cleanText = preg_replace($regExp, '-', $cleanText);
    $subtitles[] = array(
        //Milliseconds ahve a different format in STL so we convert the 3 digit SRT to a 2 digit STL format.
        //we Round the result and add Zeroes to numbers smaller than 10
        "start" => $matches[1][$i].':'.str_pad(round($matches[2][$i]/500*30,0),2,"0"),
        "end" => $matches[3][$i].':'.str_pad(round($matches[4][$i]/500*30,0),2,"0"),
        "text" => $cleanText
//Now we create the Output Lines
//This is done seperately so we can add encoding or adapt later changes in the format.
//Known Problems:
    DVD Studio Pro wants the Subtitles
    a) to be in chronological order
    b) not to overlap
    A solution *could* be added here to parse all Timcodes, Sort them and remove Overlaps.
    But this is considered "DWIS-CODE" (Did what it should)

$linesOut = array();
foreach ($subtitles as $sub) {
    // This gives something like
    //start, end, text
    //and works fine with STL.
    $linesOut[] = implode(", ", $sub);
//Output the Lines
echo implode("\n", $linesOut);
//To make everything work with Special Chars i had to save the output as UTF-16 Little Endian,
//Before Importing it to DVD Studio Pro 2
//Either PHP Support for UTF-16 is very limited or my ability to search the internet is.
//So i just used TextMate to do the Job.
Getagged mit

4 Responses to Converting SRT Subtitles to STL for DVD Studio Pro

  1. francisco sagt:

    Dear Paul, I’m sorry that my question is probably ‚too‘ simple. I work with DVD Studio Pro and had the same issue as you (how to covert srt to stl subtitles). I found your page but I have no idea about php. So I was wondering if the code that you have there can be pasted on a dreamweaver page for example and save as .php and would work if I open it with Firefox? or maybe you could help me realize how I can use your code so I can convert from srt to stl, please. sorry! all the best Francisco Mazza

  2. Michael sagt:

    Hi Paul, thanks for sharing! I have one question, why do you remove the in subtitles? Does stl not recognise line breaks? I usually make 2 lines of subtitles… and try to put the line break in a meaningful position… Cheers, Michael

  3. Michael sagt:

    Hi Paul, your script has proved pretty useful, thanks for sharing. I want to point you to a bug though which has caused me a few sleepless nights, as my stl files wouldn’t import properly into dvd studio pro. There were always more than a handful random lines missing… This was the problem: this two bits [$i]/500*30,0),2,0), [$i]/500*30,0),2,0), had to be replaced by [$i]/1000*25,0),2,0,STR_PAD_LEFT), [$i]/1000*25,0),2,0,STR_PAD_LEFT), to reflect the SMPTE timeformat of stl. Apparently it’s not milliseconds but frames… the STR_PAD_LEFT was necessary to have .040 converted to 01 instead of 10. I hope you can adapt my changes on your script as it is very useful and other people my find it useful too. Cheers, Michael

  4. Henry Pantowski sagt:

    Sorry to see that Paul has not answered questions such as those made by francisco. Would be great knowing, specially for us that don’t know anything about TextMate, if this could work, as suggested, in dreamweaver…


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *