[MPlayer-users] Any tool for converting srt to ssa subtitles?

Loren Merritt lorenm at u.washington.edu
Mon Apr 26 07:23:50 CEST 2004


On Sun, 25 Apr 2004 romildo at uber.com.br wrote:

> Does anybody know of any tool for linux
> for converting a text subtitle to the
> ssa format?

I don't know if there was one before, but I just pasted an SSA template
into my SRT parser, and here you go.

--Loren Merritt
-------------- next part --------------
#!/usr/bin/env perl
# srt2ssa by Loren Merritt

$^W=1;

sub roundtime($) {
    my $t = shift;
    $t =~ tr/,/./;
    $t =~ /^(\d+):(\d\d?):(\d+\.\d*)/;
    $t = int(($1*3600 + $2*60 + $3) * 100 + .5);
    sprintf "%d:%02d:%02d.%02d", $t/360000, ($t/6000)%60, ($t/100)%60, $t%100;
}

undef $/;
while(<>) {

    if($ARGV eq '-') {
	$source = '<unknown>';
    } else {
	($ssa = $ARGV) =~ s/\.srt$/.ssa/i or $ssa .= '.ssa';
	open STDOUT, ">", $ssa or warn "failed to open '$ssa': $!\n" and next;
	$source = $ARGV;
    }
    
    print << "EOT";
[Script Info]
; This is a Sub Station Alpha v4 script.
; For Sub Station Alpha info and downloads,
; go to http://www.eswat.demon.co.uk/
; or email kotus\@eswat.demon.co.uk
Title: <untitled>
Original Script: $source
ScriptType: v4.00
Collisions: Normal
Timer: 100.0000

[V4 Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
Style: Style1,Verdana,24,16777215,16777215,16777215,0,-1,0,1,2,2,2,30,30,25,0,0

[Events]
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
EOT
    
    s/^???//;
    s/\r\n/\n/g;
    foreach (split /\n\s*\n/) {
	/^\d+\s*\n
	0*(\d+:\d\d:\d\d[,.]\d\d\d)\s+-->\s+0*(\d+:\d\d:\d\d[,.]\d\d\d)\s*\n
	(.*)
	/sx or print STDERR "bad srt line:\n$_\n" and next;
	$start = roundtime($1);
	$end = roundtime($2);
	chomp ($text = $3);
	$text =~ s/\n/\\n/g;
	$text =~ s/<([bi])>/{\\${1}1}/g;
	$text =~ s/<\/([bi])>/{\\${1}0}/g;
	print "Dialogue: Marked=0,$start,$end,Style1,,0000,0000,0000,,$text\n";
    }
}



More information about the MPlayer-users mailing list