forked from nimatrueway/mpv-bookmark-lua-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarker.lua
More file actions
277 lines (250 loc) · 8.07 KB
/
Copy pathbookmarker.lua
File metadata and controls
277 lines (250 loc) · 8.07 KB
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
local assdraw = require "mp.assdraw"
--// Save/Load string serializer function
function exportstring( s )
return string.format("%q", s)
end
--// The Save Function [copied from http://lua-users.org/wiki/SaveTableToFile]
function saveTable( tbl,filename )
local charS,charE = " ","\n"
local file,err = io.open( filename, "wb" )
if err then return err end
-- initiate variables for save procedure
local tables,lookup = { tbl },{ [tbl] = 1 }
file:write( "return {"..charE )
for idx,t in ipairs( tables ) do
file:write( "-- Table: {"..idx.."}"..charE )
file:write( "{"..charE )
local thandled = {}
for i,v in ipairs( t ) do
thandled[i] = true
local stype = type( v )
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables, v )
lookup[v] = #tables
end
file:write( charS.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( charS..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( charS..tostring( v )..","..charE )
end
end
for i,v in pairs( t ) do
-- escape handled values
if (not thandled[i]) then
local str = ""
local stype = type( i )
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert( tables,i )
lookup[i] = #tables
end
str = charS.."[{"..lookup[i].."}]="
elseif stype == "string" then
str = charS.."["..exportstring( i ).."]="
elseif stype == "number" then
str = charS.."["..tostring( i ).."]="
end
if str ~= "" then
stype = type( v )
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables,v )
lookup[v] = #tables
end
file:write( str.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( str..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( str..tostring( v )..","..charE )
end
end
end
end
file:write( "},"..charE )
end
file:write( "}" )
file:close()
end
--// The Load Function [copied from http://lua-users.org/wiki/SaveTableToFile]
function loadTable( sfile )
local ftables,err = loadfile( sfile )
if err then return _,err end
local tables = ftables()
for idx = 1,#tables do
local tolinki = {}
for i,v in pairs( tables[idx] ) do
if type( v ) == "table" then
tables[idx][i] = tables[v[1]]
end
if type( i ) == "table" and tables[i[1]] then
table.insert( tolinki,{ i,tables[i[1]] } )
end
end
-- link indices
for _,v in ipairs( tolinki ) do
tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
end
end
return tables[1]
end
--// default file to save/load bookmarks to/from
function getConfigFile()
return os.getenv('APPDATA') .. "\\mpv-bookmarks.json"
end
--// check whether a file exists or not
function file_exists(path)
local f = io.open(path,"r")
if f~=nil then
io.close(f)
return true
else
return false
end
end
--// save current file/pos to a bookmark object
function currentPositionAsBookmark()
local bookmark = {}
bookmark["pos"] = mp.get_property_number("time-pos")
bookmark["filepath"] = mp.get_property("path")
bookmark["filename"] = mp.get_property("filename")
return bookmark
end
--// play to a bookmark
function bookmarkToCurrentPosition(bookmark, tryToLoadFile)
if mp.get_property("path") == bookmark["filepath"] then -- if current media is the same as bookmark media
mp.set_property_number("time-pos", bookmark["pos"])
return
elseif tryToLoadFile == true then
mp.commandv("loadfile", bookmark["filepath"], "replace")
local seekerFunc = {}
seekerFunc.fn = function()
mp.unregister_event(seekerFunc.fn);
bookmarkToCurrentPosition(bookmark, false)
end
mp.register_event("playback-restart", seekerFunc.fn)
end
end
function timestamp(duration)
-- mpv may return nil before exiting.
if not duration then return "" end
local hours = duration / 3600
local minutes = duration % 3600 / 60
local seconds = duration % 60
return string.format("%02d:%02d:%06.03f", hours, minutes, seconds)
end
function clear_ass_text()
mp.set_osd_ass(0, 0, "")
end
function draw_ass_text(text)
ass = assdraw.ass_new()
ass:pos(0, 0)
ass:append(text)
mp.set_osd_ass(0, 0, ass.text)
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- local inspect = require 'inspect' # for debugging
function getSafeString(array,key)
local arrayKey = array[key]
if arrayKey == nil then
return "-"
else
return arrayKey
end
end
function getSafeInt(array,key)
local arrayKey = array[key]
if arrayKey == nil then
return 0
else
return arrayKey
end
end
local displayListCountdownElapsed = 6
local displayListString = ""
displayListCountdown = mp.add_periodic_timer(1, function()
displayListCountdownElapsed = displayListCountdownElapsed + 1
if displayListCountdownElapsed >= 5 then
displayListCountdown:kill()
clear_ass_text()
else
draw_ass_text(string.format("%s\\N\\h\\hHiding in %d ..",displayListString, 5 - displayListCountdownElapsed) )
end
end)
function unichr(ord)
if ord == nil then return nil end
if ord < 32 then return string.format('\\x%02x', ord) end
if ord < 126 then return string.char(ord) end
if ord < 65539 then return string.format("\\u%04x", ord) end
if ord < 1114111 then return string.format("\\u%08x", ord) end
end
mp.register_script_message("bookmark-list", function(slot)
local bookmarks, error = loadTable(getConfigFile())
local curFileName = mp.get_property_osd("filename")
if error ~= nil then
mp.osd_message("Error: " .. error)
return
end
displayListString = "{\\fnSource Sans Pro\\b1\\fs12\\bord0.7\\c&HFFFFFF&\\1a&H00&\\3c&H000000&\\3a&H00&\\4c&H000000&\\4a&HFF&}\\h\\N\\h\\hBookmark list:\\N"
local bookmarkCount = tablelength(bookmarks)
print("List of bookmarks:")
for i=1,bookmarkCount,1
do
local fileName = getSafeString(bookmarks[tostring(i)],"filename")
local path = getSafeString(bookmarks[tostring(i)],"filepath")
local pos = getSafeInt(bookmarks[tostring(i)],"pos")
local preSpace = "\\h{\\1a&HFF&\\3a&HFF&}" .. "►" .. "{\\1a&H00&\\3a&H00&}\\h"
if string.lower(fileName) == string.lower(curFileName) then
preSpace = "\\h{\\1a&H00&\\3a&H00&}" .. "►" .. "{\\1a&H00&\\3a&H00&}\\h"
end
print(string.format("[%d] %s [%s]\\N", i, fileName, timestamp(pos)))
displayListString = displayListString .. string.format("%s[Alt-%d] %s [%s]\\N",preSpace, i, fileName, timestamp(pos))
end
ass = assdraw.ass_new()
ass:pos(0, 0)
ass:append(displayListString .. "\\N\\h\\hHiding in 5 ..")
mp.set_osd_ass(0, 0, ass.text)
displayListCountdownElapsed = 0
displayListCountdown:resume()
end)
--// handle "bookmark-set" function triggered by a key in "input.conf"
mp.register_script_message("bookmark-set", function(slot)
print("Saving " .. slot )
local bookmarks, error = loadTable(getConfigFile())
if error ~= nil then
bookmarks = {}
end
bookmarks[slot] = currentPositionAsBookmark()
local result = saveTable( bookmarks, getConfigFile())
if result ~= nil then
mp.osd_message("Error saving: " .. result)
end
mp.osd_message("Bookmark#" .. slot .. " saved.")
end)
--// handle "bookmark-load" function triggered by a key in "input.conf"
mp.register_script_message("bookmark-load", function(slot)
local bookmarks, error = loadTable(getConfigFile())
if error ~= nil then
mp.osd_message("Error: " .. error)
return
end
local bookmark = bookmarks[slot]
if bookmark == nil then
mp.osd_message("Bookmark#" .. slot .. " is not set.")
return
end
if file_exists(bookmark["filepath"]) == false then
mp.osd_message("File " .. bookmark["filepath"] .. " not found!")
return
end
bookmarkToCurrentPosition(bookmark, true)
mp.osd_message("Bookmark#" .. slot .. " loaded.")
end)