forked from Ajatt-Tools/videoclip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoclip.lua
471 lines (409 loc) · 15.5 KB
/
videoclip.lua
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
--[[
Videoclip - mp4/webm clips creator for mpv.
Copyright (C) 2021 Ren Tatsumoto
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
]]
local NAME = 'videoclip'
local mp = require('mp')
local mpopt = require('mp.options')
local utils = require('mp.utils')
local OSD = require('osd_styler')
local p = require('platform')
local h = require('helpers')
local encoder = require('encoder')
local Timings = require('timings_mgr')
------------------------------------------------------------
-- System-dependent variables
-- Options can be changed here or in a separate config file.
-- Config path: ~/.config/mpv/script-opts/videoclip.conf
local config = {
-- absolute paths
-- relative paths (e.g. ~ for home dir) do NOT work.
video_folder_path = p.default_video_folder,
audio_folder_path = p.default_audio_folder,
-- The range of the CRF scale is 0–51, where 0 is lossless,
-- 23 is the default, and 51 is worst quality possible.
-- Insane values like 9999 still work but produce the worst quality.
video_quality = 23,
-- Use the slowest preset that you have patience for.
-- https://trac.ffmpeg.org/wiki/Encode/H.264
preset = 'faster',
video_format = 'mp4', -- mp4, vp9, vp8
video_bitrate = '1M',
video_width = -2,
video_height = 480,
video_fps = 'auto',
audio_format = 'opus', -- aac, opus
audio_bitrate = '32k', -- 32k, 64k, 128k, 256k. aac requires higher bitrates.
font_size = 24,
osd_align = 7, -- https://aegisub.org/docs/3.2/ASS_Tags/#\an
osd_outline = 1.5,
clean_filename = true,
-- Whether to upload to catbox (permanent) or litterbox (temporary)
litterbox = true,
-- Determines expire time of files uploaded to litterbox
litterbox_expire = '72h', -- 1h, 12h, 24h, 72h
sub_font = 'Noto Sans CJK JP',
}
mpopt.read_options(config, NAME)
local main_menu
local pref_menu
local allowed_presets = {
ultrafast = true,
superfast = true,
veryfast = true,
faster = true,
fast = true,
medium = true,
slow = true,
slower = true,
veryslow = true,
}
------------------------------------------------------------
-- Utility functions
local function force_resolution(width, height, clip_fn, ...)
local cached_prefs = {
video_width = config.video_width,
video_height = config.video_height,
}
config.video_width = width
config.video_height = height
clip_fn(...)
config.video_width = cached_prefs.video_width
config.video_height = cached_prefs.video_height
end
local function set_encoding_settings()
if config.video_format == 'mp4' then
config.video_codec = 'libx264'
config.video_extension = '.mp4'
elseif config.video_format == 'vp9' then
config.video_codec = 'libvpx-vp9'
config.video_extension = '.webm'
else
config.video_codec = 'libvpx'
config.video_extension = '.webm'
end
if config.audio_format == 'aac' then
config.audio_codec = 'aac'
config.audio_extension = '.aac'
else
config.audio_codec = 'libopus'
config.audio_extension = '.opus'
end
end
local function validate_config()
if not config.audio_bitrate:match('^%d+[kK]$') then
config.audio_bitrate = (tonumber(config.audio_bitrate) or 32) .. 'k'
end
if not config.video_bitrate:match('^%d+[kKmM]$') then
config.video_bitrate = '1M'
end
if not allowed_presets[config.preset] then
config.preset = 'faster'
end
set_encoding_settings()
end
local function upload_to_catbox(outfile)
local endpoint = config.litterbox and 'https://litterbox.catbox.moe/resources/internals/api.php' or 'https://catbox.moe/user/api.php'
h.notify("Uploading to " .. (config.litterbox and "litterbox.catbox.moe..." or "catbox.moe..."), "info", 9999)
-- This uses cURL to send a request to the cat-/litterbox API.
-- cURL is included on Windows 10 and up, most Linux distributions and macOS.
local r = h.subprocess({ -- This is technically blocking, but I don't think it has any real consequences ..?
p.curl_exe, '-s',
'-F', 'reqtype=fileupload',
'-F', 'time=' .. config['litterbox_expire'],
'-F', 'fileToUpload=@"' .. outfile .. '"',
endpoint
})
-- Exit codes in the range [0, 99] are returned by cURL itself.
-- Any other exit code means the shell failed to execute cURL.
if r.status < 0 or r.status > 99 then
h.notify_error("Error: Failed to upload. Make sure cURL is installed and in your PATH.", "error", 3)
return
elseif r.status ~= 0 then
h.notify_error("Error: Failed to upload to " .. (config.litterbox and "litterbox.catbox.moe" or "catbox.moe"), "error", 2)
return
end
mp.msg.info("Catbox URL: " .. r.stdout)
-- Copy to clipboard
p.copy_or_open_url(r.stdout)
end
------------------------------------------------------------
-- Menu interface
local Menu = {}
Menu.__index = Menu
function Menu:new(parent)
local o = {
parent = parent,
overlay = parent and parent.overlay or mp.create_osd_overlay('ass-events'),
keybindings = { },
}
return setmetatable(o, self)
end
function Menu:overlay_draw(text)
self.overlay.data = text
self.overlay:update()
end
function Menu:open()
if self.parent then
self.parent:close()
end
for _, val in pairs(self.keybindings) do
mp.add_forced_key_binding(val.key, val.key, val.fn)
end
self:update()
end
function Menu:close()
for _, val in pairs(self.keybindings) do
mp.remove_key_binding(val.key)
end
if self.parent then
self.parent:open()
else
self.overlay:remove()
end
end
function Menu:update()
local osd = OSD:new():config(config)
osd:append('Dummy menu.'):newline()
self:overlay_draw(osd:get_text())
end
------------------------------------------------------------
-- Main menu
main_menu = Menu:new()
main_menu.timings = Timings:new()
main_menu.keybindings = {
{ key = 's', fn = function() main_menu:set_time('start') end },
{ key = 'e', fn = function() main_menu:set_time('end') end },
{ key = 'S', fn = function() main_menu:set_time_sub('start') end },
{ key = 'E', fn = function() main_menu:set_time_sub('end') end },
{ key = 'r', fn = function() main_menu:reset_timings() end },
{ key = 'c', fn = function() main_menu:create_clip('video') end },
{ key = 'C', fn = function() force_resolution(1920, -2, encoder.create_clip, 'video') end },
{ key = 'a', fn = function() main_menu:create_clip('audio') end },
{ key = 'x', fn = function() main_menu:create_clip('video', upload_to_catbox) end },
{ key = 'X', fn = function() force_resolution(1920, -2, main_menu.create_clip, 'video', upload_to_catbox) end },
{ key = 'p', fn = function() pref_menu:open() end },
{ key = 'o', fn = function() p.open('https://streamable.com/') end },
{ key = 'ESC', fn = function() main_menu:close() end },
}
function main_menu:set_time(property)
self.timings[property] = math.max(0, mp.get_property_number('time-pos'))
self:update()
end
function main_menu:set_time_sub(property)
local sub_delay = mp.get_property_native("sub-delay")
local time_pos = mp.get_property_number(string.format("sub-%s", property))
if time_pos == nil then
h.notify_error("Warning: No subtitles visible.", "warn", 2)
return
end
self.timings[property] = math.max(0, time_pos + sub_delay)
self:update()
end
function main_menu:reset_timings()
self.timings:reset()
self:update()
end
main_menu.open = function()
Menu.open(main_menu)
end
function main_menu:update()
local osd = OSD:new():config(config)
if encoder.alive == false then
osd:red("Error: "):append("mpv is not found in the PATH."):newline()
end
osd:submenu('Timings '):italics('(+shift use sub timings)'):newline()
osd:tab():item('s: '):append('start time '):item(h.human_readable_time(self.timings['start'])):newline()
osd:tab():item('e: '):append('end time '):item(h.human_readable_time(self.timings['end'])):newline()
osd:tab():item('r: '):append('reset'):newline()
osd:submenu('Create clip '):italics('(+shift to force fullHD preset)'):newline()
osd:tab():item('c: '):append('video clip'):newline()
osd:tab():item('a: '):append('audio clip'):newline()
-- osd:tab():item('x: '):append('video clip to ' .. (config.litterbox and 'litterbox.catbox.moe (' .. config.litterbox_expire .. ')' or 'catbox.moe')):newline()
-- osd:submenu('Options '):newline()
osd:tab():item('p: '):append('preferences '):newline()
-- osd:tab():item('o: '):append('Open streamable.com'):newline()
-- osd:tab():item('ESC: '):append('Close'):newline()
self:overlay_draw(osd:get_text())
end
function main_menu:create_clip(clip_type, on_complete_fn)
self:close()
encoder.create_clip(clip_type, on_complete_fn)
end
------------------------------------------------------------
-- Preferences
pref_menu = Menu:new(main_menu)
pref_menu.keybindings = {
{ key = 'f', fn = function() pref_menu:cycle_video_formats() end },
{ key = 'a', fn = function() pref_menu:cycle_audio_formats() end },
{ key = 'm', fn = function() pref_menu:toggle_mute_audio() end },
{ key = 'r', fn = function() pref_menu:cycle_resolutions() end },
{ key = 'b', fn = function() pref_menu:cycle_audio_bitrates() end },
{ key = 'e', fn = function() pref_menu:toggle_embed_subtitles() end },
{ key = 'x', fn = function() pref_menu:toggle_catbox() end },
{ key = 'z', fn = function() pref_menu:cycle_litterbox_expiration() end },
{ key = 's', fn = function() pref_menu:save() end },
{ key = 'c', fn = function() end },
{ key = 'ESC', fn = function() pref_menu:close() end },
{ key = 'q', fn = function() pref_menu:close() end },
}
pref_menu.resolutions = {
{ w = config.video_width, h = config.video_height, },
{ w = -2, h = -2, },
{ w = -2, h = 240, },
{ w = -2, h = 360, },
{ w = -2, h = 480, },
{ w = -2, h = 720, },
{ w = -2, h = 1080, },
{ w = -2, h = 1440, },
{ w = -2, h = 2160, },
selected = 1,
}
pref_menu.audio_bitrates = {
config.audio_bitrate,
'32k',
'64k',
'128k',
'256k',
'384k',
selected = 1,
}
pref_menu.vid_formats = { 'mp4', 'vp9', 'vp8', }
pref_menu.aud_formats = { 'aac', 'opus', }
pref_menu.litterbox_expirations = { '1h', '12h', '24h', '72h', }
function pref_menu:get_selected_resolution()
return string.format(
'%s x %s',
config.video_width == -2 and 'auto' or config.video_width,
config.video_height == -2 and 'auto' or config.video_height
)
end
function pref_menu:cycle_resolutions()
self.resolutions.selected = self.resolutions.selected + 1 > #self.resolutions and 1 or self.resolutions.selected + 1
local res = self.resolutions[self.resolutions.selected]
config.video_width = res.w
config.video_height = res.h
self:update()
end
function pref_menu:cycle_audio_bitrates()
self.audio_bitrates.selected = self.audio_bitrates.selected + 1 > #self.audio_bitrates and 1 or self.audio_bitrates.selected + 1
config.audio_bitrate = self.audio_bitrates[self.audio_bitrates.selected]
self:update()
end
function pref_menu:cycle_formats(config_type)
local formats
if config_type == 'video_format' then
formats = pref_menu.vid_formats
else
formats = pref_menu.aud_formats
end
local selected = 1
for i, format in ipairs(formats) do
if config[config_type] == format then
selected = i
break
end
end
config[config_type] = formats[selected + 1] or formats[1]
set_encoding_settings()
self:update()
end
function pref_menu:cycle_video_formats()
pref_menu:cycle_formats('video_format')
end
function pref_menu:cycle_audio_formats()
pref_menu:cycle_formats('audio_format')
end
function pref_menu:toggle_mute_audio()
mp.commandv("cycle", "mute")
self:update()
end
function pref_menu:toggle_embed_subtitles()
mp.commandv("cycle", "sub-visibility")
self:update()
end
function pref_menu:toggle_catbox()
config['litterbox'] = not config['litterbox']
self:update()
end
function pref_menu:cycle_litterbox_expiration()
if not config['litterbox'] then
return
end
local expirations = pref_menu.litterbox_expirations
local selected = 1
for i, expiration in ipairs(expirations) do
if config['litterbox_expire'] == expiration then
selected = i
break
end
end
config['litterbox_expire'] = expirations[selected + 1] or expirations[1]
self:update()
end
function pref_menu:update()
local osd = OSD:new():config(config)
osd:submenu('Preferences'):newline()
osd:tab():item('r: Video resolution: '):append(self:get_selected_resolution()):newline()
osd:tab():item('f: Video format: '):append(config.video_format):newline()
osd:tab():item('a: Audio format: '):append(config.audio_format):newline()
osd:tab():item('b: Audio bitrate: '):append(config.audio_bitrate):newline()
osd:tab():item('m: Mute audio: '):append(mp.get_property("mute")):newline()
osd:tab():item('e: Embed subtitles: '):append(mp.get_property("sub-visibility")):newline()
osd:submenu('Catbox'):newline()
osd:tab():item('x: Using: '):append(config.litterbox and 'Litterbox (temporary)' or 'Catbox (permanent)'):newline()
if config.litterbox then
osd:tab():item('z: Litterbox expires after: '):append(config.litterbox_expire):newline()
else
osd:tab():color("b0b0b0"):text('x: Litterbox expires after: '):append("N/A"):newline()
end
osd:submenu('Save'):newline()
osd:tab():item('s: Save preferences'):newline()
self:overlay_draw(osd:get_text())
end
function pref_menu:save()
local function lua_to_mpv(config_value)
if type(config_value) == 'boolean' then
return config_value and 'yes' or 'no'
else
return config_value
end
end
local ignore_list = {
video_extension = true,
audio_extension = true,
video_codec = true,
audio_codec = true,
}
local mpv_dirpath = string.gsub(mp.get_script_directory(), "scripts[\\/][^\\/]+", "")
local config_filepath = utils.join_path(utils.join_path(mpv_dirpath, "script-opts"), string.format('%s.conf', NAME))
local handle = io.open(config_filepath, 'w')
if handle ~= nil then
handle:write(string.format("# Written by %s on %s.\n", NAME, os.date()))
for key, value in pairs(config) do
if ignore_list[key] == nil then
handle:write(string.format('%s=%s\n', key, lua_to_mpv(value)))
end
end
handle:close()
h.notify("Settings saved.", "info", 2)
else
h.notify_error(string.format("Couldn't open %s.", config_filepath), "error", 4)
end
end
------------------------------------------------------------
-- Finally, set an 'entry point' in mpv
validate_config()
encoder.init(config, main_menu.timings)
mp.add_key_binding('c', 'videoclip-menu-open', main_menu.open)
mp.msg.warn("Press 'c' to open the videoclip menu.")