-
Notifications
You must be signed in to change notification settings - Fork 10
/
customscreenshot.lua
124 lines (101 loc) · 4.4 KB
/
customscreenshot.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
-- A script to save screenshots in different
-- locations based on whatever conditions.
-- Requires lua knowledge or some will to experiment.
local utils = require 'mp.utils'
local msg = require 'mp.msg'
local settings = {
basepath = mp.get_property('screenshot-directory'),
basetemplate = mp.get_property('screenshot-template'),
--the command to creating your custom directory without the path
createdir = { 'mkdir', '-p' },
--patterns are in priority order, first match will be used for screenshot
--about patterns in lua: http://lua-users.org/wiki/PatternsTutorial and https://www.lua.org/pil/20.2.html
--about strings in lua: http://lua-users.org/wiki/StringLibraryTutorial
--useful tool to test patterns and strings: https://www.lua.org/demo.html
patterns = {
--you can copy paste the template below and change values to create more rules
{
--This rule will be applied for files containing [SubGroup] in their name
--An example filename would be [SubGroup] Show name - 01 [720].mkv
--screenshots will be saved in your_screenshots/Show name/Show name - 01/Show name - 01[02m25s].jpg
--this function will be called on file load to determine whether current file is a match for these rules or not
--return booleanish value(for if statement)
['match'] =
function()
return mp.get_property('filename/no-ext'):match('%[SubGroup%]') and true or false
end
,
--function that will return the absolute save path for files
['savepath'] =
function()
--parse filename to create directory name - note that naming conventions differ so you need to modify this
--in this case I parse it so that files will be added in show_name/show_name_episode/
--you can use your imagination when creating this. Having a static string will make all under this rule in same folder
--you could even make sub folders inside episode based on duration if you wanted
--match the show name on a known format because this rule only applies to shows formatted like this
local head_dir = mp.get_property('filename/no-ext'):match('%]%s(.*)%s%-%s%d')
--remove brackets for the subfolder name
--instead of match you can also use gsub to strip parts of the filename
local sub_dir = mp.get_property('filename/no-ext'):gsub('%s*[%[%(].-[%]%)]%s*', '')
local relative_dir = utils.join_path(head_dir, sub_dir)
--join and return our custom path with basepath
return utils.join_path(mp.get_property('screenshot-directory'), relative_dir)
end
,
--if not nil then overriding default screenshot-template, will property expand
--needs to be a function that returns the template as a string
['filename'] =
function()
--remove brackets, their content and surrounding white space from filename
local name = mp.get_property('filename/no-ext'):gsub('%s*[%[%(].-[%]%)]%s*', '')
return name.."[%wMm%wSs]" --add property expanding part
end
,
},
}
}
local state = {
pattern = nil,
}
function on_load()
state.pattern = nil
for index, pattern in ipairs(settings.patterns) do
if pattern.match() then
state.pattern = pattern
break
end
end
end
function screenshot(param)
param = (param or "")
if state.pattern then
custom_screenshot(state.pattern, param)
else
mp.command("screenshot "..param)
end
end
function custom_screenshot(pattern, param)
local savepath = pattern.savepath()
--prepare and create/check directory
local args = settings.createdir
table.insert(args, savepath)
res = utils.subprocess({ args = args })
if not res.error and res.status == 0 then
--set temporary screenshot settings
if pattern.filename then
mp.set_property('screenshot-template', pattern.filename())
end
mp.set_property("screenshot-directory", savepath)
--take the screenshot
mp.command("screenshot "..param)
--reset screenshot settings
mp.set_property("screenshot-directory", settings.basepath)
mp.set_property("screenshot-template", settings.basetemplate)
else
msg.error("Failed to create directory "..savepath)
msg.error("Status: "..(res.status or "unknown"))
msg.error("Error: "..(res.error or "unknown"))
end
end
mp.register_script_message("custom-screenshot", screenshot)
mp.register_event('file-loaded', on_load)