-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrt2ass.rb
78 lines (67 loc) · 1.92 KB
/
srt2ass.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/ruby
require "pp"
$KCODE = "u"
# ass data
$ass_header = <<ASS
[Script Info]
Title: <untitled>
Original Script: <unknown>
Script Type: v4.00+
PlayResX: 0
PlayResY: 0
PlayDepth: 0
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: English,Arial,10,&H00FFFFFF,&H000000FF,&H0016360E,&H0017460B,0,0,0,0,100,100,0,0,1,1.4,0.6,8,10,10,0,1
Style: Japanese,MS Gothic,10,&H00FFFFFF,&H000000FF,&H0016360E,&H0017460B,0,0,0,0,100,100,0,0,1,1.4,0.6,8,10,10,0,1
[Events]
Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
ASS
$regex_srt = /^\d+\n(\d\d:\d\d:\d\d),(\d\d)\d[\ ]?-->[\ ]?(\d\d:\d\d:\d\d),(\d\d)\d(?: SSA.*)?\n([\s\S]+)/
$ass_ja = 'Dialogue: 0,\1.\2,\3.\4,Japanese,,0000,0000,0000,,\5'
$ass_en = 'Dialogue: 0,\1.34,\3.\4,English,,0000,0000,0000,,\5'
def split_emptyline filename
open(filename) do |f|
block = ""
f.each_line do |line|
if line =~ /^$/ then
yield block
block = ""
else
block << line
end
end
yield block
end
end
def parse_srt filename, template
split_emptyline filename do |block|
yield block.gsub($regex_srt, template)
end
end
def main
puts $ass_header
array = []
ARGV.each do |filename|
next unless File.file? filename
next unless filename =~ /\.srt$/
if filename =~ /ja.srt/
parse_srt(filename, $ass_ja) do |line|
array << line
end
else
parse_srt(filename, $ass_en) do |line|
array << line
end
end
end
ary = array.sort
first = ary.first
if first =~ /Dialogue: 0,([.:0-9]+),([.:0-9]+),(.+?),,0000,0000,0000,,/
ary.unshift "Dialogue: 0,00:00:00.00,#{$1.dup},#{$3.dup},,0000,0000,0000,,"
end
ary.each do |line|
puts line
end
end
main