[Home]Srt2ssa

#!/usr/bin/tclsh
#
# Name: srt2ssa.tcl
#
# Usage:
# Pass the base filename of the srt file to convert
# on the commandline.
#
# You may want to modify the header and "dummy" formats.
#
set header "\[Script Info\]
Title: <untitled>
Original Script: <unknown>
ScriptType: v4.00

\[V4 Styles\]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
Style: Default,Tahoma,24,16777215,16777215,16777215,12632256,-1,0,1,1,1,6,30,30,415,0,0

\[Events\]
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"

set dummy "Dialogue: Marked=0,\$start,\$end,Default,NTP,0000,0000,0000,!Effect,\$txt"

proc parsesrt { srt } {
     regsub -all {\n\n} $srt < srt
     set srt [ split $srt < ]
     set subtitles [ list ]
     foreach line $srt {
        set line [ split $line "\n" ]
        set ts [ lindex $line 1 ]
        set txt [ join [ lrange $line 2 end ] \\\N ]
        regsub -all {\s+\\N} $txt \\N txt
        regexp {(\S+)\s+-->\s+(\S+)} $ts -> start end
        lappend subtitles [ list $start $end $txt ] 
     }
    return $subtitles
}

proc buildssa { parsed } {
     set ssa $::header
     foreach line $parsed {
        set start [ lindex $line 0 ]
        regsub {,} $start . start
        set start [ string range $start 0 end-1 ]
        set end   [ lindex $line 1 ]
        regsub {,} $end . end
        set end [ string range $end 0 end-1 ]
        set txt   [ string trim [ lindex $line 2 ] ]
        if { [ string length $txt ] } {
           set newline [ subst -nocommands -nobackslashes $::dummy ]
           append ssa "\n$newline"
        }
     }
     return $ssa 
}

set basename [ lindex $argv 0 ]
set fid [ open $basename.srt r ]
set srt [ read $fid [ file size $basename.srt ] ]
close $fid
set parsed [ parsesrt $srt ]
set ssa [ buildssa $parsed ]
set fid [ open $basename.ssa.out w ]
fconfigure $fid -translation crlf
puts $fid $ssa
close $fid
exit

Transcode Wiki | Recent Changes | Preferences
Password required to edit | View other revisions
Last edited January 12, 2007 6:28 pm by tarazed.ligo.caltech.edu (diff)
Search: