-
Notifications
You must be signed in to change notification settings - Fork 0
/
titlecard_screenshot.lua
70 lines (68 loc) · 2.64 KB
/
titlecard_screenshot.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
-- use in mpv via keybind in input.conf: e.g. F11 script-message-to titlecard_screenshot take_titlecard_screenshot 1
-- creates screenshot with sha1 of source filename, to not run into file name length limits for the screenshot file
-- TODO: expose screenshot basedir and extension as script-opts
-- TODO: either
-- * use lua native function for trimming filnames, while keeping crc32 (suggested by Dagger)
-- * include lua-native hashing function
function get_screenshot_target_path(source_filename, time_pos)
local screenshot_basepath = "/anime/screenshots/unsorted/"
local screenshot_extension = "jpg"
local r =
mp.command_native(
{
name = "subprocess",
playback_only = false,
capture_stdout = true,
stdin_data = source_filename,
args = {"sha1sum"}
}
)
if r.status == 0 then
local csum = r.stdout:match("^(.-) ")
local screenshot_file_path =
string.format("%s%s_%s.%s", screenshot_basepath, csum, time_pos, screenshot_extension)
return screenshot_file_path
end
return string.format("%s%s_%s.%s", screenshot_basepath, filename, time_pos, screenshot_extension)
end
-- titlecard type: 1 = ep, 2 = anime, 4 = ep preview
function take_titlecard_screenshot(titlecard_type, done_command)
local source_filename = mp.get_property_native("filename")
local source_file_path = mp.command_native({"normalize-path", mp.get_property("path")})
local time_pos = mp.get_property("time-pos")
local screenshot_file_path = get_screenshot_target_path(source_filename, time_pos)
mp.commandv("screenshot-to-file", screenshot_file_path, "video")
local r =
mp.command_native(
{
name = "subprocess",
playback_only = false,
capture_stdout = true,
args = {
"/usr/bin/python3",
"/anime/screenshots/tag-anime-screenshot.py",
"-S",
screenshot_file_path,
"-F",
source_file_path,
"-s",
tostring(mp.get_property("file-size")),
"-d",
mp.get_property_osd("duration"),
"-D",
mp.get_property("duration"),
"-t",
mp.get_property_osd("time-pos"),
"-T",
mp.get_property("time-pos"),
"--screenshot-type",
titlecard_type
}
}
)
print(r.stdout)
if done_command then
mp.command(done_command)
end
end
mp.register_script_message("take_titlecard_screenshot", take_titlecard_screenshot)