2023-08-02 10:35:46 -07:00
|
|
|
-- mpv-youtube-queue.lua
|
|
|
|
--
|
|
|
|
-- YouTube 'Add To Queue' for mpv
|
|
|
|
--
|
|
|
|
-- Copyright (C) 2023 sudacode
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
local mp = require 'mp'
|
|
|
|
mp.options = require 'mp.options'
|
2023-08-08 00:49:31 -07:00
|
|
|
local utils = require 'mp.utils'
|
2023-08-09 01:24:24 -07:00
|
|
|
local assdraw = require 'mp.assdraw'
|
2023-08-03 00:30:29 -07:00
|
|
|
local styleOn = mp.get_property("osd-ass-cc/0")
|
|
|
|
local styleOff = mp.get_property("osd-ass-cc/1")
|
2023-08-09 20:18:26 -07:00
|
|
|
local YouTubeQueue = {}
|
|
|
|
local video_queue = {}
|
|
|
|
local MSG_DURATION = 1.5
|
|
|
|
local index = 0
|
|
|
|
local selected_index = 1
|
|
|
|
local display_offset = 0
|
|
|
|
local marked_index = nil
|
|
|
|
local current_video = nil
|
|
|
|
local destroyer = nil
|
|
|
|
local timeout
|
2023-08-02 10:35:46 -07:00
|
|
|
|
|
|
|
local options = {
|
|
|
|
add_to_queue = "ctrl+a",
|
2023-08-05 12:54:18 -07:00
|
|
|
download_current_video = "ctrl+d",
|
2023-08-06 23:30:28 -07:00
|
|
|
download_selected_video = "ctrl+D",
|
2023-08-07 02:17:05 -07:00
|
|
|
move_cursor_down = "ctrl+j",
|
|
|
|
move_cursor_up = "ctrl+k",
|
2023-08-05 12:54:18 -07:00
|
|
|
move_video = "ctrl+m",
|
2023-08-02 10:35:46 -07:00
|
|
|
play_next_in_queue = "ctrl+n",
|
2023-08-06 23:30:28 -07:00
|
|
|
open_video_in_browser = "ctrl+o",
|
|
|
|
open_channel_in_browser = "ctrl+O",
|
2023-08-02 10:35:46 -07:00
|
|
|
play_previous_in_queue = "ctrl+p",
|
|
|
|
print_current_video = "ctrl+P",
|
2023-08-05 12:54:18 -07:00
|
|
|
print_queue = "ctrl+q",
|
2023-08-06 17:31:05 -07:00
|
|
|
remove_from_queue = "ctrl+x",
|
2023-08-06 23:30:28 -07:00
|
|
|
play_selected_video = "ctrl+ENTER",
|
2023-08-05 12:54:18 -07:00
|
|
|
browser = "firefox",
|
2023-08-06 23:30:28 -07:00
|
|
|
clipboard_command = "xclip -o",
|
2023-08-06 02:22:44 -07:00
|
|
|
cursor_icon = "➤",
|
2023-08-09 01:24:24 -07:00
|
|
|
display_limit = 10,
|
2023-08-04 11:20:58 -07:00
|
|
|
download_directory = "~/videos/YouTube",
|
2023-08-05 12:54:18 -07:00
|
|
|
download_quality = "720p",
|
2023-08-06 23:30:28 -07:00
|
|
|
downloader = "curl",
|
2023-08-05 12:54:18 -07:00
|
|
|
font_name = "JetBrains Mono",
|
2023-08-06 03:13:13 -07:00
|
|
|
font_size = 12,
|
2023-08-06 23:30:28 -07:00
|
|
|
marked_icon = "⇅",
|
2023-08-09 23:14:00 -07:00
|
|
|
menu_timeout = 5,
|
2023-08-07 02:17:05 -07:00
|
|
|
show_errors = true,
|
2023-08-07 17:46:25 -07:00
|
|
|
ytdlp_file_format = "mp4",
|
2023-08-06 23:30:28 -07:00
|
|
|
ytdlp_output_template = "%(uploader)s/%(title)s.%(ext)s"
|
2023-08-03 00:49:56 -07:00
|
|
|
}
|
2023-08-06 00:56:38 -07:00
|
|
|
mp.options.read_options(options, "mpv-youtube-queue")
|
|
|
|
|
2023-08-09 20:18:26 -07:00
|
|
|
local function destroy()
|
|
|
|
timeout:kill()
|
|
|
|
mp.set_osd_ass(0, 0, "")
|
|
|
|
destroyer = nil
|
|
|
|
end
|
|
|
|
|
2023-08-09 23:14:00 -07:00
|
|
|
timeout = mp.add_periodic_timer(options.menu_timeout, destroy)
|
2023-08-09 20:18:26 -07:00
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
-- STYLE {{{
|
2023-08-03 00:49:56 -07:00
|
|
|
local colors = {
|
|
|
|
error = "676EFF",
|
2023-08-03 20:35:48 -07:00
|
|
|
selected = "F993BD",
|
|
|
|
hover_selected = "FAA9CA",
|
2023-08-03 15:53:59 -07:00
|
|
|
cursor = "FDE98B",
|
2023-08-03 20:35:48 -07:00
|
|
|
header = "8CFAF1",
|
|
|
|
hover = "F2F8F8",
|
2023-08-05 12:54:18 -07:00
|
|
|
text = "BFBFBF",
|
|
|
|
marked = "C679FF"
|
2023-08-03 20:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-08-03 23:18:12 -07:00
|
|
|
local notransparent = "\\alpha&H00&"
|
2023-08-06 03:13:13 -07:00
|
|
|
local semitransparent = "\\alpha&H40&"
|
|
|
|
local sortoftransparent = "\\alpha&H59&"
|
2023-08-03 23:18:12 -07:00
|
|
|
|
2023-08-03 20:35:48 -07:00
|
|
|
local style = {
|
2023-08-03 23:18:12 -07:00
|
|
|
error = "{\\c&" .. colors.error .. "&" .. notransparent .. "}",
|
|
|
|
selected = "{\\c&" .. colors.selected .. "&" .. semitransparent .. "}",
|
|
|
|
hover_selected = "{\\c&" .. colors.hover_selected .. "&\\alpha&H33&}",
|
|
|
|
cursor = "{\\c&" .. colors.cursor .. "&" .. notransparent .. "}",
|
2023-08-05 12:54:18 -07:00
|
|
|
marked = "{\\c&" .. colors.marked .. "&" .. notransparent .. "}",
|
2023-08-06 03:13:13 -07:00
|
|
|
reset = "{\\c&" .. colors.text .. "&" .. sortoftransparent .. "}",
|
2023-08-03 23:18:12 -07:00
|
|
|
header = "{\\fn" .. options.font_name .. "\\fs" .. options.font_size * 1.5 ..
|
|
|
|
"\\u1\\b1\\c&" .. colors.header .. "&" .. notransparent .. "}",
|
|
|
|
hover = "{\\c&" .. colors.hover .. "&" .. semitransparent .. "}",
|
|
|
|
font = "{\\fn" .. options.font_name .. "\\fs" .. options.font_size .. "{" ..
|
2023-08-06 03:13:13 -07:00
|
|
|
sortoftransparent .. "}"
|
2023-08-02 10:35:46 -07:00
|
|
|
}
|
2023-08-09 01:24:24 -07:00
|
|
|
-- }}}
|
2023-08-03 00:10:55 -07:00
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
-- HELPERS {{{
|
2023-08-07 23:43:30 -07:00
|
|
|
-- surround string with single quotes if it does not already have them
|
|
|
|
local function surround_with_quotes(s)
|
|
|
|
if string.sub(s, 0, 1) == "'" and string.sub(s, -1) == "'" then
|
|
|
|
return s
|
|
|
|
else
|
|
|
|
return "'" .. s .. "'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function remove_quotes(s) return string.gsub(s, "'", "") end
|
2023-08-07 23:11:51 -07:00
|
|
|
|
2023-08-02 17:45:21 -07:00
|
|
|
-- run sleep shell command for n seconds
|
|
|
|
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
|
|
|
|
2023-08-04 00:09:28 -07:00
|
|
|
local function print_osd_message(message, duration, s)
|
2023-08-05 18:18:12 -07:00
|
|
|
if s == style.error and not options.show_errors then return end
|
2023-08-09 01:24:24 -07:00
|
|
|
destroy()
|
2023-08-05 17:02:21 -07:00
|
|
|
if s == nil then s = style.font .. "{" .. notransparent .. "}" end
|
|
|
|
if duration == nil then duration = MSG_DURATION end
|
2023-08-04 00:09:28 -07:00
|
|
|
mp.osd_message(styleOn .. s .. message .. style.reset .. styleOff .. "\n",
|
|
|
|
duration)
|
2023-08-03 00:49:56 -07:00
|
|
|
end
|
|
|
|
|
2023-08-08 00:49:31 -07:00
|
|
|
-- returns true if the provided path exists and is a file
|
|
|
|
local function is_file(filepath)
|
|
|
|
local result = utils.file_info(filepath)
|
2023-08-09 01:24:24 -07:00
|
|
|
if result == nil then return false end
|
2023-08-08 00:49:31 -07:00
|
|
|
return result.is_file
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns the filename given a path (e.g. /home/user/file.txt -> file.txt)
|
|
|
|
local function split_path(filepath)
|
|
|
|
if is_file(filepath) then return utils.split_path(filepath) end
|
|
|
|
end
|
|
|
|
|
2023-08-04 00:55:16 -07:00
|
|
|
local function expanduser(path)
|
2023-08-06 02:22:44 -07:00
|
|
|
-- remove trailing slash if it exists
|
|
|
|
if string.sub(path, -1) == "/" then path = string.sub(path, 1, -2) end
|
2023-08-04 00:55:16 -07:00
|
|
|
if path:sub(1, 1) == "~" then
|
|
|
|
local home = os.getenv("HOME")
|
|
|
|
if home then
|
|
|
|
return home .. path:sub(2)
|
|
|
|
else
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function open_url_in_browser(url)
|
2023-08-07 23:11:51 -07:00
|
|
|
local command = options.browser .. " " .. surround_with_quotes(url)
|
2023-08-04 00:55:16 -07:00
|
|
|
os.execute(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function open_video_in_browser()
|
2023-08-09 02:04:24 -07:00
|
|
|
open_url_in_browser(current_video.video_url)
|
2023-08-04 00:55:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function open_channel_in_browser()
|
2023-08-09 02:04:24 -07:00
|
|
|
open_url_in_browser(current_video.channel_url)
|
2023-08-04 00:55:16 -07:00
|
|
|
end
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
-- local function _print_internal_playlist()
|
|
|
|
-- local count = mp.get_property_number("playlist-count")
|
|
|
|
-- print("Playlist contents:")
|
|
|
|
-- for i = 0, count - 1 do
|
|
|
|
-- local uri = mp.get_property(string.format("playlist/%d/filename", i))
|
|
|
|
-- print(string.format("%d: %s", i, uri))
|
|
|
|
-- end
|
2023-08-05 17:02:21 -07:00
|
|
|
-- end
|
2023-08-02 16:11:35 -07:00
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
local function toggle_print()
|
|
|
|
if destroyer ~= nil then
|
|
|
|
destroyer()
|
|
|
|
else
|
|
|
|
YouTubeQueue.print_queue()
|
|
|
|
end
|
|
|
|
end
|
2023-08-02 13:02:52 -07:00
|
|
|
-- }}}
|
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
-- QUEUE GETTERS AND SETTERS {{{
|
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
function YouTubeQueue.get_video_at(idx)
|
|
|
|
if idx <= 0 or idx > #video_queue then
|
2023-08-04 00:09:28 -07:00
|
|
|
print_osd_message("Invalid video index", MSG_DURATION, style.error)
|
2023-08-02 13:02:52 -07:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return video_queue[idx]
|
|
|
|
end
|
|
|
|
|
2023-08-05 17:18:35 -07:00
|
|
|
-- returns the content of the clipboard
|
|
|
|
function YouTubeQueue.get_clipboard_content()
|
2023-08-09 01:24:24 -07:00
|
|
|
local command, args = options.clipboard_command:match("(%S+)%s+(%S+)")
|
|
|
|
local res = mp.command_native({
|
|
|
|
name = "subprocess",
|
|
|
|
playback_only = false,
|
|
|
|
capture_stdout = true,
|
|
|
|
args = { command, args }
|
|
|
|
})
|
|
|
|
|
|
|
|
if res.status ~= 0 then
|
|
|
|
print_osd_message("Failed to get clipboard content", MSG_DURATION,
|
2023-08-05 17:18:35 -07:00
|
|
|
style.error)
|
|
|
|
return nil
|
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
|
|
|
|
return res.stdout
|
2023-08-05 17:18:35 -07:00
|
|
|
end
|
|
|
|
|
2023-08-06 17:31:05 -07:00
|
|
|
function YouTubeQueue.get_video_info(url)
|
2023-08-09 23:14:00 -07:00
|
|
|
local res = mp.command_native({
|
|
|
|
name = "subprocess",
|
|
|
|
playback_only = false,
|
|
|
|
capture_stdout = true,
|
|
|
|
args = {
|
|
|
|
"yt-dlp", "--print", "channel_url", "--print", "uploader",
|
|
|
|
"--print", "title", "--playlist-items", "1", url
|
|
|
|
}
|
|
|
|
})
|
2023-08-06 17:31:05 -07:00
|
|
|
|
2023-08-09 23:14:00 -07:00
|
|
|
if res.status ~= 0 then
|
|
|
|
print_osd_message("Failed to get video info", MSG_DURATION, style.error)
|
|
|
|
return nil
|
|
|
|
end
|
2023-08-06 17:31:05 -07:00
|
|
|
|
2023-08-09 23:14:00 -07:00
|
|
|
local channel_url, uploader, title = res.stdout:match("(.*)\n(.*)\n(.*)\n")
|
|
|
|
if channel_url == nil or uploader == nil or title == nil or channel_url ==
|
|
|
|
"" or uploader == "" or title == "" then
|
|
|
|
print_osd_message("Failed to get video info", MSG_DURATION, style.error)
|
|
|
|
return nil
|
|
|
|
end
|
2023-08-06 17:31:05 -07:00
|
|
|
|
2023-08-09 23:14:00 -07:00
|
|
|
return channel_url, uploader, title
|
2023-08-06 17:31:05 -07:00
|
|
|
end
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
function YouTubeQueue.print_current_video()
|
|
|
|
destroy()
|
|
|
|
local current = current_video
|
|
|
|
if is_file(current.video_url) then
|
|
|
|
print_osd_message("Playing: " .. current.video_name, 3)
|
|
|
|
else
|
|
|
|
print_osd_message("Playing: " .. current.video_name .. ' by ' ..
|
|
|
|
current.channel_name, 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- QUEUE FUNCTIONS {{{
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
-- Function to set the next or previous video in the queue as the current video
|
|
|
|
-- direction can be "NEXT" or "PREV". If nil, "next" is assumed
|
|
|
|
-- Returns nil if there are no more videos in the queue
|
|
|
|
function YouTubeQueue.set_video(direction)
|
|
|
|
local amt
|
|
|
|
direction = string.upper(direction)
|
|
|
|
if (direction == "NEXT" or direction == nil) then
|
|
|
|
amt = 1
|
|
|
|
elseif (direction == "PREV" or direction == "PREVIOUS") then
|
|
|
|
amt = -1
|
|
|
|
else
|
|
|
|
print_osd_message("Invalid direction: " .. direction, MSG_DURATION,
|
|
|
|
style.error)
|
|
|
|
return nil
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
if index + amt > #video_queue or index + amt == 0 then return nil end
|
|
|
|
index = index + amt
|
|
|
|
selected_index = index
|
|
|
|
current_video = video_queue[index]
|
|
|
|
return current_video
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function YouTubeQueue.is_in_queue(url)
|
2023-08-04 00:55:16 -07:00
|
|
|
for _, v in ipairs(video_queue) do
|
|
|
|
if v.video_url == url then return true end
|
|
|
|
end
|
2023-08-02 10:35:46 -07:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-08-02 11:07:24 -07:00
|
|
|
-- Function to find the index of the currently playing video
|
|
|
|
function YouTubeQueue.update_current_index()
|
2023-08-03 10:36:32 -07:00
|
|
|
if #video_queue == 0 then return end
|
2023-08-05 12:54:18 -07:00
|
|
|
local current_url = mp.get_property("path")
|
2023-08-02 11:07:24 -07:00
|
|
|
for i, v in ipairs(video_queue) do
|
2023-08-04 00:55:16 -07:00
|
|
|
if v.video_url == current_url then
|
2023-08-02 11:07:24 -07:00
|
|
|
index = i
|
2023-08-04 09:02:01 -07:00
|
|
|
selected_index = index
|
|
|
|
current_video = YouTubeQueue.get_video_at(index)
|
2023-08-02 11:07:24 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- if not found, reset the index
|
|
|
|
index = 0
|
|
|
|
end
|
|
|
|
|
2023-08-05 12:54:18 -07:00
|
|
|
function YouTubeQueue.mark_and_move_video()
|
|
|
|
if marked_index == nil and selected_index ~= index then
|
|
|
|
-- Mark the currently selected video for moving
|
|
|
|
marked_index = selected_index
|
|
|
|
else
|
|
|
|
-- Move the previously marked video to the selected position
|
|
|
|
YouTubeQueue.reorder_queue(marked_index, selected_index)
|
|
|
|
-- print_osd_message("Video moved to the selected position.", 1.5)
|
|
|
|
marked_index = nil -- Reset the marked index
|
|
|
|
end
|
|
|
|
-- Refresh the queue display
|
|
|
|
YouTubeQueue.print_queue()
|
|
|
|
end
|
|
|
|
|
|
|
|
function YouTubeQueue.reorder_queue(from_index, to_index)
|
|
|
|
if from_index == to_index or to_index == index then
|
|
|
|
print_osd_message("No changes made.", 1.5)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Check if the provided indices are within the bounds of the video_queue
|
|
|
|
if from_index > 0 and from_index <= #video_queue and to_index > 0 and
|
|
|
|
to_index <= #video_queue then
|
|
|
|
-- Swap the videos between the two provided indices in the video_queue
|
|
|
|
local temp_video = video_queue[from_index]
|
|
|
|
table.remove(video_queue, from_index)
|
|
|
|
table.insert(video_queue, to_index, temp_video)
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
-- swap the videos in the mpv playlist
|
|
|
|
-- playlist-move is 0-indexed and works the opposite of what is expected
|
|
|
|
-- ex: playlist-move 1 2 will move the video at index 2 to index 1
|
|
|
|
mp.commandv("loadfile", video_queue[to_index].video_url, "append")
|
|
|
|
mp.commandv("playlist-move", #video_queue, to_index - 1)
|
|
|
|
mp.commandv("playlist-move", to_index - 1, from_index - 1)
|
|
|
|
mp.commandv("playlist-move", from_index - 1, #video_queue)
|
|
|
|
mp.commandv("playlist-remove", #video_queue)
|
2023-08-05 12:54:18 -07:00
|
|
|
else
|
|
|
|
print_osd_message("Invalid indices for reordering. No changes made.",
|
|
|
|
MSG_DURATION, style.error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
function YouTubeQueue.print_queue(duration)
|
2023-08-09 01:24:24 -07:00
|
|
|
timeout:kill()
|
|
|
|
timeout:resume()
|
|
|
|
local ass = assdraw.ass_new()
|
2023-08-02 17:45:21 -07:00
|
|
|
local current_index = index
|
|
|
|
if #video_queue > 0 then
|
2023-08-09 20:18:26 -07:00
|
|
|
local start_index = math.max(1,
|
|
|
|
selected_index - options.display_limit / 2)
|
|
|
|
local end_index = math.min(#video_queue,
|
|
|
|
start_index + options.display_limit - 1)
|
2023-08-02 17:45:21 -07:00
|
|
|
display_offset = start_index - 1
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
ass:append(
|
|
|
|
style.header .. "MPV-YOUTUBE-QUEUE{\\u0\\b0}" .. style.reset ..
|
|
|
|
style.font .. "\n")
|
|
|
|
local message
|
2023-08-02 17:45:21 -07:00
|
|
|
for i = start_index, end_index do
|
2023-08-03 20:35:48 -07:00
|
|
|
local prefix = (i == selected_index) and style.cursor ..
|
2023-08-09 18:50:54 -07:00
|
|
|
options.cursor_icon .. "\\h" .. style.reset or
|
|
|
|
"\\h\\h\\h"
|
2023-08-03 20:35:48 -07:00
|
|
|
if i == current_index and i == selected_index then
|
2023-08-09 01:24:24 -07:00
|
|
|
message = prefix .. style.hover_selected .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
2023-08-05 12:54:18 -07:00
|
|
|
video_queue[i].channel_name .. ")" .. style.reset
|
2023-08-03 20:35:48 -07:00
|
|
|
elseif i == current_index then
|
2023-08-09 01:24:24 -07:00
|
|
|
message = prefix .. style.selected .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
2023-08-05 12:54:18 -07:00
|
|
|
video_queue[i].channel_name .. ")" .. style.reset
|
2023-08-03 20:35:48 -07:00
|
|
|
elseif i == selected_index then
|
2023-08-09 01:24:24 -07:00
|
|
|
message = prefix .. style.hover .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
2023-08-05 12:54:18 -07:00
|
|
|
video_queue[i].channel_name .. ")" .. style.reset
|
2023-08-03 00:10:55 -07:00
|
|
|
else
|
2023-08-09 01:24:24 -07:00
|
|
|
message = prefix .. style.reset .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
2023-08-05 12:54:18 -07:00
|
|
|
video_queue[i].channel_name .. ")" .. style.reset
|
|
|
|
end
|
|
|
|
if i == marked_index then
|
|
|
|
message =
|
|
|
|
message .. " " .. style.marked .. options.marked_icon ..
|
|
|
|
style.reset .. "\n"
|
|
|
|
else
|
|
|
|
message = message .. "\n"
|
2023-08-03 00:10:55 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
ass:append(style.font .. message)
|
|
|
|
end
|
|
|
|
mp.set_osd_ass(0, 0, ass.text)
|
|
|
|
if duration ~= nil then
|
|
|
|
mp.add_timeout(duration, function() destroy() end)
|
2023-08-02 13:02:52 -07:00
|
|
|
end
|
|
|
|
else
|
2023-08-03 10:36:32 -07:00
|
|
|
print_osd_message("No videos in the queue or history.", duration,
|
2023-08-04 00:09:28 -07:00
|
|
|
style.error)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
destroyer = destroy
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
function YouTubeQueue.move_cursor(amt)
|
|
|
|
timeout:kill()
|
|
|
|
timeout:resume()
|
2023-08-08 02:21:24 -07:00
|
|
|
selected_index = selected_index - amt
|
|
|
|
if selected_index < 1 then
|
|
|
|
selected_index = 1
|
|
|
|
elseif selected_index > #video_queue then
|
|
|
|
selected_index = #video_queue
|
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
if amt == 1 and selected_index > 1 and selected_index < display_offset + 1 then
|
2023-08-08 02:21:24 -07:00
|
|
|
display_offset = display_offset - math.abs(selected_index - amt)
|
2023-08-09 01:24:24 -07:00
|
|
|
elseif amt == -1 and selected_index < #video_queue and selected_index >
|
2023-08-09 20:18:26 -07:00
|
|
|
display_offset + options.display_limit then
|
2023-08-08 02:21:24 -07:00
|
|
|
display_offset = display_offset + math.abs(selected_index - amt)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
YouTubeQueue.print_queue()
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
2023-08-05 17:02:21 -07:00
|
|
|
function YouTubeQueue.play_video_at(idx)
|
2023-08-09 01:24:24 -07:00
|
|
|
if idx <= 0 or idx > #video_queue then
|
2023-08-04 00:09:28 -07:00
|
|
|
print_osd_message("Invalid video index", MSG_DURATION, style.error)
|
2023-08-03 00:10:55 -07:00
|
|
|
return nil
|
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
index = idx
|
|
|
|
selected_index = idx
|
2023-08-03 00:10:55 -07:00
|
|
|
mp.set_property_number("playlist-pos", index - 1) -- zero-based index
|
2023-08-09 01:24:24 -07:00
|
|
|
YouTubeQueue.print_current_video()
|
2023-08-09 02:04:24 -07:00
|
|
|
return current_video
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- play the next video in the queue
|
2023-08-09 01:24:24 -07:00
|
|
|
function YouTubeQueue.play_video(direction)
|
|
|
|
direction = string.upper(direction)
|
|
|
|
local video = YouTubeQueue.set_video(direction)
|
|
|
|
if video == nil then
|
|
|
|
print_osd_message("No video available.", MSG_DURATION, style.error)
|
2023-08-04 00:09:28 -07:00
|
|
|
return
|
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
current_video = video
|
|
|
|
selected_index = index
|
2023-08-07 02:17:05 -07:00
|
|
|
-- if the current video is not the first in the queue, then play the video
|
|
|
|
-- else, check if the video is playing and if not play the video with replace
|
2023-08-09 01:24:24 -07:00
|
|
|
if direction == "NEXT" and #video_queue > 1 then
|
|
|
|
YouTubeQueue.play_video_at(index)
|
|
|
|
elseif direction == "NEXT" and #video_queue == 1 then
|
2023-08-05 18:08:48 -07:00
|
|
|
local state = mp.get_property("core-idle")
|
2023-08-09 01:24:24 -07:00
|
|
|
-- yes if the video is loaded but not currently playing
|
2023-08-05 18:08:48 -07:00
|
|
|
if state == "yes" then
|
2023-08-09 01:24:24 -07:00
|
|
|
mp.commandv("loadfile", video.video_url, "replace")
|
2023-08-05 18:08:48 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
elseif direction == "PREV" or direction == "PREVIOUS" then
|
|
|
|
mp.set_property_number("playlist-pos", index - 1)
|
2023-08-02 13:24:18 -07:00
|
|
|
end
|
2023-08-09 01:24:24 -07:00
|
|
|
YouTubeQueue.print_current_video()
|
2023-08-02 17:45:21 -07:00
|
|
|
sleep(MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
2023-08-07 23:11:51 -07:00
|
|
|
-- add the video to the queue from the clipboard or call from script-message
|
|
|
|
-- 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
|
2023-08-05 17:02:21 -07:00
|
|
|
if url == nil or url == "" then
|
|
|
|
url = YouTubeQueue.get_clipboard_content()
|
|
|
|
if url == nil or url == "" then
|
2023-08-05 12:54:18 -07:00
|
|
|
print_osd_message("Nothing found in the clipboard.", MSG_DURATION,
|
|
|
|
style.error)
|
|
|
|
return
|
|
|
|
end
|
2023-08-02 16:11:35 -07:00
|
|
|
end
|
2023-08-05 18:18:12 -07:00
|
|
|
if YouTubeQueue.is_in_queue(url) then
|
|
|
|
print_osd_message("Video already in queue.", MSG_DURATION, style.error)
|
|
|
|
return
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
2023-08-07 23:11:51 -07:00
|
|
|
|
2023-08-08 01:54:07 -07:00
|
|
|
local video, channel_url, channel_name, video_name
|
|
|
|
if not is_file(url) then
|
2023-08-07 23:11:51 -07:00
|
|
|
channel_url, channel_name, video_name = YouTubeQueue.get_video_info(url)
|
2023-08-07 23:43:30 -07:00
|
|
|
url = remove_quotes(url)
|
2023-08-07 23:11:51 -07:00
|
|
|
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)
|
2023-08-09 23:14:00 -07:00
|
|
|
return
|
2023-08-07 23:11:51 -07:00
|
|
|
else
|
|
|
|
video = {
|
|
|
|
video_url = url,
|
|
|
|
video_name = video_name,
|
|
|
|
channel_url = channel_url,
|
|
|
|
channel_name = channel_name
|
|
|
|
}
|
|
|
|
end
|
2023-08-08 01:54:07 -07:00
|
|
|
else
|
|
|
|
channel_url, video_name = split_path(url)
|
|
|
|
if channel_url == nil or video_name == nil or channel_url == "" or
|
|
|
|
video_name == "" then
|
|
|
|
print_osd_message("Error getting video info.", MSG_DURATION,
|
|
|
|
style.error)
|
2023-08-09 23:14:00 -07:00
|
|
|
return
|
2023-08-08 01:54:07 -07:00
|
|
|
end
|
|
|
|
video = {
|
|
|
|
video_url = url,
|
|
|
|
video_name = video_name,
|
|
|
|
channel_url = channel_url,
|
|
|
|
channel_name = "Local file"
|
|
|
|
}
|
2023-08-03 00:30:29 -07:00
|
|
|
end
|
|
|
|
|
2023-08-05 17:02:21 -07:00
|
|
|
table.insert(video_queue, video)
|
2023-08-07 02:17:05 -07:00
|
|
|
-- if the queue was empty, start playing the video
|
|
|
|
-- otherwise, add the video to the playlist
|
2023-08-09 02:04:24 -07:00
|
|
|
if not current_video then
|
2023-08-09 01:24:24 -07:00
|
|
|
YouTubeQueue.play_video("NEXT")
|
2023-08-07 23:11:51 -07:00
|
|
|
elseif update_internal_playlist == 0 then
|
2023-08-02 10:35:46 -07:00
|
|
|
mp.commandv("loadfile", url, "append-play")
|
|
|
|
end
|
2023-08-07 23:11:51 -07:00
|
|
|
print_osd_message("Added " .. video_name .. " to queue.", MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
2023-08-06 23:30:28 -07:00
|
|
|
function YouTubeQueue.download_video_at(idx)
|
2023-08-09 02:04:24 -07:00
|
|
|
if idx < 0 or idx > #video_queue then return end
|
2023-08-06 23:30:28 -07:00
|
|
|
local v = video_queue[idx]
|
2023-08-09 02:04:24 -07:00
|
|
|
if is_file(v.video_url) then
|
|
|
|
print_osd_message("Current video is a local file... doing nothing.",
|
|
|
|
MSG_DURATION, style.error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local o = options
|
2023-08-06 23:30:28 -07:00
|
|
|
local q = o.download_quality:sub(1, -2)
|
|
|
|
local dl_dir = expanduser(o.download_directory)
|
|
|
|
|
2023-08-09 01:24:24 -07:00
|
|
|
print_osd_message("Downloading " .. v.video_name .. "...", MSG_DURATION)
|
2023-08-06 23:30:28 -07:00
|
|
|
-- Run the download command
|
2023-08-09 01:24:24 -07:00
|
|
|
mp.command_native_async({
|
|
|
|
name = "subprocess",
|
|
|
|
capture_stderr = true,
|
|
|
|
detach = true,
|
|
|
|
args = {
|
|
|
|
"yt-dlp", "-f",
|
|
|
|
"bestvideo[height<=" .. q .. "][ext=" .. options.ytdlp_file_format ..
|
|
|
|
"]+bestaudio/best[height<=" .. q .. "]/bestvideo[height<=" .. q ..
|
|
|
|
"]+bestaudio/best[height<=" .. q .. "]", "-o",
|
|
|
|
dl_dir .. "/" .. options.ytdlp_output_template, "--downloader",
|
|
|
|
o.downloader, "--", v.video_url
|
|
|
|
}
|
|
|
|
}, function(success, _, err)
|
|
|
|
if success then
|
|
|
|
print_osd_message("Finished downloading " .. v.video_name .. ".",
|
|
|
|
MSG_DURATION)
|
|
|
|
else
|
|
|
|
print_osd_message("Error downloading " .. v.video_name .. ": " ..
|
|
|
|
err, MSG_DURATION, style.error)
|
|
|
|
end
|
|
|
|
end)
|
2023-08-06 23:30:28 -07:00
|
|
|
end
|
|
|
|
|
2023-08-06 17:31:05 -07:00
|
|
|
function YouTubeQueue.remove_from_queue()
|
|
|
|
if index == selected_index then
|
|
|
|
print_osd_message("Cannot remove current video", MSG_DURATION,
|
|
|
|
style.error)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
table.remove(video_queue, selected_index)
|
|
|
|
mp.commandv("playlist-remove", selected_index - 1)
|
|
|
|
print_osd_message("Deleted " .. current_video.video_name .. " from queue.",
|
|
|
|
MSG_DURATION)
|
|
|
|
if selected_index > 1 then selected_index = selected_index - 1 end
|
|
|
|
index = index - 1
|
|
|
|
YouTubeQueue.print_queue()
|
|
|
|
end
|
|
|
|
|
2023-08-05 17:02:21 -07:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- LISTENERS {{{
|
2023-08-04 11:45:41 -07:00
|
|
|
-- Function to be called when the end-file event is triggered
|
|
|
|
local function on_end_file(event)
|
|
|
|
if event.reason == "eof" then -- The file ended normally
|
|
|
|
YouTubeQueue.update_current_index()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Function to be called when the track-changed event is triggered
|
|
|
|
local function on_track_changed() YouTubeQueue.update_current_index() end
|
|
|
|
|
|
|
|
-- Function to be called when the playback-restart event is triggered
|
2023-08-05 12:54:18 -07:00
|
|
|
local function on_playback_restart()
|
|
|
|
local playlist_size = mp.get_property_number("playlist-count", 0)
|
2023-08-07 23:11:51 -07:00
|
|
|
if current_video ~= nil and playlist_size > 1 then
|
2023-08-05 12:54:18 -07:00
|
|
|
YouTubeQueue.update_current_index()
|
2023-08-06 03:13:13 -07:00
|
|
|
elseif current_video == nil then
|
2023-08-05 12:54:18 -07:00
|
|
|
local url = mp.get_property("path")
|
2023-08-05 18:18:12 -07:00
|
|
|
YouTubeQueue.add_to_queue(url)
|
2023-08-05 12:54:18 -07:00
|
|
|
end
|
|
|
|
end
|
2023-08-04 11:45:41 -07:00
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- KEY BINDINGS {{{
|
2023-08-05 17:02:21 -07:00
|
|
|
mp.add_key_binding(options.add_to_queue, "add_to_queue",
|
|
|
|
YouTubeQueue.add_to_queue)
|
2023-08-02 14:30:25 -07:00
|
|
|
mp.add_key_binding(options.play_next_in_queue, "play_next_in_queue",
|
2023-08-09 01:24:24 -07:00
|
|
|
function() YouTubeQueue.play_video("NEXT") end)
|
|
|
|
mp.add_key_binding(options.play_previous_in_queue, "play_prev_in_queue",
|
|
|
|
function() YouTubeQueue.play_video("PREV") end)
|
|
|
|
mp.add_key_binding(options.print_queue, "print_queue", toggle_print)
|
2023-08-06 01:15:13 -07:00
|
|
|
mp.add_key_binding(options.move_cursor_up, "move_cursor_up",
|
2023-08-09 01:24:24 -07:00
|
|
|
function() YouTubeQueue.move_cursor(1) end,
|
|
|
|
{ repeatable = true })
|
2023-08-06 01:15:13 -07:00
|
|
|
mp.add_key_binding(options.move_cursor_down, "move_cursor_down",
|
2023-08-09 01:24:24 -07:00
|
|
|
function() YouTubeQueue.move_cursor(-1) end,
|
|
|
|
{ repeatable = true })
|
2023-08-02 14:30:25 -07:00
|
|
|
mp.add_key_binding(options.play_selected_video, "play_selected_video",
|
2023-08-09 02:04:24 -07:00
|
|
|
function() YouTubeQueue.play_video_at(selected_index) end)
|
2023-08-02 14:30:25 -07:00
|
|
|
mp.add_key_binding(options.open_video_in_browser, "open_video_in_browser",
|
|
|
|
open_video_in_browser)
|
|
|
|
mp.add_key_binding(options.print_current_video, "print_current_video",
|
2023-08-09 01:24:24 -07:00
|
|
|
YouTubeQueue.print_current_video)
|
2023-08-02 16:11:35 -07:00
|
|
|
mp.add_key_binding(options.open_channel_in_browser, "open_channel_in_browser",
|
|
|
|
open_channel_in_browser)
|
2023-08-04 00:55:16 -07:00
|
|
|
mp.add_key_binding(options.download_current_video, "download_current_video",
|
2023-08-09 02:04:24 -07:00
|
|
|
function() YouTubeQueue.download_video_at(index) end)
|
2023-08-06 23:30:28 -07:00
|
|
|
mp.add_key_binding(options.download_selected_video, "download_selected_video",
|
2023-08-09 02:04:24 -07:00
|
|
|
function() YouTubeQueue.download_video_at(selected_index) end)
|
2023-08-06 01:15:13 -07:00
|
|
|
mp.add_key_binding(options.move_video, "move_video",
|
2023-08-05 12:54:18 -07:00
|
|
|
YouTubeQueue.mark_and_move_video)
|
2023-08-06 17:31:05 -07:00
|
|
|
mp.add_key_binding(options.remove_from_queue, "delete_video",
|
|
|
|
YouTubeQueue.remove_from_queue)
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-04 11:45:41 -07:00
|
|
|
mp.register_event("end-file", on_end_file)
|
|
|
|
mp.register_event("track-changed", on_track_changed)
|
|
|
|
mp.register_event("playback-restart", on_playback_restart)
|
2023-08-05 17:02:21 -07:00
|
|
|
|
|
|
|
mp.register_script_message("add_to_queue", YouTubeQueue.add_to_queue)
|
|
|
|
mp.register_script_message("print_queue", YouTubeQueue.print_queue)
|
2023-08-09 20:18:26 -07:00
|
|
|
mp.register_script_message("toggle_youtube_queue", toggle_print)
|
2023-08-02 10:35:46 -07:00
|
|
|
-- }}}
|