mirror of
https://github.com/ksyasuda/mpv-youtube-queue.git
synced 2024-10-28 04:44:11 -07:00
Compare commits
4 Commits
fdb02bc58d
...
c4f058bafe
Author | SHA1 | Date | |
---|---|---|---|
|
c4f058bafe | ||
|
e1e033f37d | ||
|
b8ef352bf5 | ||
|
e2e4d2ddd5 |
@ -6,6 +6,8 @@ move_selection_up=ctrl+UP
|
|||||||
move_selection_down=ctrl+DOWN
|
move_selection_down=ctrl+DOWN
|
||||||
play_selected_video=ctrl+ENTER
|
play_selected_video=ctrl+ENTER
|
||||||
open_video_in_browser=ctrl+o
|
open_video_in_browser=ctrl+o
|
||||||
|
open_channel_in_browser=ctrl+O
|
||||||
print_current_video=ctrl+P
|
print_current_video=ctrl+P
|
||||||
browser=firefox
|
browser=firefox
|
||||||
clipboard_command=xclip -o
|
clipboard_command=xclip -o
|
||||||
|
display_limit=6
|
||||||
|
@ -22,7 +22,7 @@ local video_queue = {}
|
|||||||
local current_video = nil
|
local current_video = nil
|
||||||
local index = 0
|
local index = 0
|
||||||
local selected_index = 1
|
local selected_index = 1
|
||||||
local SLEEP_TIME = 1.5
|
local MSG_DURATION = 1.5
|
||||||
|
|
||||||
local options = {
|
local options = {
|
||||||
add_to_queue = "ctrl+a",
|
add_to_queue = "ctrl+a",
|
||||||
@ -33,14 +33,22 @@ local options = {
|
|||||||
move_selection_down = "ctrl+DOWN",
|
move_selection_down = "ctrl+DOWN",
|
||||||
play_selected_video = "ctrl+ENTER",
|
play_selected_video = "ctrl+ENTER",
|
||||||
open_video_in_browser = "ctrl+o",
|
open_video_in_browser = "ctrl+o",
|
||||||
|
open_channel_in_browser = "ctrl+O",
|
||||||
print_current_video = "ctrl+P",
|
print_current_video = "ctrl+P",
|
||||||
browser = "firefox",
|
browser = "firefox",
|
||||||
clipboard_command = "xclip -o"
|
clipboard_command = "xclip -o",
|
||||||
|
display_limit = 6
|
||||||
}
|
}
|
||||||
mp.options.read_options(options, "mpv-youtube-queue")
|
mp.options.read_options(options, "mpv-youtube-queue")
|
||||||
|
|
||||||
|
local display_limit = options.display_limit
|
||||||
|
local display_offset = 0
|
||||||
|
|
||||||
-- HELPERS {{{
|
-- HELPERS {{{
|
||||||
|
|
||||||
|
-- run sleep shell command for n seconds
|
||||||
|
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
||||||
|
|
||||||
-- print the name of the current video to the OSD
|
-- print the name of the current video to the OSD
|
||||||
local function print_video_name(video, duration)
|
local function print_video_name(video, duration)
|
||||||
if not video then return end
|
if not video then return end
|
||||||
@ -58,6 +66,26 @@ local function get_video_name(url)
|
|||||||
return result:gsub("%s+$", "")
|
return result:gsub("%s+$", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- get the channel url from a video url
|
||||||
|
local function get_channel_url(url)
|
||||||
|
local command = 'yt-dlp --print channel_url --playlist-items 1 ' .. url
|
||||||
|
local handle = io.popen(command)
|
||||||
|
if not handle then return nil end
|
||||||
|
local result = handle:read("*a")
|
||||||
|
handle:close()
|
||||||
|
return result:gsub("%s+$", "")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function is_valid_ytdlp_url(url)
|
||||||
|
local command = 'yt-dlp --simulate \'' .. url .. '\' >/dev/null 2>&1'
|
||||||
|
local handle = io.popen(command .. "; echo $?")
|
||||||
|
if not handle then return false end
|
||||||
|
local result = handle:read("*a")
|
||||||
|
if not result then return false end
|
||||||
|
handle:close()
|
||||||
|
return result:gsub("%s+$", "") == "0"
|
||||||
|
end
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
-- QUEUE GETTERS AND SETTERS {{{
|
-- QUEUE GETTERS AND SETTERS {{{
|
||||||
@ -91,6 +119,7 @@ function YouTubeQueue.add_to_queue(video) table.insert(video_queue, video) end
|
|||||||
function YouTubeQueue.next_in_queue()
|
function YouTubeQueue.next_in_queue()
|
||||||
if index < #video_queue then
|
if index < #video_queue then
|
||||||
index = index + 1
|
index = index + 1
|
||||||
|
selected_index = index
|
||||||
current_video = video_queue[index]
|
current_video = video_queue[index]
|
||||||
return current_video
|
return current_video
|
||||||
end
|
end
|
||||||
@ -99,6 +128,7 @@ end
|
|||||||
function YouTubeQueue.prev_in_queue()
|
function YouTubeQueue.prev_in_queue()
|
||||||
if index > 1 then
|
if index > 1 then
|
||||||
index = index - 1
|
index = index - 1
|
||||||
|
selected_index = index
|
||||||
current_video = video_queue[index]
|
current_video = video_queue[index]
|
||||||
else
|
else
|
||||||
current_video = video_queue[1]
|
current_video = video_queue[1]
|
||||||
@ -112,8 +142,8 @@ function YouTubeQueue.play_video_at(idx)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
index = idx
|
index = idx
|
||||||
|
selected_index = index
|
||||||
current_video = video_queue[index]
|
current_video = video_queue[index]
|
||||||
mp.msg.log("info", "Playing video at index " .. index)
|
|
||||||
mp.set_property_number("playlist-pos", index - 1) -- zero-based index
|
mp.set_property_number("playlist-pos", index - 1) -- zero-based index
|
||||||
return current_video
|
return current_video
|
||||||
end
|
end
|
||||||
@ -134,6 +164,7 @@ function YouTubeQueue.update_current_index()
|
|||||||
end
|
end
|
||||||
-- if not found, reset the index
|
-- if not found, reset the index
|
||||||
index = 0
|
index = 0
|
||||||
|
selected_index = index
|
||||||
current_video = YouTubeQueue.get_video_at(index)
|
current_video = YouTubeQueue.get_video_at(index)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,17 +182,21 @@ function YouTubeQueue.on_track_changed() YouTubeQueue.update_current_index() end
|
|||||||
function YouTubeQueue.on_playback_restart() YouTubeQueue.update_current_index() end
|
function YouTubeQueue.on_playback_restart() YouTubeQueue.update_current_index() end
|
||||||
|
|
||||||
function YouTubeQueue.print_queue(duration)
|
function YouTubeQueue.print_queue(duration)
|
||||||
local queue = YouTubeQueue.get_video_queue()
|
local current_index = index
|
||||||
local current_index = YouTubeQueue.get_current_index()
|
if not duration then duration = 3 end
|
||||||
if not duration then duration = 5 end
|
if #video_queue > 0 then
|
||||||
if #queue > 0 then
|
|
||||||
local message = ""
|
local message = ""
|
||||||
for i, v in ipairs(queue) do
|
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
|
||||||
|
|
||||||
|
for i = start_index, end_index do
|
||||||
local prefix = (i == current_index and i == selected_index) and
|
local prefix = (i == current_index and i == selected_index) and
|
||||||
"=>> " or (i == current_index) and "=> " or
|
"=>> " or (i == current_index) and "=> " or
|
||||||
(i == selected_index) and "> " or " "
|
(i == selected_index) and "> " or " "
|
||||||
-- prefix = (i == selected_index) and prefix .. "> " or prefix
|
message = message .. prefix .. i .. ". " .. video_queue[i].name ..
|
||||||
message = message .. prefix .. i .. ". " .. v.name .. "\n"
|
"\n"
|
||||||
end
|
end
|
||||||
mp.osd_message(message, duration)
|
mp.osd_message(message, duration)
|
||||||
else
|
else
|
||||||
@ -176,7 +211,10 @@ end
|
|||||||
-- returns the content of the clipboard
|
-- returns the content of the clipboard
|
||||||
local function get_clipboard_content()
|
local function get_clipboard_content()
|
||||||
local handle = io.popen(options.clipboard_command)
|
local handle = io.popen(options.clipboard_command)
|
||||||
if not handle then return nil end
|
if not handle then
|
||||||
|
mp.osd_message("Error getting clipboard content")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
local result = handle:read("*a")
|
local result = handle:read("*a")
|
||||||
handle:close()
|
handle:close()
|
||||||
return result
|
return result
|
||||||
@ -186,7 +224,10 @@ local function move_selection_up()
|
|||||||
-- selected_index = YouTubeQueue.get_current_index()
|
-- selected_index = YouTubeQueue.get_current_index()
|
||||||
if selected_index > 1 then
|
if selected_index > 1 then
|
||||||
selected_index = selected_index - 1
|
selected_index = selected_index - 1
|
||||||
YouTubeQueue.print_queue()
|
if selected_index < display_offset + 1 then
|
||||||
|
display_offset = display_offset - 1
|
||||||
|
end
|
||||||
|
YouTubeQueue.print_queue(MSG_DURATION)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -195,18 +236,19 @@ local function move_selection_down()
|
|||||||
if selected_index < YouTubeQueue.size() then
|
if selected_index < YouTubeQueue.size() then
|
||||||
selected_index = selected_index + 1
|
selected_index = selected_index + 1
|
||||||
-- YouTubeQueue.set_current_index(current_index)
|
-- YouTubeQueue.set_current_index(current_index)
|
||||||
YouTubeQueue.print_queue()
|
if selected_index > display_offset + display_limit then
|
||||||
|
display_offset = display_offset + 1
|
||||||
|
end
|
||||||
|
YouTubeQueue.print_queue(MSG_DURATION)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sleep(n) os.execute("sleep " .. tonumber(n)) end
|
|
||||||
|
|
||||||
local function play_selected_video()
|
local function play_selected_video()
|
||||||
-- local current_index = YouTubeQueue.get_current_index()
|
-- local current_index = YouTubeQueue.get_current_index()
|
||||||
local video = YouTubeQueue.play_video_at(selected_index)
|
local video = YouTubeQueue.play_video_at(selected_index)
|
||||||
YouTubeQueue.print_queue(SLEEP_TIME - 0.5)
|
YouTubeQueue.print_queue(MSG_DURATION - 0.5)
|
||||||
sleep(SLEEP_TIME)
|
sleep(MSG_DURATION)
|
||||||
print_video_name(video, SLEEP_TIME)
|
print_video_name(video, MSG_DURATION)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- play the next video in the queue
|
-- play the next video in the queue
|
||||||
@ -220,22 +262,35 @@ local function play_next_in_queue()
|
|||||||
else
|
else
|
||||||
mp.commandv("loadfile", next_video_url, "replace")
|
mp.commandv("loadfile", next_video_url, "replace")
|
||||||
end
|
end
|
||||||
print_video_name(next_video, SLEEP_TIME)
|
print_video_name(next_video, MSG_DURATION)
|
||||||
sleep(SLEEP_TIME)
|
sleep(MSG_DURATION)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add the video to the queue from the clipboard
|
-- add the video to the queue from the clipboard
|
||||||
local function add_to_queue()
|
local function add_to_queue()
|
||||||
local url = get_clipboard_content()
|
local url = get_clipboard_content()
|
||||||
-- get video name in background
|
if not url then
|
||||||
local name = get_video_name(url)
|
mp.osd_message("Nothing found in the clipboard.")
|
||||||
|
return
|
||||||
-- check to make sure the video is not already in the queue
|
end
|
||||||
|
if not string.match(url, "^https://www.youtube.com") then
|
||||||
|
mp.osd_message("Not a YouTube URL.")
|
||||||
|
return
|
||||||
|
end
|
||||||
if YouTubeQueue.is_in_queue(url) then
|
if YouTubeQueue.is_in_queue(url) then
|
||||||
mp.osd_message("Video already in queue.")
|
mp.osd_message("Video already in queue.")
|
||||||
return
|
return
|
||||||
|
elseif not is_valid_ytdlp_url(url) then
|
||||||
|
mp.osd_message("Invalid URL.")
|
||||||
|
return
|
||||||
end
|
end
|
||||||
YouTubeQueue.add_to_queue({ url = url, name = name })
|
local name = get_video_name(url)
|
||||||
|
local channel_url = get_channel_url(url)
|
||||||
|
YouTubeQueue.add_to_queue({
|
||||||
|
url = url,
|
||||||
|
name = name,
|
||||||
|
channel_url = channel_url
|
||||||
|
})
|
||||||
if not YouTubeQueue.get_current_video() then
|
if not YouTubeQueue.get_current_video() then
|
||||||
play_next_in_queue()
|
play_next_in_queue()
|
||||||
else
|
else
|
||||||
@ -252,8 +307,8 @@ local function play_previous_video()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
mp.set_property_number("playlist-pos", YouTubeQueue.get_current_index() - 1)
|
mp.set_property_number("playlist-pos", YouTubeQueue.get_current_index() - 1)
|
||||||
print_video_name(previous_video, SLEEP_TIME)
|
print_video_name(previous_video, MSG_DURATION)
|
||||||
sleep(SLEEP_TIME)
|
sleep(MSG_DURATION)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function open_url_in_browser(url)
|
local function open_url_in_browser(url)
|
||||||
@ -261,9 +316,10 @@ local function open_url_in_browser(url)
|
|||||||
os.execute(command)
|
os.execute(command)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function open_video_in_browser()
|
local function open_video_in_browser() open_url_in_browser(current_video.url) end
|
||||||
local current_url = mp.get_property("path")
|
|
||||||
open_url_in_browser(current_url)
|
local function open_channel_in_browser()
|
||||||
|
open_url_in_browser(current_video.channel_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function print_current_video()
|
local function print_current_video()
|
||||||
@ -288,6 +344,8 @@ mp.add_key_binding(options.open_video_in_browser, "open_video_in_browser",
|
|||||||
open_video_in_browser)
|
open_video_in_browser)
|
||||||
mp.add_key_binding(options.print_current_video, "print_current_video",
|
mp.add_key_binding(options.print_current_video, "print_current_video",
|
||||||
print_current_video)
|
print_current_video)
|
||||||
|
mp.add_key_binding(options.open_channel_in_browser, "open_channel_in_browser",
|
||||||
|
open_channel_in_browser)
|
||||||
|
|
||||||
-- Listen for the file-loaded event
|
-- Listen for the file-loaded event
|
||||||
mp.register_event("end-file", YouTubeQueue.on_end_file)
|
mp.register_event("end-file", YouTubeQueue.on_end_file)
|
||||||
|
Loading…
Reference in New Issue
Block a user