-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsave-sub-delay.lua
60 lines (52 loc) · 1.93 KB
/
save-sub-delay.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
-- This script saves the sub-delay quantity for each file.
-- When next time the file is opened, sub-delay is automatically restored.
-- Using `--sub-delay=<sec>` or `x` and `z` key-bindings both work.
-- But keep in mind that this script distinguishes different files by reading
-- their 'path' properties. If you use in command line:
-- `mpv --sub-delay=0.1 example.mkv`
-- this delay value won't be applied to the same file you open by
-- double-clicking it.
local mputils = require "mp.utils"
local JSON = (os.getenv('APPDATA') or os.getenv('HOME')..'/.config')..'/mpv/mpv_sub-delay.json'
local jsonFile = io.open(JSON, 'a+')
local sub_delay_table = mputils.parse_json(jsonFile:read("*all"))
jsonFile:close()
function read_sub_delay()
local sub_delay = mp.get_property_native("sub-delay")
local path = mp.get_property_native("path")
if sub_delay_table == nil then
sub_delay_table = {}
end
if sub_delay == 0 then
if sub_delay_table[path] ~= nil then
sub_delay = sub_delay_table[path]
if sub_delay > 0.000999 or sub_delay < -0.000999 then
mp.command("add sub-delay " .. sub_delay)
end
end
else
sub_delay_table[path] = sub_delay
write_sub_delay()
end
end
function write_sub_delay()
local jsonFile = io.open(JSON, 'w+')
local path = mp.get_property_native("path")
sub_delay_table[path] = mp.get_property_native("sub-delay")
local jsonContent, ret = mputils.format_json(sub_delay_table)
if ret ~= error and jsonContent ~= nil then
jsonFile:write(jsonContent)
end
jsonFile:close()
end
function sub_delay_pos()
mp.command("add sub-delay 0.1")
write_sub_delay()
end
function sub_delay_neg()
mp.command("add sub-delay -0.1")
write_sub_delay()
end
mp.register_event("file-loaded", read_sub_delay)
mp.add_key_binding("x", "sub-delay+", sub_delay_pos)
mp.add_key_binding("z", "sub-delay-", sub_delay_neg)