-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptimize_videos.lua
More file actions
292 lines (218 loc) · 7.12 KB
/
Copy pathoptimize_videos.lua
File metadata and controls
292 lines (218 loc) · 7.12 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
local params = {...}
local last_param
for _, param in ipairs(params) do
local param_name = param:match("^[-/]+(%C+)$") -- matches for common flag initializers, which are "/", "-" and "--" and captures flag name, if possible.
if param_name then
params[param_name] = true
last_param = param_name
elseif last_param then
params[last_param] = tonumber(param) or param
last_param = nil
else
print("Invalid command, incorrect flag pass near: " .. param)
os.exit()
end
end
if not params["output"] then
params["output"] = "encoded"
end
-- Config
--#region Group configs
local groups = {
-- Promo videos, new character, new location showcases
{"^mv_prm142%.mp4", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -vf scale=-1:540 -an -crf 28 -preset %s -tune film -n \"%s\""},
{"^mv_prm", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -an -crf 28 -preset %s -tune film -n \"%s\""},
-- Event trailers
{"^mv103_", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
{"^mv", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -vf scale=-1:720 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
-- Cutscenes (slightly better sound)
{"^mm01011403", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
{"^mm03010105", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
{"^mm03010106", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
{"^mm03010107", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
{"^mm02010805_en", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 192k -crf 26 -preset %s -tune film -n \"%s\""},
-- Cutscenes (remaining)
{"^mm", "ffmpeg -i \"%s\" -filter:v fps=fps=30 -c:a aac -b:a 128k -crf 26 -preset %s -tune film -n \"%s\""},
}
--#endregion
-- OS detection
local OS
if package.config:sub(1,1) == "/" then
OS = "posix"
else
OS = "win32"
end
-- Create output directory
local output = params["output"]
if OS == "win32" then
os.execute(string.format("if not exist \"%s\" mkdir \"%s\"", output, output))
elseif OS == "posix" then
os.execute(string.format("mkdir -p \"%s\"", output))
end
--#region functions
local function available(name)
if params["skip-checks"] then
return "yes"
end
local h, err
if OS == "win32" then
h, err = io.popen("where " .. name)
else
h, err = io.popen("which " .. name)
end
if h then
local result = h:read("*a")
h:close()
if result:find(name) then
return result
else
return false
end
end
error("Error checking ffmpeg installation! Err: " .. tostring(err) .. "\nPass flag --skip-checks for a failsafe.")
end
local function enumerate_mp4(path)
path = path or ""
local files = {}
local h, err
if OS == "win32" then
h, err = io.popen("dir /b \"" .. path .. "\"")
else
h, err = io.popen("ls -1 \"" .. path .. "\"")
end
if h then
local filenames = h:read("*a")
h:close()
for file in string.gmatch(filenames, "%C+%.mp4") do
files[#files+1] = file
end
return files
end
error("Error enumerating films. Err: " .. tostring(err))
end
local function filter_korean(videos)
for i = #videos, 1, -1 do
local video = videos[i]
if video:find("ko") then
table.remove(videos, i)
end
end
end
--#endregion
-- Init
print("Nier:reiN video compression utility")
local ffmpeg = available("ffmpeg")
if not ffmpeg then
print("You do not have ffmpeg installed! Please install ffmpeg or open a new console if you recently added it to PATH.")
return
end
print("Found ffmpeg at " .. ffmpeg)
local videos = enumerate_mp4(params["input"])
print("Found " .. #videos .. " videos.")
if #videos == 0 then
if params["input"] then
print("No videos were found in provided input folder. Check again your \"--input\" parameter.")
else
print("No videos were found in working directory. Have you forgot to pass the \"--input\" parameter?")
end
end
-- Skip Koreans
local choice
repeat
io.write("Skip Korean language videos (Recoding will be 40% faster)? Y/N [Default: Y]: ")
choice = string.lower(io.read())
until choice == "y" or choice == "t" or choice == "n" or choice == "f" or choice == ""
if #choice == 0 then
choice = "y"
end
local skip_korean = choice == "y" or choice == "t" or false
if skip_korean then
local old_count = #videos
filter_korean(videos)
print(string.format("Skipping Korean language films. Now processing %d videos instead of %d.", #videos, old_count))
else
print(string.format("Processing all %d videos.", #videos))
end
-- Preset logic
local presets = {
"ultrafast",
"superfast",
"veryfast",
"faster",
"fast",
"medium",
"slow",
"slower",
"veryslow",
"placebo"
}
for i, preset in ipairs(presets) do
presets[preset] = i
end
local function printPresets()
print("")
for i, preset in ipairs(presets) do
print(i .. ". " .. preset)
end
print("(never pick placebo)")
end
repeat
printPresets()
print("\nSelect an ffmpeg preset which will be used. Preset affects video size, quality and encoding time.")
print("From \"ultrafast\" (worst file size, fastest encoding) to \"veryslow\" (best file size, slowest encoding)")
print("Default preset is \"medium\" (6), \"slower\" (8) provides a 5% filesize difference but is recommended on better systems.")
io.write("Select preset (number or full name) [Default: 6]: ")
choice = string.lower(io.read())
choice = choice == "" and presets[6] or choice
choice = tonumber(choice) and presets[tonumber(choice)] or choice
until presets[choice]
print(string.format("\nSelected preset: %s (%d)", choice, presets[choice]))
local preset = choice
-- Start encoding
local pattern_stats = {}
local skip_pattern = "skip"
local input_folder = params["input"]
if input_folder then
input_folder = input_folder .. "/"
else
input_folder = ""
end
local output_folder = output .. "/"
for _, file in ipairs(videos) do
local matched = false
for i, group in pairs(groups) do
local pattern, command = group[1], group[2]
if file:match(pattern) then
pattern_stats[i] = pattern_stats[i] or {}
pattern_stats[i][#pattern_stats[i]+1] = file
os.execute(string.format(command, input_folder .. file, preset, output_folder .. file))
matched = true
break
end
end
if not matched then
pattern_stats[skip_pattern] = pattern_stats[skip_pattern] or {}
pattern_stats[skip_pattern][#pattern_stats[skip_pattern]+1] = file
end
end
if params["v"] or params["verbose"] then
print("\nPattern statistics:")
local i = 1
for pattern, files in pairs(pattern_stats) do
if pattern ~= skip_pattern then
print("")
print(i .. ". [".. #files .." files] Command: " .. string.format(groups[pattern][2], "input_file", preset, "output_file"))
print(table.concat(files, ", "))
i = i + 1
end
end
if pattern_stats[skip_pattern] then
print("")
print(i .. ". [" .. #pattern_stats[skip_pattern] .. (skip_korean and "+" or "") .. " files] Skipped recoding")
if skip_korean then
io.write("All korean movies, ")
end
print(table.concat(pattern_stats[skip_pattern]))
end
end
print("\nRecoding finished! Processed " .. (#videos - #(pattern_stats[skip_pattern] or {})) .. " files out of " .. #videos .. ".")