4 # str_ass: converts srt files in directory into ass files
14 # directory to parse. current is default
16 # --mask anime_#(-m anime_#)
17 # a mask which will be used in files replacement
23 opts = GetoptLong.new(
24 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
25 [ '--directory', '-d', GetoptLong::OPTIONAL_ARGUMENT ],
26 [ '--mask', '-m', GetoptLong::OPTIONAL_ARGUMENT ]
32 opts.each do |opt, arg|
41 mask = arg.to_s.sub("#", '\\\1')
46 ass_header = "[Script Info]
48 Original Script: <unknown>
55 Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
56 Style: Default,Flibustier,22,&H00FFFFFF,&H000000FF,&H0016360E,&H0017460B,0,0,0,0,100,100,0,0,1,1.4,0.6,2,10,10,10,1
59 Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
62 #Style: Default,Comic Sans MS,23,&H00C3FFD2,&H0000FFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,3,1,2,10,10,12,204
65 ass_line = 'Dialogue: 0,\1.\2,\3.\4,Default,,0000,0000,0000,,\5'+"{ENDOFTEXT}\n"+'\6'
66 srt_line = /^\d+\n(\d\d:\d\d:\d\d),(\d\d)\d[\ ]?-->[\ ]?(\d\d:\d\d:\d\d),(\d\d)\d(?: SSA.*)?\n([\s\S]+?)\n(^\d+\n\d\d:\d\d:\d\d)/
70 Dir.foreach(dir) do |f|
71 next if [".", ".."].include?(f) || !f.match(/\.srt$/)
74 File.open(dir+'/'+f, 'r') {|file| data = file.read }
75 data = data.gsub("\r\n", "\n").gsub(/[\s\n]+$/m, '')+"\n0\n00:00:00"
77 while data.match(srt_line) do
78 data.gsub(srt_line) do |entry|
79 tmp = entry.sub(srt_line, ass_line).split("{ENDOFTEXT}")
80 tmp[0] = tmp[0].gsub("\n", "\\N").gsub(/\\N$/, '')
81 new_entry = tmp.join("")
83 data = data.sub(entry, new_entry)
88 new_filename = (dir == "." ? "" : dir)+f.sub(/\.srt$/ ,'').sub(/.*\b(\d{4}|\d{3}|\d{2})\b.*/, mask)
89 new_filename = new_filename+".ass" unless new_filename.match(/\.ass$/)
91 new_filename = dir+'/'+f.sub(/\.srt$/, '.ass')
94 File.open(new_filename, 'w') {|file| file << ass_header << data.gsub(/\n0\n00:00:00$/m, '') }