-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdvd-browser.lua
422 lines (339 loc) · 13.2 KB
/
dvd-browser.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
--[[
mpv-dvd-browser
This script uses the `lsdvd` commandline utility to allow users to view and select titles
for DVDs from directly within mpv. The browser is interractive and allows for both playing
the selected title, or appending it to the playlist.
For full documentation see: https://github.com/CogentRedTester/mpv-dvd-browser
]]--
local mp = require 'mp'
local msg = require 'mp.msg'
local opt = require 'mp.options'
local utils = require 'mp.utils'
local o = {
lsdvd = 'lsdvd',
--path to the dvd device to send to lsdvd, leaving this blank will set the script to
--use the --dvd-device option.
--It is recommended that this be left blank unless using wsl
dvd_device = "",
--number of titles to display on the screen at once
num_entries = 20,
--by default the player enters an infinite loop, usually of the DVD menu screen, after moving
--past the last second of the file. If this option is enabled, then the script will
--automatically configure mpv to end playback before entering the loop
escape_loop = true,
--changes default mpv behaviour and loads the first title instead of the longest when
--the title isn't specified
start_from_first_title = true,
---------------------
--playlist options:
---------------------
--adds the previous and subsequent titles to the playlist when playing a dvd
--only does this when there is only one item in the playlist
create_playlist = true,
--when dvd:// (no specified title) is loaded the script will always insert all of the
--titles into the playlist, regardless of the playlist length
--similar to loading a directory or playlist file
treat_root_as_playlist = true,
------------------------------------------
--wsl options for limitted windows support
------------------------------------------
wsl = false,
wsl_password = "",
--------------
--ass options
---------------
ass_header = "{\\q2\\fs35\\c&00ccff&}",
ass_body = "{\\q2\\fs25\\c&Hffffff&}",
ass_selected = "{\\c&Hfce788&}",
ass_playing = "{\\c&H33ff66&}",
ass_footerheader = "{\\c&00ccff&\\fs16}",
ass_cursor = "{\\c&00ccff&}",
ass_length = "{\\fs20\\c&aaaaaa&}"
}
opt.read_options(o, 'dvd_browser')
--[[
lsdvd returns a JSON object with a number of details about the dvd
some notable values are:
title = title of the dvd
longest_track = longest track on the dvd
device = path to the dvd mount point
track = array of 'titles'/tracks on the disc
length = length of each title
ix = numerical id of the title starting from 1
chapter = array of chapters in the title
num_chapters = length of chapters array (added by me)
]]--
--if the script name includes file_browser, then the script is being loaded
--as an addon, and hence we can skip loading several things
local STANDALONE = not mp.get_script_name():find("file_browser")
local list = {}
if STANDALONE then
package.path = mp.command_native( {"expand-path", "~~/script-modules/?.lua;" } ) .. package.path
list = require "scroll-list"
list.header_style = o.ass_header
list.list_style = o.ass_body
list.wrapper_style = o.ass_footerheader
list.empty_text = "insert DVD"
end
local dvd = {}
local state = {
playing_disc = false,
selected = 1,
flag_update = false
}
--automatically match to the current dvd device
if (o.dvd_device == "") then
mp.observe_property('dvd-device', 'string', function(_, device)
if device == "" then device = "/dev/dvd" end
o.dvd_device = device
--we set this to false to force a dvd rescan
state.playing_disc = false
end)
end
local function get_header_str()
local title
if dvd == nil then title = ""
else title = dvd.title end
return '📀 dvd://'..title
end
local function get_line_str(v)
return "Title "..(v.ix-1)..o.ass_length.." ["..v.length.."] "..v.num_chapters.." chapters"
end
function list:format_header()
self.ass.data = o.ass_header..get_header_str().."\\N ---------------------------------------------------- \\N"
end
--simple function to append to the ass string
function list:format_line(i, v)
self:append(o.ass_body)
--the below text contains unicode whitespace characters
if i == list.selected then self:append(o.ass_cursor..[[➤\h]]..o.ass_selected)
else self:append([[\h\h\h\h]]) end
--prints the currently-playing icon and style
if mp.get_property('filename', "0") == tostring(i-1) then
self:append(o.ass_playing)
end
self:append(get_line_str(v))
self:newline()
end
--sends a call to lsdvd to read the contents of the disc
local function read_disc()
msg.verbose('reading contents of ' .. o.dvd_device)
local args
if o.wsl then
msg.verbose('wsl compatibility mode enabled')
--if wsl password is not set then we'll assume the user has mounted manually
if o.wsl_password ~= "" then
local dvd_device = mp.get_property('dvd-device', ''):gsub([[\]], [[/]])
msg.verbose('mounting '..dvd_device..' at '..o.dvd_device)
mp.command_native({
name = 'subprocess',
playback_only = false,
args = {'wsl', 'echo', o.wsl_password, '|', 'sudo', '-S', 'mount', '-t', 'drvfs', dvd_device, o.dvd_device}
})
end
--setting wsl arguments
args = {'wsl', o.lsdvd, o.dvd_device, '-Oy', '-c'}
else
args = {o.lsdvd, o.dvd_device, '-Oy', '-c'}
end
local cmd = mp.command_native({
name = 'subprocess',
playback_only = false,
capture_stdout = true,
capture_stderr = true,
args = args
})
--making the python string JSON compatible
local result = cmd.stdout:gsub("'", '"')
result = result:gsub('lsdvd = ', '')
dvd = utils.parse_json(result)
if (not dvd) then
msg.error(cmd.stderr)
state.playing_disc = false
return
end
msg.trace(utils.to_string(dvd))
--creating a fallback for the title
-- if dvd.title == "unknown" then dvd.title = "dvd://" end
--making modifications to all the entries
for i = 1, #dvd.track do
local v = dvd.track[i]
--saving the chapter count
v.num_chapters = #v.chapter
--modifying the length
local l = v.length
local lstr = tostring(l)
--adding the microseconds as is
local index = tostring(l):find([[.[^.]*$]])
local str
if index == 1 then str = "00"
else
str = tostring(lstr:sub(index+1))
str = string.format('%02d', str)
end
l = math.floor(l)
local seconds = l%60
str = string.format('%02d', seconds) .. '.' .. str
l = (l - seconds)/60
local mins = l%60
str = string.format('%02d', mins) .. ':' .. str
l = (l-mins)/60
local hours = l%24
str = string.format('%02d', hours) .. ':' .. str
msg.debug('changing length string for title '..(i-1)..' to '..str)
v.length = str
end
state.playing_disc = true
list.list = dvd.track
end
--this function updates dvd information and updates the browser
local function update()
read_disc()
list:update()
end
--appends the specified playlist item along with the desired options
local function load_dvd_title(title, flag)
local i = title.ix-1
mp.commandv("loadfile", "dvd://"..i, flag)
end
--handles actions when dvd:// paths are played directly
--updates dvd information and inserts disc titles into the playlist
local function load_disc()
local path = mp.get_property('stream-open-filename', '')
if path:find('dvd://') ~= 1 then
state.playing_disc = false
return
end
msg.verbose('playing dvd')
--if we have not stopped playing a disc then there's no need to parse the disc again
if not state.playing_disc then read_disc() end
--if we still can't detect a disc then return
if (not state.playing_disc) then return end
--if we successfully loaded info about the disc it's time to do some other stuff:
--this code block finds the default title of the disc
local curr_title
--if the user specified a title number we use that
if path ~= "dvd://" then
--treating whatever comes after "dvd://" as the title number
curr_title = tonumber(path:sub(7))
--if dvd:// was sent and this option is set we set the default ourselves
elseif o.start_from_first_title then
mp.set_property('stream-open-filename', "dvd://0")
curr_title = 0
--otherwise if just dvd:// was sent we need to find the longest title
else
curr_title = dvd.longest_track
end
mp.set_property('file-local-options/title', dvd.title.." - Title "..curr_title)
--if o.create_playlist is false then the function can end here
if not o.create_playlist then return end
--offsetting curr_title by one to account for lua arrays being 1-based
curr_title = curr_title+1
local length = mp.get_property_number('playlist-count', 1)
--load files in the playlist under the specified conditions
if (path == "dvd://" and o.treat_root_as_playlist) or length == 1 then
local pos = mp.get_property_number('playlist-pos', 1)
--add all of the files to the playlist
for i = 1, #dvd.track do
if i ~= curr_title then
load_dvd_title(dvd.track[i], "append")
length = length + 1
--we need slightly different behaviour when prepending vs appending a playlist entry
if (i < curr_title) then
mp.commandv("playlist-move", length-1, pos)
pos = pos+1
elseif (i > curr_title) then
mp.commandv("playlist-move", length-1, pos+(i-curr_title))
end
end
end
--if the path is dvd, then we actually need to fully replace this entry in the playlist,
--otherwise the whole disc will be added to the playlist again if moving back to this entry
if (path == "dvd://") then
msg.verbose('replacing dvd:// with playlist')
load_dvd_title(dvd.track[curr_title], "append")
length = length+1
mp.commandv('playlist-move', length-1, pos+1)
mp.commandv('playlist-remove', 'current')
end
end
end
--opens the currently selected file
local function open_file(flag)
load_dvd_title(dvd.track[list.selected], flag)
if flag == 'replace' then
list:close()
end
end
--opens the browser and declares dynamic keybinds
function list:open()
self.hidden = false
if not state.playing_disc then
update()
else
self:open_list()
end
self:add_keybinds()
end
list.keybinds = {
{"ESC", "exit", function() list:close() end, {}},
{"ENTER", "open", function() open_file('replace') end, {}},
{"Shift+ENTER", "append_playlist", function() open_file('append') end, {}},
{'DOWN', 'scroll_down', function() list:scroll_down() end, {repeatable = true}},
{'UP', 'scroll_up', function() list:scroll_up() end, {repeatable = true}},
{'Ctrl+r', 'reload', function() read_disc() ; list:update() end, {}}
}
--modifies track length to escape infinite loop
if o.escape_loop then
mp.add_hook('on_preloaded', 50, function()
if mp.get_property("path", ""):find("dvd://") ~= 1 then return end
if mp.get_property('end', 'none') ~= 'none' then return end
local chapters = mp.get_property_native('chapter-list')
if not chapters then return end
local num_chapters = #chapters
-- occurs if there are no chapters
if not chapters[num_chapters] then return end
if (mp.get_property_number('duration', 0) - (chapters[num_chapters].time or 0)) > 1 then return end
msg.verbose('modifying end of the title to escape infinite loop')
mp.set_property('file-local-options/end', "#"..num_chapters)
end)
end
--if we're playing a disc then read it and modify playlist appropriately
mp.add_hook('on_load', 50, load_disc)
--if these events are disabled then the list gui functions are never called
if STANDALONE then
mp.observe_property('path', 'string', function(_,path)
if state.playing_disc then list:update() end
end)
mp.register_script_message('browse-dvd', function() list:open() end)
mp.add_key_binding('MENU', 'dvd-browser', function() list:toggle() end)
end
--module functions when loading as file_browser addon
local dvd_module = {
priority = 50,
api_version = '1.0.0',
}
function dvd_module:can_parse(directory)
return directory:sub(1,6) == "dvd://" or directory == self.get_dvd_device()
end
function dvd_module:parse()
read_disc()
local list = {}
if dvd then
for i = 1, #dvd.track do
list[i] = {
ass = get_line_str(dvd.track[i]),
name = tostring(dvd.track[i].ix-1),
path = "dvd://"..(dvd.track[i].ix-1),
type = "file"
}
end
end
return list, {
empty_text = "insert DVD",
directory_label = get_header_str(),
filtered = true,
sorted = true
}
end
return dvd_module