-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguess-media-title.lua
115 lines (83 loc) · 3.06 KB
/
guess-media-title.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
--[[
Uses [guessit](https://github.com/guessit-io/guessit) to detect media title by filename.
Upon detection, sets `force-media-title` variable.
Useful for getting cleaner screenshot file names.
Script options can be specified in `script-opts/guess-media-title.conf` file.
If `show_detection_message` is set to `yes`, the script is going to show a flash message in OSD with a detected media title.
Requires `guessit` to be installed for a Python interpreter accessible as `python`.
--]]
local mp = require("mp")
local msg = require("mp.msg")
local utils = require("mp.utils")
local options = require("mp.options")
local opts = {
show_detection_message = false,
show_episode_title = true
}
options.read_options(opts, "guess-media-title")
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function show_flash_message(text)
local overlay = mp.create_osd_overlay("ass-events")
overlay.data = "{\\a6\\fs14}" .. text
overlay:update()
mp.add_timeout(5, function()
overlay:remove()
end)
end
local function build_title(info)
if info == nil then
return nil
elseif info.type == "episode" and info.season ~= nil and type(info.season) == "number" and info.episode ~= nil and type(info.episode) == "number" then
local episode_spec = string.format("s%02de%02d", info.season, info.episode)
if opts.show_episode_title and info.episode_title ~= nil and type(info.episode_title) == "string" then
return string.format("%s (%s — %s)", info.title, episode_spec, info.episode_title)
end
return string.format("%s (%s)", info.title, episode_spec)
else
return info.title
end
end
local function on_guessit_completed(success, result, error)
if not success then
msg.error("failed to guess media title: " .. error)
return
end
local media_title = build_title(utils.parse_json(trim(result.stdout)))
if media_title ~= nil then
mp.set_property_native("force-media-title", media_title)
if opts.show_detection_message then
show_flash_message("Detected media title: {\\b1}" .. media_title)
end
end
end
local function starts_with(str, prefix)
return string.sub(str, 1, #prefix) == prefix
end
local function should_guess()
local forced = mp.get_property_native("force-media-title")
if forced ~= nil and forced ~= "" then
return false
end
local path = mp.get_property_native("path")
if path == nil then
return false
end
return not (starts_with(path, "http://") or starts_with(path, "https://"))
end
local function guess_media_title()
if not should_guess() then
return
end
mp.command_native_async({
name = "subprocess",
capture_stdout = true,
args = { "python", "-m", "guessit", "--json", mp.get_property_native("filename") }
}, on_guessit_completed)
end
local function on_file_end()
mp.set_property_native("force-media-title", "")
end
mp.register_event("start-file", guess_media_title)
mp.register_event("end-file", on_file_end)