Skip to content

Commit

Permalink
add save screenshot and playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 24, 2024
1 parent 7fb5b73 commit 0ee94da
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions lua/dialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local opts = require('mp.options')
local utils = require('mp.utils')
local msg = require('mp.msg')

-- user options
local o = {
Expand All @@ -15,6 +16,8 @@ local o = {
opts.read_options(o)

local open_action = ''
local save_action = ''
local save_arg1 = nil

-- open bluray iso or dir
local function open_bluray(path)
Expand All @@ -28,6 +31,36 @@ local function open_dvd(path)
mp.commandv('loadfile', 'dvd://')
end

-- check if path is url
local function check_url(path)
return type(path) == 'string' and (path:find("^%a[%w.+-]-://") or path:find("^%a[%w.+-]-:%?"))
end

-- write m3u8 playlist
local function write_playlist(path)
local playlist = mp.get_property_native('playlist')
if #playlist == 0 then return end

local file, err = io.open(path, 'w')
if not file then
msg.error('save: failed to open file: ' .. err)
return
end
file:write('#EXTM3U\n')
local pwd = mp.get_property("working-directory")
for _, item in ipairs(playlist) do
local fullpath = item.filename
if not check_url(fullpath) then
fullpath = utils.join_path(pwd, fullpath)
end
if item.title and item.title ~= '' then
file:write('#EXTINF:-1, ', item.title, '\n')
end
file:write(fullpath, '\n')
end
file:close()
end

-- open a single file
local function open_file(i, path, action)
if action == 'add-sub' then
Expand Down Expand Up @@ -66,6 +99,16 @@ local function open_folder_cb(path)
end
end

-- save callback
local function save_cb(path)
if save_action == 'screenshot' then
local arg = save_arg1 or 'subtitles'
mp.commandv('screenshot-to-file', path, arg)
elseif save_action == 'playlist' then
write_playlist(path)
end
end

-- clipboard callback
local function clipboard_cb(clipboard)
mp.osd_message('clipboard: ' .. clipboard)
Expand All @@ -79,6 +122,7 @@ end
-- handle message replies
mp.register_script_message('dialog-open-multi-reply', open_cb)
mp.register_script_message('dialog-open-folder-reply', open_folder_cb)
mp.register_script_message('dialog-save-reply', save_cb)
mp.register_script_message('clipboard-get-reply', clipboard_cb)

-- open dialog
Expand All @@ -93,13 +137,13 @@ mp.register_script_message('open', function(action)
open_action = action
local filters = {}

if action == 'add-sub' then
if open_action == 'add-sub' then
append(filters, 'Subtitle Files', 'subtitle_exts')
elseif action == 'add-video' then
elseif open_action == 'add-video' then
append(filters, 'Video Files', 'video_exts')
elseif action == 'add-audio' then
elseif open_action == 'add-audio' then
append(filters, 'Audio Files', 'audio_exts')
elseif action == 'bd-iso' or action == 'dvd-iso' then
elseif open_action == 'bd-iso' or open_action == 'dvd-iso' then
append_raw(filters, 'ISO Files', '*.iso')
else
append(filters, 'Video Files', 'video_exts')
Expand All @@ -108,7 +152,7 @@ mp.register_script_message('open', function(action)
append(filters, 'Playlist Files', 'playlist_exts')
end

if action ~= 'bd-iso' and action ~= 'dvd-iso' then
if open_action ~= 'bd-iso' and open_action ~= 'dvd-iso' then
append_raw(filters, 'All Files', '*.*')
end

Expand All @@ -121,6 +165,29 @@ mp.register_script_message('open-folder', function()
mp.commandv('script-message-to', 'menu', 'dialog/open-folder', mp.get_script_name())
end)

-- save dialog
mp.register_script_message('save', function(action, arg1)
save_action = action
save_arg1 = arg1
if save_action == 'screenshot' then
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'JPEG Image', spec = '*.jpg' },
{ name = 'PNG Image', spec = '*.png' },
{ name = 'WebP Image', spec = '*.webp' },
})
local filename = mp.get_property('filename/no-ext') or ('screenshot-' .. os.time())
mp.set_property_native('user-data/menu/dialog/default-name', filename)
elseif save_action == 'playlist' then
mp.set_property_native('user-data/menu/dialog/filters', {
{ name = 'M3U8 Playlist', spec = '*.m3u8' },
})
mp.set_property_native('user-data/menu/dialog/default-name', 'playlist-' .. os.time())
else
return
end
mp.commandv('script-message-to', 'menu', 'dialog/save', mp.get_script_name())
end)

-- open clipboard
mp.register_script_message('open-clipboard', function(action)
open_action = action
Expand Down

0 comments on commit 0ee94da

Please sign in to comment.