Compare commits

...

3 Commits

Author SHA1 Message Date
ksyasuda@umich.edu
b46116e976 move listeners out of YouTubeQueue object 2023-08-04 11:45:41 -07:00
ksyasuda@umich.edu
2ca1234ee4 add downloader config option 2023-08-04 11:20:58 -07:00
ksyasuda@umich.edu
14e57bd9b4 fix update_current_index 2023-08-04 09:02:01 -07:00
2 changed files with 34 additions and 30 deletions

View File

@ -15,4 +15,5 @@ cursor_icon=🠺
font_size=24
font_name=JetBrains Mono
download_quality=720p
download_directory=~/Videos/YouTube
download_directory=~/videos/YouTube
downloader=curl

View File

@ -43,7 +43,8 @@ local options = {
font_size = 14,
font_name = "JetBrains Mono",
download_quality = "720p",
download_directory = "~/Videos/YouTube"
download_directory = "~/videos/YouTube",
downloader = "curl"
}
local colors = {
@ -218,28 +219,15 @@ function YouTubeQueue.update_current_index()
for i, v in ipairs(video_queue) do
if v.video_url == current_url then
index = i
selected_index = index
current_video = YouTubeQueue.get_video_at(index)
return
end
end
-- if not found, reset the index
index = 0
selected_index = index
current_video = YouTubeQueue.get_video_at(index)
end
-- Function to be called when the end-file event is triggered
function YouTubeQueue.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
function YouTubeQueue.on_track_changed() YouTubeQueue.update_current_index() end
-- Function to be called when the playback-restart event is triggered
function YouTubeQueue.on_playback_restart() YouTubeQueue.update_current_index() end
function YouTubeQueue.print_queue(duration)
local current_index = index
if not duration then duration = 3 end
@ -422,30 +410,33 @@ local function download_current_video()
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 "' ..
']+bestaudio/best[height<=' .. q .. ']\' -o "' ..
expanduser(o.download_directory) .. '/' ..
v.channel_name .. '/' .. v.video_name ..
'.%(ext)s" ' .. v.video_url
'.%(ext)s" ' .. '--downloader ' .. o.downloader ..
' ' .. v.video_url
-- Run the download command
local handle = io.popen(command)
if not handle then
mp.osd_message("Error starting download.")
print_osd_message("Error starting download.", MSG_DURATION,
style.error)
return
end
print_osd_message("Starting download for " .. v.video_name, MSG_DURATION)
local result = handle:read("*a")
handle:close()
if not result then
mp.osd_message("Error starting download.")
print_osd_message("Error starting download.", MSG_DURATION,
style.error)
return
end
handle:close()
mp.msg.log("info", "RESULTS: " .. result)
if result then
print_osd_message("Downloading " .. v.video_name, MSG_DURATION)
print_osd_message("Finished downloading " .. v.video_name,
MSG_DURATION)
else
print_osd_message("Error starting download for " .. v.video_name,
print_osd_message("Error downloading " .. v.video_name,
MSG_DURATION, style.error)
end
else
@ -453,6 +444,19 @@ local function download_current_video()
end
end
-- 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
local function on_playback_restart() YouTubeQueue.update_current_index() end
-- }}}
-- KEY BINDINGS {{{
@ -477,8 +481,7 @@ mp.add_key_binding(options.open_channel_in_browser, "open_channel_in_browser",
mp.add_key_binding(options.download_current_video, "download_current_video",
download_current_video)
-- Listen for the file-loaded event
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)
mp.register_event("end-file", on_end_file)
mp.register_event("track-changed", on_track_changed)
mp.register_event("playback-restart", on_playback_restart)
-- }}}