Skip to content

Commit

Permalink
add more open actions support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 20, 2024
1 parent 9da7fa6 commit f396ed1
Showing 1 changed file with 85 additions and 36 deletions.
121 changes: 85 additions & 36 deletions lua/dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,66 @@ local file_types = {
{ '*.srt', '*.ass', '*.idx', '*.sub', '*.sup', '*.ttxt', '*.txt', '*.ssa', '*.smi', '*.mks' }, ';'),
playlist = table.concat({ '*.m3u', '*.m3u8', '*.pls', '*.cue' }, ';'),
}
local open_action = ''

-- set file type filters
local function update_default_filters()
local dialog_filters = {
{ name = 'All Files (*.*)', spec = '*.*' },
{ name = 'Video Files', spec = file_types['video'] },
{ name = 'Audio Files', spec = file_types['audio'] },
{ name = 'Image Files', spec = file_types['image'] },
{ name = 'ISO Image Files', spec = file_types['iso'] },
{ name = 'Subtitle Files', spec = file_types['subtitle'] },
{ name = 'Playlist Files', spec = file_types['playlist'] },
}
mp.set_property_native('user-data/menu/dialog/filters', dialog_filters)
end

-- open Bluray iso or dir
-- open bluray iso or dir
local function open_bluray(path)
mp.commandv('set', 'bluray-device', path)
mp.commandv('loadfile', 'bd://')
end

-- open DVD iso or dir
-- open dvd iso or dir
local function open_dvd(path)
mp.commandv('set', 'dvd-device', path)
mp.commandv('loadfile', 'dvd://')
end

-- open callback with support for Bluray/DVD iso and subtitle file
local function open_cb(...)
-- open a single file
local function open_file(path, append)
local ext = path:match('^.+(%..+)$') or ''
local function check_file_type(ext, type)
return ext ~= '' and file_types[type]:find(ext)
end

for i, v in ipairs({ ... }) do
local path = tostring(v)
local ext = path:match('^.+(%..+)$') or ''

if check_file_type(ext, 'iso') then
local info = utils.file_info(path)
if info and info.is_file then
if info.size > 4.7 * 1000 * 1000 * 1000 then
open_bluray(path)
else
open_dvd(path)
end
break -- do not load other files
-- play iso file directly
if check_file_type(ext, 'iso') then
local info = utils.file_info(path)
if info and info.is_file then
if info.size > 4.7 * 1000 * 1000 * 1000 then
open_bluray(path)
else
open_dvd(path)
end
return
end
end

if check_file_type(ext, 'subtitle') then
mp.commandv('sub-add', path, 'auto')
else
mp.commandv('loadfile', path, append and 'append-play' or 'replace')
end
end

if check_file_type(ext, 'subtitle') then
-- open callback
local function open_cb(...)
for i, v in ipairs({ ... }) do
local path = tostring(v)
if open_action == 'add-sub' then
mp.commandv('sub-add', path, 'auto')
elseif open_action == 'add-video' then
mp.commandv('video-add', path, 'auto')
elseif open_action == 'add-audio' then
mp.commandv('audio-add', path, 'auto')
elseif open_action == 'add-playlist' then
mp.commandv('loadfile', path, 'append')
else
mp.commandv('loadfile', path, i > 1 and 'append' or 'replace')
open_file(path, i > 1)
end
end
end

-- open folder callback with support for Bluray/DVD Folder
-- open folder callback
local function open_folder_cb(path)
if utils.file_info(utils.join_path(path, 'BDMV')) then
open_bluray(path)
Expand All @@ -104,11 +106,58 @@ mp.register_script_message('dialog-open-folder-reply', open_folder_cb)
mp.register_script_message('clipboard-get-reply', clipboard_cb)


-- init default file types
update_default_filters()
-- add subtitle track
mp.register_script_message('add-sub', function()
open_action = 'add-sub'
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'Subtitle Files', spec = file_types['subtitle'] },
{ name = 'All Files (*.*)', spec = '*.*' },
})
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
end)

-- add video track
mp.register_script_message('add-video', function()
open_action = 'add-video'
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'Video Files', spec = file_types['video'] },
{ name = 'All Files (*.*)', spec = '*.*' },
})
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
end)

-- add audio track
mp.register_script_message('add-audio', function()
open_action = 'add-audio'
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'Audio Files', spec = file_types['audio'] },
{ name = 'All Files (*.*)', spec = '*.*' },
})
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
end)

-- add to playlist
mp.register_script_message('add-playlist', function()
open_action = 'add-playlist'
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'Video Files', spec = file_types['video'] },
{ name = 'All Files (*.*)', spec = '*.*' },
})
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
end)

-- open dialog
mp.register_script_message('open', function()
open_action = ''
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'All Files (*.*)', spec = '*.*' },
{ name = 'Video Files', spec = file_types['video'] },
{ name = 'Audio Files', spec = file_types['audio'] },
{ name = 'Image Files', spec = file_types['image'] },
{ name = 'ISO Image Files', spec = file_types['iso'] },
{ name = 'Subtitle Files', spec = file_types['subtitle'] },
{ name = 'Playlist Files', spec = file_types['playlist'] },
})
mp.commandv('script-message-to', 'menu', 'dialog/open-multi', mp.get_script_name())
end)

Expand Down

10 comments on commit f396ed1

@butterw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some extra filetype filtering is needed with script-message-to dialog open-folder. I get the following error displayed with one of my folders (here it loads hidden system .ini into the playlist, on another folder it tries to load .cube files):
[file] This is a directory - adding to playlist.
Playing: B:\Downloads\up/desktop.ini
Failed to recognize file format.

@butterw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dialog.lua:
Default open file type should be Media Files(.mp4, .mkv, ...) rather than (.)
The displayed lists of extensions for video and audio files are also very long.

@butterw
Copy link
Contributor

@butterw butterw commented on f396ed1 Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos in https://github.com/tsl0922/mpv-menu-plugin/wiki/Scripting
srcript instead of script.

dialog.lua: should provide a script-messages example to copy/paste a string to clipboard, not only open clipboard filepath.

-- set clipboard
mp.register_script_message('set-clipboard', function(str)
	-- print(str)
	local ctime = mp.command_native({"expand-text", str})
	print("copied to clipboard:", ctime)
	mp.commandv('show-text', ctime)
	mp.commandv('script-message-to', 'menu', 'clipboard/set', ctime)
end)

input.conf:
Ctrl+t script-message-to dialog set-clipboard ${time-pos/full} #menu: Open > Copy time to clipboard

To get a string from clipboard (ex a timestamp for a go-to-time feature) creating 'user-data/menu/clipboard' would be required in clipboard_cb
local function clipboard_cb(clipboard)
-- mp.commandv('loadfile', clipboard, 'append-play')

@tsl0922
Copy link
Owner Author

@tsl0922 tsl0922 commented on f396ed1 Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it loads hidden system .ini into the playlist

The dir is loaded by mpv, this script doesn't list dir, it just pass the dir to mpv.
Maybe we can list the dir recursively and do the filter stuff.

srcript instead of script

Fixed.

The displayed lists of extensions for video and audio files are also very long

screenshot? What do you mean, It should only show Video Files in the list.

dialog.lua: should provide a script-messages example to copy/paste a string to clipboard, not only open clipboard filepath.

Well, I can't provide examples for everything, dialog.lua is meant to be a user script that can be used directly.

@butterw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dialog.lua: should provide a script-messages example to copy/paste a string to clipboard, not only open clipboard filepath.
Well, I can't provide examples for everything, dialog.lua is meant to be a user script that can be used directly.

The problem in dialog.lua is that the clipboard callback is hardcoded to play the filepath. which causes dialog.lua to conflict with other clipboard/get uses (ex: I want to be able to display the clipboard string on screen rather than always try to open the file, I want to seek to clipboard timestamp).
Is the reply sent to all scripts or only the script that put in the clipboard/get request ? I think it would be preferable if the clipboard callback just set user-data/menu/clipboard, maybe this could be done directly in c and the clipboard-get-reply message wouldn't be required ? In my go-to-time example simply observing 'user-data/menu/clipboard' seems to work.

local function clipboard_cb(clipboard)
 mp.set_property_native('user-data/menu/clipboard', clipboard)
 -- mp.commandv('loadfile', clipboard, 'append-play')
end

mp.register_script_message('clipboard-get-reply', clipboard_cb)

@butterw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The displayed lists of extensions for video and audio files are also very long

screenshot? What do you mean, It should only show Video Files in the list.

Yes, but still very long.

video_extensions

@tsl0922
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS Version? I'm on win10:
image

@tsl0922
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which causes dialog.lua to conflict with other clipboard/get uses

No, it won't conflict, you should pass mp.get_script_name() as the last argument:

mp.commandv('script-message-to', 'menu', 'clipboard/get', mp.get_script_name())

@butterw
Copy link
Contributor

@butterw butterw commented on f396ed1 Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS Version? I'm on win10

Win10, Latest.
default App mode: Light.

@tsl0922
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a commit with new message design, and reduced the file extensions.

you may read the updated usage here: https://github.com/tsl0922/mpv-menu-plugin/wiki/File-Dialog

Please sign in to comment.