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'
|
|
|
|
local YouTubeQueue = {}
|
|
|
|
local video_queue = {}
|
|
|
|
local current_video = nil
|
|
|
|
local index = 0
|
2023-08-02 14:30:25 -07:00
|
|
|
local selected_index = 1
|
2023-08-02 17:45:21 -07:00
|
|
|
local MSG_DURATION = 1.5
|
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-02 10:35:46 -07:00
|
|
|
|
|
|
|
local options = {
|
|
|
|
add_to_queue = "ctrl+a",
|
|
|
|
play_next_in_queue = "ctrl+n",
|
|
|
|
play_previous_in_queue = "ctrl+p",
|
|
|
|
print_queue = "ctrl+q",
|
|
|
|
move_selection_up = "ctrl+UP",
|
|
|
|
move_selection_down = "ctrl+DOWN",
|
|
|
|
play_selected_video = "ctrl+ENTER",
|
|
|
|
open_video_in_browser = "ctrl+o",
|
2023-08-02 16:11:35 -07:00
|
|
|
open_channel_in_browser = "ctrl+O",
|
2023-08-02 10:35:46 -07:00
|
|
|
print_current_video = "ctrl+P",
|
2023-08-04 00:55:16 -07:00
|
|
|
download_current_video = "ctrl+d",
|
2023-08-02 10:35:46 -07:00
|
|
|
browser = "firefox",
|
2023-08-02 17:45:21 -07:00
|
|
|
clipboard_command = "xclip -o",
|
2023-08-03 00:10:55 -07:00
|
|
|
display_limit = 6,
|
2023-08-03 00:53:52 -07:00
|
|
|
cursor_icon = "🠺",
|
2023-08-03 23:18:12 -07:00
|
|
|
font_size = 14,
|
2023-08-04 00:55:16 -07:00
|
|
|
font_name = "JetBrains Mono",
|
|
|
|
download_quality = "720p",
|
|
|
|
download_directory = "~/Videos/YouTube"
|
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",
|
|
|
|
text = "BFBFBF"
|
|
|
|
}
|
|
|
|
|
2023-08-03 23:18:12 -07:00
|
|
|
local notransparent = "\\alpha&H00&"
|
|
|
|
local semitransparent = "\\alpha&H4D&"
|
|
|
|
local transparent = "\\alpha&H73&"
|
|
|
|
|
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 .. "}",
|
|
|
|
reset = "{\\c&" .. colors.text .. "&" .. transparent .. "}",
|
|
|
|
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 .. "{" ..
|
|
|
|
transparent .. "}"
|
2023-08-02 10:35:46 -07:00
|
|
|
}
|
2023-08-03 00:10:55 -07:00
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
mp.options.read_options(options, "mpv-youtube-queue")
|
|
|
|
|
2023-08-02 17:45:21 -07:00
|
|
|
local display_limit = options.display_limit
|
|
|
|
local display_offset = 0
|
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
-- HELPERS {{{
|
|
|
|
|
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)
|
|
|
|
if not s then s = style.font .. "{" .. notransparent .. "}" end
|
2023-08-04 00:55:16 -07:00
|
|
|
if not duration 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-04 00:09:28 -07:00
|
|
|
local function print_current_video()
|
|
|
|
print_osd_message("Playing: " .. current_video.video_name .. ' by ' ..
|
|
|
|
current_video.video_name, 3)
|
2023-08-02 13:02:52 -07:00
|
|
|
end
|
|
|
|
|
2023-08-04 00:55:16 -07:00
|
|
|
local function expanduser(path)
|
|
|
|
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)
|
|
|
|
local command = options.browser .. " " .. url
|
|
|
|
os.execute(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function open_video_in_browser()
|
|
|
|
open_url_in_browser(current_video.video_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function open_channel_in_browser()
|
|
|
|
open_url_in_browser(current_video.channel_url)
|
|
|
|
end
|
|
|
|
|
2023-08-03 23:18:12 -07:00
|
|
|
local function get_video_info(url)
|
|
|
|
local command =
|
|
|
|
'yt-dlp --print channel_url --print uploader --print title --playlist-items 1 ' ..
|
|
|
|
url
|
2023-08-02 13:02:52 -07:00
|
|
|
local handle = io.popen(command)
|
2023-08-03 23:18:12 -07:00
|
|
|
if not handle then return nil, nil, nil end
|
2023-08-02 13:02:52 -07:00
|
|
|
|
2023-08-02 16:11:35 -07:00
|
|
|
local result = handle:read("*a")
|
|
|
|
handle:close()
|
2023-08-03 23:18:12 -07:00
|
|
|
|
|
|
|
-- Split the result into URL, name, and video title
|
|
|
|
local channel_url, channel_name, video_name = result:match(
|
|
|
|
"(.-)\n(.-)\n(.*)")
|
|
|
|
|
|
|
|
-- Remove trailing whitespace
|
|
|
|
if channel_url then channel_url = channel_url:gsub("%s+$", "") end
|
|
|
|
if channel_name then channel_name = channel_name:gsub("%s+$", "") end
|
|
|
|
if video_name then video_name = video_name:gsub("%s+$", "") end
|
|
|
|
|
|
|
|
return channel_url, channel_name, video_name
|
2023-08-02 16:11:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function is_valid_ytdlp_url(url)
|
|
|
|
local command = 'yt-dlp --simulate \'' .. url .. '\' >/dev/null 2>&1'
|
|
|
|
local handle = io.popen(command .. "; echo $?")
|
2023-08-02 16:14:31 -07:00
|
|
|
if not handle then return false end
|
2023-08-02 16:11:35 -07:00
|
|
|
local result = handle:read("*a")
|
2023-08-02 16:14:31 -07:00
|
|
|
if not result then return false end
|
2023-08-02 16:11:35 -07:00
|
|
|
handle:close()
|
|
|
|
return result:gsub("%s+$", "") == "0"
|
|
|
|
end
|
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
-- }}}
|
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
-- QUEUE GETTERS AND SETTERS {{{
|
|
|
|
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.size() return #video_queue end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.get_current_index() return index end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.get_video_queue() return video_queue end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-03 00:10:55 -07:00
|
|
|
function YouTubeQueue.set_current_index(idx)
|
|
|
|
index = idx
|
|
|
|
current_video = video_queue[idx]
|
|
|
|
end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.get_current_video() return current_video end
|
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-02 10:35:46 -07:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- QUEUE FUNCTIONS {{{
|
|
|
|
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.add_to_queue(video) table.insert(video_queue, video) end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
|
|
|
-- Function to get the next video in the queue
|
|
|
|
-- Returns nil if there are no videos in the queue
|
|
|
|
function YouTubeQueue.next_in_queue()
|
|
|
|
if index < #video_queue then
|
|
|
|
index = index + 1
|
2023-08-02 16:54:02 -07:00
|
|
|
selected_index = index
|
2023-08-02 10:35:46 -07:00
|
|
|
current_video = video_queue[index]
|
|
|
|
return current_video
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function YouTubeQueue.prev_in_queue()
|
|
|
|
if index > 1 then
|
|
|
|
index = index - 1
|
2023-08-02 16:54:02 -07:00
|
|
|
selected_index = index
|
2023-08-02 10:35:46 -07:00
|
|
|
current_video = video_queue[index]
|
2023-08-04 00:09:28 -07:00
|
|
|
return current_video
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
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()
|
|
|
|
local current_url = mp.get_property("path")
|
2023-08-03 10:36:32 -07:00
|
|
|
if #video_queue == 0 then return end
|
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
|
|
|
|
|
|
|
|
-- Function to be called when the end-file event is triggered
|
|
|
|
function YouTubeQueue.on_end_file(event)
|
2023-08-02 14:30:25 -07:00
|
|
|
if event.reason == "eof" then -- The file ended normally
|
2023-08-02 11:07:24 -07:00
|
|
|
YouTubeQueue.update_current_index()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Function to be called when the track-changed event is triggered
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.on_track_changed() YouTubeQueue.update_current_index() end
|
2023-08-02 11:07:24 -07:00
|
|
|
|
|
|
|
-- Function to be called when the playback-restart event is triggered
|
2023-08-02 14:30:25 -07:00
|
|
|
function YouTubeQueue.on_playback_restart() YouTubeQueue.update_current_index() end
|
2023-08-02 10:35:46 -07:00
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
function YouTubeQueue.print_queue(duration)
|
2023-08-02 17:45:21 -07:00
|
|
|
local current_index = index
|
|
|
|
if not duration then duration = 3 end
|
|
|
|
if #video_queue > 0 then
|
|
|
|
local start_index = math.max(1, selected_index - display_limit / 2)
|
|
|
|
local end_index =
|
|
|
|
math.min(#video_queue, start_index + display_limit - 1)
|
|
|
|
display_offset = start_index - 1
|
|
|
|
|
2023-08-03 20:35:48 -07:00
|
|
|
local message =
|
2023-08-03 23:18:12 -07:00
|
|
|
styleOn .. style.header .. "MPV-YOUTUBE-QUEUE{\\u0\\b0}" ..
|
2023-08-03 20:35:48 -07:00
|
|
|
style.reset .. style.font .. "\n"
|
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-03 23:18:12 -07:00
|
|
|
options.cursor_icon .. " " .. style.reset or
|
|
|
|
" "
|
2023-08-03 20:35:48 -07:00
|
|
|
if i == current_index and i == selected_index then
|
2023-08-03 23:18:12 -07:00
|
|
|
mp.msg.log("info", "YES")
|
2023-08-03 20:35:48 -07:00
|
|
|
message =
|
|
|
|
message .. prefix .. style.hover_selected .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
|
|
|
video_queue[i].channel_name .. ")" .. style.reset ..
|
|
|
|
"\n"
|
2023-08-03 20:35:48 -07:00
|
|
|
elseif i == current_index then
|
|
|
|
message = message .. prefix .. style.selected .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
|
|
|
video_queue[i].channel_name .. ")" .. style.reset ..
|
|
|
|
"\n"
|
2023-08-03 20:35:48 -07:00
|
|
|
elseif i == selected_index then
|
|
|
|
message = message .. prefix .. style.hover .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
|
|
|
video_queue[i].channel_name .. ")" .. style.reset ..
|
|
|
|
"\n"
|
2023-08-03 00:10:55 -07:00
|
|
|
else
|
2023-08-03 20:35:48 -07:00
|
|
|
message = message .. prefix .. style.reset .. i .. ". " ..
|
2023-08-03 23:18:12 -07:00
|
|
|
video_queue[i].video_name .. " - (" ..
|
|
|
|
video_queue[i].channel_name .. ")" .. style.reset ..
|
|
|
|
"\n"
|
2023-08-03 00:10:55 -07:00
|
|
|
end
|
2023-08-02 13:02:52 -07:00
|
|
|
end
|
2023-08-03 20:35:48 -07:00
|
|
|
message = message .. styleOff
|
2023-08-02 13:02:52 -07:00
|
|
|
mp.osd_message(message, duration)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2023-08-02 13:02:52 -07:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- MAIN FUNCTIONS {{{
|
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
-- returns the content of the clipboard
|
|
|
|
local function get_clipboard_content()
|
|
|
|
local handle = io.popen(options.clipboard_command)
|
2023-08-02 16:11:35 -07:00
|
|
|
if not handle then
|
2023-08-03 10:36:32 -07:00
|
|
|
print_osd_message("Error getting clipboard content", MSG_DURATION,
|
2023-08-04 00:09:28 -07:00
|
|
|
style.error)
|
2023-08-02 16:11:35 -07:00
|
|
|
return nil
|
|
|
|
end
|
2023-08-02 10:35:46 -07:00
|
|
|
local result = handle:read("*a")
|
|
|
|
handle:close()
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
local function move_selection_up()
|
2023-08-02 14:30:25 -07:00
|
|
|
if selected_index > 1 then
|
|
|
|
selected_index = selected_index - 1
|
2023-08-02 17:45:21 -07:00
|
|
|
if selected_index < display_offset + 1 then
|
|
|
|
display_offset = display_offset - 1
|
|
|
|
end
|
|
|
|
YouTubeQueue.print_queue(MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function move_selection_down()
|
2023-08-02 14:30:25 -07:00
|
|
|
if selected_index < YouTubeQueue.size() then
|
|
|
|
selected_index = selected_index + 1
|
2023-08-02 17:45:21 -07:00
|
|
|
if selected_index > display_offset + display_limit then
|
|
|
|
display_offset = display_offset + 1
|
|
|
|
end
|
|
|
|
YouTubeQueue.print_queue(MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-03 00:10:55 -07:00
|
|
|
local function play_video_at(idx)
|
|
|
|
local queue = YouTubeQueue.get_video_queue()
|
|
|
|
if idx <= 0 or idx > #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
|
|
|
|
YouTubeQueue.set_current_index(idx)
|
|
|
|
selected_index = index
|
|
|
|
mp.set_property_number("playlist-pos", index - 1) -- zero-based index
|
|
|
|
return current_video
|
|
|
|
end
|
|
|
|
|
2023-08-02 10:35:46 -07:00
|
|
|
local function play_selected_video()
|
2023-08-02 14:30:25 -07:00
|
|
|
-- local current_index = YouTubeQueue.get_current_index()
|
2023-08-04 00:09:28 -07:00
|
|
|
play_video_at(selected_index)
|
2023-08-02 17:45:21 -07:00
|
|
|
YouTubeQueue.print_queue(MSG_DURATION - 0.5)
|
|
|
|
sleep(MSG_DURATION)
|
2023-08-04 00:09:28 -07:00
|
|
|
print_current_video()
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- play the next video in the queue
|
|
|
|
local function play_next_in_queue()
|
|
|
|
local next_video = YouTubeQueue.next_in_queue()
|
2023-08-04 00:09:28 -07:00
|
|
|
if not next_video or next_video == "" then
|
|
|
|
print_osd_message("No more videos in the queue.", MSG_DURATION,
|
|
|
|
style.error)
|
|
|
|
return
|
|
|
|
end
|
2023-08-04 00:55:16 -07:00
|
|
|
local next_video_url = next_video.video_url
|
2023-08-03 01:43:18 -07:00
|
|
|
local current_index = YouTubeQueue.get_current_index()
|
2023-08-02 13:24:18 -07:00
|
|
|
if YouTubeQueue.size() > 1 then
|
2023-08-03 01:43:18 -07:00
|
|
|
mp.set_property_number("playlist-pos", current_index - 1)
|
2023-08-02 13:24:18 -07:00
|
|
|
else
|
|
|
|
mp.commandv("loadfile", next_video_url, "replace")
|
|
|
|
end
|
2023-08-04 00:09:28 -07:00
|
|
|
print_current_video()
|
2023-08-03 01:43:18 -07:00
|
|
|
selected_index = current_index
|
2023-08-02 17:45:21 -07:00
|
|
|
sleep(MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- add the video to the queue from the clipboard
|
|
|
|
local function add_to_queue()
|
|
|
|
local url = get_clipboard_content()
|
2023-08-02 16:11:35 -07:00
|
|
|
if not url then
|
2023-08-03 10:36:32 -07:00
|
|
|
print_osd_message("Nothing found in the clipboard.", MSG_DURATION,
|
2023-08-04 00:09:28 -07:00
|
|
|
style.error)
|
2023-08-02 16:11:35 -07:00
|
|
|
return
|
|
|
|
end
|
2023-08-02 10:35:46 -07:00
|
|
|
if YouTubeQueue.is_in_queue(url) then
|
2023-08-04 00:09:28 -07:00
|
|
|
print_osd_message("Video already in queue.", MSG_DURATION, style.error)
|
2023-08-02 10:35:46 -07:00
|
|
|
return
|
2023-08-03 00:10:55 -07:00
|
|
|
-- elseif not is_valid_ytdlp_url(url) then
|
|
|
|
-- mp.osd_message("Invalid URL.")
|
|
|
|
-- return
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
2023-08-03 23:18:12 -07:00
|
|
|
local channel_url, channel_name, video_name = get_video_info(url)
|
|
|
|
if (not channel_url or not channel_name or not video_name) or
|
|
|
|
(channel_url == "" or channel_name == "" or video_name == "") then
|
2023-08-04 00:09:28 -07:00
|
|
|
print_osd_message("Error getting video info.", MSG_DURATION, style.error)
|
2023-08-03 00:30:29 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-08-02 16:11:35 -07:00
|
|
|
YouTubeQueue.add_to_queue({
|
2023-08-04 00:55:16 -07:00
|
|
|
video_url = url,
|
2023-08-03 23:18:12 -07:00
|
|
|
video_name = video_name,
|
|
|
|
channel_url = channel_url,
|
|
|
|
channel_name = channel_name
|
2023-08-02 16:11:35 -07:00
|
|
|
})
|
2023-08-02 10:35:46 -07:00
|
|
|
if not YouTubeQueue.get_current_video() then
|
|
|
|
play_next_in_queue()
|
|
|
|
else
|
|
|
|
mp.commandv("loadfile", url, "append-play")
|
2023-08-03 23:18:12 -07:00
|
|
|
print_osd_message("Added " .. video_name .. " to queue.", MSG_DURATION)
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- play the previous video in the queue
|
|
|
|
local function play_previous_video()
|
|
|
|
local previous_video = YouTubeQueue.prev_in_queue()
|
2023-08-04 00:09:28 -07:00
|
|
|
if not previous_video or previous_video == "" then
|
2023-08-03 10:36:32 -07:00
|
|
|
print_osd_message("No previous video available.", MSG_DURATION,
|
2023-08-04 00:09:28 -07:00
|
|
|
style.error)
|
2023-08-02 10:35:46 -07:00
|
|
|
return
|
|
|
|
end
|
2023-08-04 00:09:28 -07:00
|
|
|
local current_index = YouTubeQueue.get_current_index()
|
2023-08-03 01:43:18 -07:00
|
|
|
mp.set_property_number("playlist-pos", current_index - 1)
|
|
|
|
selected_index = current_index
|
2023-08-04 00:09:28 -07:00
|
|
|
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-04 00:55:16 -07:00
|
|
|
local function download_current_video()
|
|
|
|
if current_video and current_video ~= "" then
|
|
|
|
local o = options
|
|
|
|
local v = current_video
|
|
|
|
local q = o.download_quality:sub(1, -2)
|
|
|
|
local command = 'yt-dlp -f \'bestvideo[height<=' .. q ..
|
|
|
|
']+bestaudio/best[height<=' .. q ..
|
|
|
|
']\' --newline -o "' ..
|
|
|
|
expanduser(o.download_directory) .. '/' ..
|
|
|
|
v.channel_name .. '/' .. v.video_name ..
|
|
|
|
'.%(ext)s" ' .. v.video_url
|
|
|
|
|
|
|
|
-- Run the download command
|
|
|
|
local handle = io.popen(command)
|
|
|
|
if not handle then
|
|
|
|
mp.osd_message("Error starting download.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local result = handle:read("*a")
|
|
|
|
if not result then
|
|
|
|
mp.osd_message("Error starting download.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
handle:close()
|
|
|
|
|
|
|
|
mp.msg.log("info", "RESULTS: " .. result)
|
|
|
|
if result then
|
|
|
|
print_osd_message("Downloading " .. v.video_name, MSG_DURATION)
|
|
|
|
else
|
|
|
|
print_osd_message("Error starting download for " .. v.video_name,
|
|
|
|
MSG_DURATION, style.error)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print_osd_message("No video to download.", MSG_DURATION, style.error)
|
|
|
|
end
|
2023-08-02 10:35:46 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- KEY BINDINGS {{{
|
|
|
|
mp.add_key_binding(options.add_to_queue, "add_to_queue", add_to_queue)
|
2023-08-02 14:30:25 -07:00
|
|
|
mp.add_key_binding(options.play_next_in_queue, "play_next_in_queue",
|
|
|
|
play_next_in_queue)
|
|
|
|
mp.add_key_binding(options.play_previous_in_queue, "play_previous_video",
|
|
|
|
play_previous_video)
|
2023-08-02 13:02:52 -07:00
|
|
|
mp.add_key_binding(options.print_queue, "print_queue", YouTubeQueue.print_queue)
|
2023-08-02 14:30:25 -07:00
|
|
|
mp.add_key_binding(options.move_selection_up, "move_selection_up",
|
|
|
|
move_selection_up)
|
|
|
|
mp.add_key_binding(options.move_selection_down, "move_selection_down",
|
|
|
|
move_selection_down)
|
|
|
|
mp.add_key_binding(options.play_selected_video, "play_selected_video",
|
|
|
|
play_selected_video)
|
|
|
|
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",
|
|
|
|
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",
|
|
|
|
download_current_video)
|
2023-08-02 10:35:46 -07:00
|
|
|
|
|
|
|
-- Listen for the file-loaded event
|
2023-08-02 11:07:24 -07:00
|
|
|
mp.register_event("end-file", YouTubeQueue.on_end_file)
|
|
|
|
mp.register_event("track-changed", YouTubeQueue.on_track_changed)
|
|
|
|
mp.register_event("playback-restart", YouTubeQueue.on_playback_restart)
|
2023-08-02 10:35:46 -07:00
|
|
|
-- }}}
|