mirror of
https://github.com/ksyasuda/mpv-youtube-queue.git
synced 2024-10-28 04:44:11 -07:00
refactor next/prev in queue functions to work with internal playlist
This commit is contained in:
parent
0fa2970132
commit
beeaee28dd
@ -23,7 +23,6 @@ mp.options = require 'mp.options'
|
|||||||
local YouTubeQueue = {}
|
local YouTubeQueue = {}
|
||||||
local video_queue = {}
|
local video_queue = {}
|
||||||
local current_video = nil
|
local current_video = nil
|
||||||
local queue_is_displayed = false
|
|
||||||
local index = 0
|
local index = 0
|
||||||
|
|
||||||
local options = {
|
local options = {
|
||||||
@ -41,6 +40,33 @@ local options = {
|
|||||||
}
|
}
|
||||||
mp.options.read_options(options, "mpv-youtube-queue")
|
mp.options.read_options(options, "mpv-youtube-queue")
|
||||||
|
|
||||||
|
-- HELPERS {{{
|
||||||
|
|
||||||
|
-- print the name of the current video to the OSD
|
||||||
|
local function print_video_name(video, time)
|
||||||
|
if not video then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not time then
|
||||||
|
time = 2
|
||||||
|
end
|
||||||
|
mp.osd_message('Playing:' .. video.name, time)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Function to get the video name from a YouTube URL
|
||||||
|
local function get_video_name(url)
|
||||||
|
local command = 'yt-dlp --get-title ' .. 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
|
||||||
|
|
||||||
|
-- }}}
|
||||||
|
|
||||||
-- QUEUE GETTERS AND SETTERS {{{
|
-- QUEUE GETTERS AND SETTERS {{{
|
||||||
|
|
||||||
function YouTubeQueue.size()
|
function YouTubeQueue.size()
|
||||||
@ -63,6 +89,15 @@ function YouTubeQueue.get_current_video()
|
|||||||
return current_video
|
return current_video
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function YouTubeQueue.get_video_at(idx)
|
||||||
|
if idx <= 0 or idx > #video_queue then
|
||||||
|
mp.osd_message("Invalid video index")
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
return video_queue[idx]
|
||||||
|
end
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
-- QUEUE FUNCTIONS {{{
|
-- QUEUE FUNCTIONS {{{
|
||||||
@ -98,7 +133,8 @@ function YouTubeQueue.play_video_at(idx)
|
|||||||
end
|
end
|
||||||
index = idx
|
index = idx
|
||||||
current_video = video_queue[index]
|
current_video = video_queue[index]
|
||||||
mp.commandv("loadfile", current_video.url, "replace")
|
mp.commandv("loadfile", current_video.url, "append-play")
|
||||||
|
mp.set_property_number("playlist-pos", index)
|
||||||
return current_video
|
return current_video
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -122,41 +158,51 @@ function YouTubeQueue.update_current_index()
|
|||||||
end
|
end
|
||||||
-- if not found, reset the index
|
-- if not found, reset the index
|
||||||
index = 0
|
index = 0
|
||||||
|
current_video = YouTubeQueue.get_video_at(index)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function to be called when the end-file event is triggered
|
-- Function to be called when the end-file event is triggered
|
||||||
function YouTubeQueue.on_end_file(event)
|
function YouTubeQueue.on_end_file(event)
|
||||||
if event.reason == "eof" then -- The file ended normally
|
if event.reason == "eof" then -- The file ended normally
|
||||||
mp.msg.log("info", "End of file reached")
|
|
||||||
YouTubeQueue.update_current_index()
|
YouTubeQueue.update_current_index()
|
||||||
|
print_video_name(YouTubeQueue.get_current_video())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function to be called when the track-changed event is triggered
|
-- Function to be called when the track-changed event is triggered
|
||||||
function YouTubeQueue.on_track_changed()
|
function YouTubeQueue.on_track_changed()
|
||||||
mp.msg.log("info", "Track changed")
|
|
||||||
YouTubeQueue.update_current_index()
|
YouTubeQueue.update_current_index()
|
||||||
|
print_video_name(YouTubeQueue.get_current_video())
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function to be called when the playback-restart event is triggered
|
-- Function to be called when the playback-restart event is triggered
|
||||||
function YouTubeQueue.on_playback_restart()
|
function YouTubeQueue.on_playback_restart()
|
||||||
YouTubeQueue.update_current_index()
|
YouTubeQueue.update_current_index()
|
||||||
|
print_video_name(YouTubeQueue.get_current_video())
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function YouTubeQueue.print_queue(duration)
|
||||||
|
local queue = YouTubeQueue.get_video_queue()
|
||||||
|
local current_index = YouTubeQueue.get_current_index()
|
||||||
|
if not duration then
|
||||||
|
duration = 5
|
||||||
|
end
|
||||||
|
if #queue > 0 then
|
||||||
|
local message = ""
|
||||||
|
for i, v in ipairs(queue) do
|
||||||
|
local prefix = (i == current_index) and "=> " or " "
|
||||||
|
message = message .. prefix .. i .. ". " .. v.name .. "\n"
|
||||||
|
end
|
||||||
|
mp.osd_message(message, duration)
|
||||||
|
else
|
||||||
|
mp.osd_message("No videos in the queue or history.")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
-- MAIN FUNCTIONS {{{
|
-- MAIN FUNCTIONS {{{
|
||||||
-- Function to get the video name from a YouTube URL
|
|
||||||
local function get_video_name(url)
|
|
||||||
local command = 'yt-dlp --get-title ' .. 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
|
|
||||||
|
|
||||||
-- returns the content of the clipboard
|
-- returns the content of the clipboard
|
||||||
local function get_clipboard_content()
|
local function get_clipboard_content()
|
||||||
@ -169,48 +215,27 @@ local function get_clipboard_content()
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- print the queue to the OSD
|
|
||||||
local function print_queue()
|
|
||||||
local complete_list = YouTubeQueue.get_video_queue()
|
|
||||||
local current_index = YouTubeQueue.get_current_index()
|
|
||||||
if #complete_list > 0 then
|
|
||||||
local message = ""
|
|
||||||
for i, v in ipairs(complete_list) do
|
|
||||||
local prefix = (i == current_index) and "=> " or " "
|
|
||||||
message = message .. prefix .. i .. ". " .. v.name .. "\n"
|
|
||||||
end
|
|
||||||
mp.osd_message(message, 5)
|
|
||||||
queue_is_displayed = true
|
|
||||||
else
|
|
||||||
mp.osd_message("No videos in the queue or history.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function move_selection_up()
|
local function move_selection_up()
|
||||||
local current_index = YouTubeQueue.get_current_index()
|
local current_index = YouTubeQueue.get_current_index()
|
||||||
if queue_is_displayed and current_index > 1 then
|
if current_index > 1 then
|
||||||
current_index = current_index - 1
|
current_index = current_index - 1
|
||||||
YouTubeQueue.set_current_index(current_index)
|
YouTubeQueue.set_current_index(current_index)
|
||||||
print_queue()
|
YouTubeQueue.print_queue()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function move_selection_down()
|
local function move_selection_down()
|
||||||
local current_index = YouTubeQueue.get_current_index()
|
local current_index = YouTubeQueue.get_current_index()
|
||||||
if queue_is_displayed and current_index < YouTubeQueue.size() then
|
if current_index < YouTubeQueue.size() then
|
||||||
current_index = current_index + 1
|
current_index = current_index + 1
|
||||||
YouTubeQueue.set_current_index(current_index)
|
YouTubeQueue.set_current_index(current_index)
|
||||||
print_queue()
|
YouTubeQueue.print_queue()
|
||||||
end
|
end
|
||||||
end
|
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()
|
||||||
if queue_is_displayed then
|
YouTubeQueue.play_video_at(current_index)
|
||||||
YouTubeQueue.play_video_at(current_index)
|
|
||||||
queue_is_displayed = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- play the next video in the queue
|
-- play the next video in the queue
|
||||||
@ -220,14 +245,9 @@ local function play_next_in_queue()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local next_video_url = next_video.url
|
local next_video_url = next_video.url
|
||||||
local name = next_video.name
|
print_video_name(next_video)
|
||||||
-- append the next video to the playlist
|
|
||||||
mp.commandv("loadfile", next_video_url, "append-play")
|
mp.commandv("loadfile", next_video_url, "append-play")
|
||||||
-- get the index of the last item in the playlist
|
mp.set_property_number("playlist-pos", YouTubeQueue.get_current_index())
|
||||||
local playlist_count = mp.get_property_number("playlist-count")
|
|
||||||
-- play the last item in the playlist
|
|
||||||
mp.set_property_number("playlist-pos", playlist_count - 1)
|
|
||||||
mp.osd_message("Playing " .. name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add the video to the queue from the clipboard
|
-- add the video to the queue from the clipboard
|
||||||
@ -259,10 +279,9 @@ local function play_previous_video()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local previous_video_url = previous_video.url
|
local previous_video_url = previous_video.url
|
||||||
local name = previous_video.name
|
print_video_name(previous_video)
|
||||||
mp.commandv("loadfile", previous_video_url, "replace")
|
mp.commandv("loadfile", previous_video_url, "append-play")
|
||||||
mp.commandv("loadfile", current_video.url, "append-play")
|
mp.set_property_number("playlist-pos", YouTubeQueue.get_current_index())
|
||||||
mp.osd_message("Playing " .. name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function open_url_in_browser(url)
|
local function open_url_in_browser(url)
|
||||||
@ -278,9 +297,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
local function print_current_video()
|
local function print_current_video()
|
||||||
local current_url = mp.get_property("path")
|
mp.osd_message("Currently playing " .. current_video.name, 3)
|
||||||
local current_name = get_video_name(current_url)
|
|
||||||
mp.osd_message("Currently playing " .. current_name, 3)
|
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
@ -288,7 +305,7 @@ end
|
|||||||
mp.add_key_binding(options.add_to_queue, "add_to_queue", add_to_queue)
|
mp.add_key_binding(options.add_to_queue, "add_to_queue", add_to_queue)
|
||||||
mp.add_key_binding(options.play_next_in_queue, "play_next_in_queue", play_next_in_queue)
|
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)
|
mp.add_key_binding(options.play_previous_in_queue, "play_previous_video", play_previous_video)
|
||||||
mp.add_key_binding(options.print_queue, "print_queue", print_queue)
|
mp.add_key_binding(options.print_queue, "print_queue", YouTubeQueue.print_queue)
|
||||||
mp.add_key_binding(options.move_selection_up, "move_selection_up", move_selection_up)
|
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.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.play_selected_video, "play_selected_video", play_selected_video)
|
||||||
@ -301,5 +318,3 @@ mp.register_event("track-changed", YouTubeQueue.on_track_changed)
|
|||||||
mp.register_event("playback-restart", YouTubeQueue.on_playback_restart)
|
mp.register_event("playback-restart", YouTubeQueue.on_playback_restart)
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
||||||
return YouTubeQueue
|
|
||||||
|
Loading…
Reference in New Issue
Block a user