mirror of
https://github.com/ksyasuda/mpv-youtube-queue.git
synced 2024-10-28 04:44:11 -07:00
update add_to_queue function to allow adding local files to the queue (#7)
* update add_to_queue function to allow adding local files to the queue * update functions to work with local files - change download to do nothing on local file - change print function to not try to print channel name - change move cursor up and down to be repeatable
This commit is contained in:
parent
6e3d782081
commit
64bde58672
@ -91,9 +91,26 @@ local current_video = nil
|
|||||||
|
|
||||||
-- HELPERS {{{
|
-- HELPERS {{{
|
||||||
|
|
||||||
|
-- surround string with single quotes
|
||||||
|
local function surround_with_quotes(s) return '\'' .. s .. '\'' end
|
||||||
|
|
||||||
-- run sleep shell command for n seconds
|
-- run sleep shell command for n seconds
|
||||||
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
||||||
|
|
||||||
|
-- returns true if the provided path exists and is a file
|
||||||
|
local function is_file(filepath)
|
||||||
|
local result = os.execute("test -f " .. surround_with_quotes(filepath))
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
-- returns the filename given a path (e.g. /home/user/file.txt -> file.txt)
|
||||||
|
local function get_filename(filepath) return string.match(filepath, ".+/(.+)$") end
|
||||||
|
|
||||||
|
-- return the directory given a path (e.g. /home/user/file.txt -> /home/user)
|
||||||
|
local function get_directory(filepath)
|
||||||
|
return surround_with_quotes(string.match(filepath, "(.+)/.+"))
|
||||||
|
end
|
||||||
|
|
||||||
local function print_osd_message(message, duration, s)
|
local function print_osd_message(message, duration, s)
|
||||||
if s == style.error and not options.show_errors then return end
|
if s == style.error and not options.show_errors then return end
|
||||||
if s == nil then s = style.font .. "{" .. notransparent .. "}" end
|
if s == nil then s = style.font .. "{" .. notransparent .. "}" end
|
||||||
@ -104,8 +121,12 @@ end
|
|||||||
|
|
||||||
local function print_current_video()
|
local function print_current_video()
|
||||||
local current = YouTubeQueue.get_current_video()
|
local current = YouTubeQueue.get_current_video()
|
||||||
print_osd_message("Playing: " .. current.video_name .. ' by ' ..
|
if is_file(current.video_url) then
|
||||||
current.channel_name, 3)
|
print_osd_message("Playing: " .. current.video_name, 3)
|
||||||
|
else
|
||||||
|
print_osd_message("Playing: " .. current.video_name .. ' by ' ..
|
||||||
|
current.channel_name, 3)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function expanduser(path)
|
local function expanduser(path)
|
||||||
@ -124,7 +145,7 @@ local function expanduser(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function open_url_in_browser(url)
|
local function open_url_in_browser(url)
|
||||||
local command = options.browser .. " " .. url
|
local command = options.browser .. " " .. surround_with_quotes(url)
|
||||||
os.execute(command)
|
os.execute(command)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -187,7 +208,7 @@ end
|
|||||||
function YouTubeQueue.get_video_info(url)
|
function YouTubeQueue.get_video_info(url)
|
||||||
local command =
|
local command =
|
||||||
'yt-dlp --print channel_url --print uploader --print title --playlist-items 1 ' ..
|
'yt-dlp --print channel_url --print uploader --print title --playlist-items 1 ' ..
|
||||||
url
|
surround_with_quotes(url)
|
||||||
local handle = io.popen(command)
|
local handle = io.popen(command)
|
||||||
if handle == nil then return nil, nil, nil end
|
if handle == nil then return nil, nil, nil end
|
||||||
|
|
||||||
@ -404,8 +425,10 @@ function YouTubeQueue.play_next_in_queue()
|
|||||||
sleep(MSG_DURATION)
|
sleep(MSG_DURATION)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add the video to the queue from the clipboard
|
-- add the video to the queue from the clipboard or call from script-message
|
||||||
function YouTubeQueue.add_to_queue(url)
|
-- updates the internal playlist by default, pass 0 to disable
|
||||||
|
function YouTubeQueue.add_to_queue(url, update_internal_playlist)
|
||||||
|
if update_internal_playlist == nil then update_internal_playlist = 0 end
|
||||||
if url == nil or url == "" then
|
if url == nil or url == "" then
|
||||||
url = YouTubeQueue.get_clipboard_content()
|
url = YouTubeQueue.get_clipboard_content()
|
||||||
if url == nil or url == "" then
|
if url == nil or url == "" then
|
||||||
@ -418,29 +441,45 @@ function YouTubeQueue.add_to_queue(url)
|
|||||||
print_osd_message("Video already in queue.", MSG_DURATION, style.error)
|
print_osd_message("Video already in queue.", MSG_DURATION, style.error)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local channel_url, channel_name, video_name =
|
|
||||||
YouTubeQueue.get_video_info(url)
|
local video, channel_url, channel_name, video_name, video_url
|
||||||
if (channel_url == nil or channel_name == nil or video_name == nil) or
|
if is_file(url) then
|
||||||
(channel_url == "" or channel_name == "" or video_name == "") then
|
video_url = url
|
||||||
print_osd_message("Error getting video info.", MSG_DURATION, style.error)
|
video_name = get_filename(url)
|
||||||
return
|
channel_url = get_directory(url)
|
||||||
|
channel_name = get_directory(url)
|
||||||
|
|
||||||
|
video = {
|
||||||
|
video_url = video_url,
|
||||||
|
video_name = video_name,
|
||||||
|
channel_url = channel_url,
|
||||||
|
channel_name = channel_name
|
||||||
|
}
|
||||||
|
else
|
||||||
|
channel_url, channel_name, video_name = YouTubeQueue.get_video_info(url)
|
||||||
|
if (channel_url == nil or channel_name == nil or video_name == nil) or
|
||||||
|
(channel_url == "" or channel_name == "" or video_name == "") then
|
||||||
|
print_osd_message("Error getting video info.", MSG_DURATION,
|
||||||
|
style.error)
|
||||||
|
else
|
||||||
|
video = {
|
||||||
|
video_url = url,
|
||||||
|
video_name = video_name,
|
||||||
|
channel_url = channel_url,
|
||||||
|
channel_name = channel_name
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local video = {
|
|
||||||
video_url = url,
|
|
||||||
video_name = video_name,
|
|
||||||
channel_url = channel_url,
|
|
||||||
channel_name = channel_name
|
|
||||||
}
|
|
||||||
table.insert(video_queue, video)
|
table.insert(video_queue, video)
|
||||||
-- if the queue was empty, start playing the video
|
-- if the queue was empty, start playing the video
|
||||||
-- otherwise, add the video to the playlist
|
-- otherwise, add the video to the playlist
|
||||||
if not YouTubeQueue.get_current_video() then
|
if not YouTubeQueue.get_current_video() then
|
||||||
YouTubeQueue.play_next_in_queue()
|
YouTubeQueue.play_next_in_queue()
|
||||||
else
|
elseif update_internal_playlist == 0 then
|
||||||
mp.commandv("loadfile", url, "append-play")
|
mp.commandv("loadfile", url, "append-play")
|
||||||
print_osd_message("Added " .. video_name .. " to queue.", MSG_DURATION)
|
|
||||||
end
|
end
|
||||||
|
print_osd_message("Added " .. video_name .. " to queue.", MSG_DURATION)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- play the previous video in the queue
|
-- play the previous video in the queue
|
||||||
@ -493,6 +532,11 @@ function YouTubeQueue.download_video_at(idx)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function YouTubeQueue.download_current_video()
|
function YouTubeQueue.download_current_video()
|
||||||
|
if is_file(current_video.video_url) then
|
||||||
|
print_osd_message("Current video is a local file... doing nothing.",
|
||||||
|
MSG_DURATION, style.error)
|
||||||
|
return
|
||||||
|
end
|
||||||
if current_video ~= nil and current_video ~= "" then
|
if current_video ~= nil and current_video ~= "" then
|
||||||
YouTubeQueue.download_video_at(index)
|
YouTubeQueue.download_video_at(index)
|
||||||
else
|
else
|
||||||
@ -505,6 +549,11 @@ function YouTubeQueue.download_selected_video()
|
|||||||
print_osd_message("No video to download.", MSG_DURATION, style.error)
|
print_osd_message("No video to download.", MSG_DURATION, style.error)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
if is_file(YouTubeQueue.get_video_at(selected_index)) then
|
||||||
|
print_osd_message("Current video is a local file... doing nothing.",
|
||||||
|
MSG_DURATION, style.error)
|
||||||
|
return
|
||||||
|
end
|
||||||
YouTubeQueue.download_video_at(selected_index)
|
YouTubeQueue.download_video_at(selected_index)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -539,7 +588,7 @@ local function on_track_changed() YouTubeQueue.update_current_index() end
|
|||||||
-- Function to be called when the playback-restart event is triggered
|
-- Function to be called when the playback-restart event is triggered
|
||||||
local function on_playback_restart()
|
local function on_playback_restart()
|
||||||
local playlist_size = mp.get_property_number("playlist-count", 0)
|
local playlist_size = mp.get_property_number("playlist-count", 0)
|
||||||
if playlist_size > 1 then
|
if current_video ~= nil and playlist_size > 1 then
|
||||||
YouTubeQueue.update_current_index()
|
YouTubeQueue.update_current_index()
|
||||||
elseif current_video == nil then
|
elseif current_video == nil then
|
||||||
local url = mp.get_property("path")
|
local url = mp.get_property("path")
|
||||||
@ -558,9 +607,9 @@ mp.add_key_binding(options.play_previous_in_queue, "play_previous_video",
|
|||||||
YouTubeQueue.play_previous_video)
|
YouTubeQueue.play_previous_video)
|
||||||
mp.add_key_binding(options.print_queue, "print_queue", YouTubeQueue.print_queue)
|
mp.add_key_binding(options.print_queue, "print_queue", YouTubeQueue.print_queue)
|
||||||
mp.add_key_binding(options.move_cursor_up, "move_cursor_up",
|
mp.add_key_binding(options.move_cursor_up, "move_cursor_up",
|
||||||
YouTubeQueue.move_cursor_up)
|
YouTubeQueue.move_cursor_up, { repeatable = true })
|
||||||
mp.add_key_binding(options.move_cursor_down, "move_cursor_down",
|
mp.add_key_binding(options.move_cursor_down, "move_cursor_down",
|
||||||
YouTubeQueue.move_cursor_down)
|
YouTubeQueue.move_cursor_down, { repeatable = true })
|
||||||
mp.add_key_binding(options.play_selected_video, "play_selected_video",
|
mp.add_key_binding(options.play_selected_video, "play_selected_video",
|
||||||
YouTubeQueue.play_selected_video)
|
YouTubeQueue.play_selected_video)
|
||||||
mp.add_key_binding(options.open_video_in_browser, "open_video_in_browser",
|
mp.add_key_binding(options.open_video_in_browser, "open_video_in_browser",
|
||||||
|
Loading…
Reference in New Issue
Block a user