-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
210 lines (185 loc) · 5.29 KB
/
Rakefile
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require 'tmpdir'
require 'zip'
require 'fileutils'
require 'securerandom'
require 'zip'
require 'pry'
{
subtitleedit: "SubtitleEdit",
ffmpeg: "ffmpeg",
netflix_dir: ENV.fetch("NETFLIX_VIDEOS",
File.join(ENV["USERPROFILE"], "Videos")),
extensions: {
"ja" => %w(日本語 Japanese CC.Japanese FORCED.Japanese),
"en" => %w(英語 English CC.English FORCED.English),
},
softsub_targets: {
"ja" => "sub",
"en" => "srt",
},
}.each do |key, value|
define_method key do
value
end
end
# https://stackoverflow.com/questions/9204423/how-to-unzip-a-file-in-ruby-on-rails
def extract_zip(file, destination)
FileUtils.mkdir_p(destination)
Zip::File.open(file) do |zip_file|
zip_file.each do |f|
fpath = File.join(destination, f.name)
zip_file.extract(f, fpath) unless File.exist?(fpath)
end
end
end
def resolution_of(bdnxml)
body = IO.read(bdnxml)
height = body[%r(Format VideoFormat="(\d+)p"), 1]
h = height.to_i
w = h * 16 / 9
"#{w}x#{h}"
end
def mktmpdir
prefix = "d"
tmpdir = %W(
HARDSUB_NETFLIX_TEMP
TMPDIR TMP TEMP USERPROFILE
).each do |env|
break ENV[env] if ENV.key?(env)
end
Dir.mktmpdir(prefix, tmpdir) do |dir|
yield dir.gsub("\\", "/")
end
end
def convert_ttml(source, format)
system("where magick > nul 2> nul") or abort("install ImageMagick and add it into PATH environment variable")
system("where #{subtitleedit} > nul 2> nul") or abort("install SubtitleEdit and add it into PATH environment variable")
mktmpdir do |dir|
extract_zip(source, dir)
ttml = Dir.glob("#{dir}/*.xml").first
mktmpdir do |dir2|
ruby("ttml2bdnxml.rb", ttml, dir2)
bdnxml = Dir.glob("#{dir2}/*_bdn.xml").first
resolution = "/resolution:#{resolution_of(bdnxml)}"
outputfolder = "/outputfolder:#{dir2}"
args = [ bdnxml, format, resolution, outputfolder ]
sh(subtitleedit, "/convert", *args)
yield dir2
end
end
end
def subtitle_sources(lang, extensions)
-> (filename) do
candidates = extensions.map do |ext|
filename.gsub(/\.#{lang}\..+/, ext)
end
candidates.find do |source|
File.exist?(source)
end || candidates.first
end
end
def text_subtitle_sources(lang, extensions)
exts = extensions.map do |ext|
".#{ext}.ttml"
end + [ ".CC.#{lang}.srt", ".FORCED.#{lang}.srt" ]
subtitle_sources(lang, exts)
end
def image_subtitle_sources(lang, extensions)
exts = extensions.map do |ext|
".#{ext}.ttml.zip"
end
subtitle_sources(lang, exts)
end
def move_sub_to_target(dir, target)
Dir.glob("#{dir}/*.{sub,idx}").each do |filename|
extname = File.extname(filename)
basename = "#{File.basename(target, ".sub")}#{extname}"
destname = File.join(File.dirname(target), basename)
FileUtils.mv filename, destname
end
end
extensions.each do |lang, exts|
text_sources = text_subtitle_sources(lang, exts)
rule ".#{lang}.srt" => text_sources do |t|
source = t.source
if source.end_with?(".srt")
FileUtils.mv source, t.name
elsif source.end_with?(".ttml")
mktmpdir do |dir|
sh(subtitleedit, "/convert", source, "srt", "/outputfolder:#{dir}")
filename = Dir.glob("#{dir}/*.srt").first
FileUtils.cp filename, t.name
end
end
end
image_sources = image_subtitle_sources(lang, exts)
rule ".#{lang}.sub" => image_sources do |t|
convert_ttml(t.source, "VobSub") do |dir|
move_sub_to_target(dir, t.name)
end
end
rule ".#{lang}.sup" => image_sources do |t|
convert_ttml(t.source, "Blu-raysup") do |dir|
filename = Dir.glob("#{dir}/*.sup").first
FileUtils.cp filename, t.name
end
end
end
def hardsub(mp4, srt, sup, target)
filter_complex = [
"[0:v]subtitles=#{srt.gsub("C:", "")}:force_style='Alignment=6'[v0]",
"[1:s][v0]scale2ref[s][v1]",
"[v1][s]overlay[v]",
].join(";")
args = [
"-i", mp4,
# "-itsoffset", "5.5",
"-itsoffset", "2",
"-i", sup,
"-filter_complex", filter_complex,
"-map", "[v]",
"-codec:a", "mp3",
"-map", "0:a:0",
"-y",
]
sh ffmpeg, *args, target
end
rule '_hardsub.mp4' => [ '.mp4', '.en.srt', '.ja.sup' ] do |t|
sources = t.sources.map do |filename|
File.expand_path(filename, __FILE__)
end
mktmpdir do |dir|
$stderr.puts "creating #{t.name}"
basename = "#{dir}/#{SecureRandom.alphanumeric}"
mp4 = "#{basename}.mp4"
FileUtils.cp(sources[0], mp4)
eng = "#{basename}.srt"
FileUtils.cp(sources[1], eng)
extname = File.extname(t.sources[2])
ja = "#{basename}#{extname}"
FileUtils.cp(sources[2], ja)
target = "#{basename}_.mp4"
hardsub(mp4, eng, ja, target)
FileUtils.mv(target, t.name)
$stderr.puts "created #{t.name}"
end
end
task :hardsub, [:name] do |t, args|
glob = File.join(netflix_dir, "*#{args[:name]}*.mp4")
Dir.glob(glob.gsub("\\", "/")) do |mp4|
next if mp4.include?("_hardsub.mp4")
target = mp4.gsub(".mp4", "_hardsub.mp4")
Rake::Task[target.encode("UTF-8")].invoke
end
end
task :softsub, [:name] do |t, args|
glob = File.join(netflix_dir, "*#{args[:name]}*.mp4")
Dir.glob(glob.gsub("\\", "/")) do |mp4|
next if mp4.include?("_hardsub.mp4")
softsub_targets.each do |lang, ext|
suffix = ".#{lang}.#{ext}"
target = mp4.gsub(".mp4", suffix)
Rake::Task[target.encode("UTF-8")].invoke
end
end
end