add download function

This commit is contained in:
ksyasuda 2023-08-04 00:55:16 -07:00
parent 90c255b60e
commit d41d85b0e3
2 changed files with 74 additions and 12 deletions

View File

@ -14,3 +14,5 @@ display_limit=6
cursor_icon=🠺 cursor_icon=🠺
font_size=24 font_size=24
font_name=JetBrains Mono font_name=JetBrains Mono
download_quality=720p
download_directory=~/Videos/YouTube

View File

@ -35,12 +35,15 @@ local options = {
open_video_in_browser = "ctrl+o", open_video_in_browser = "ctrl+o",
open_channel_in_browser = "ctrl+O", open_channel_in_browser = "ctrl+O",
print_current_video = "ctrl+P", print_current_video = "ctrl+P",
download_current_video = "ctrl+d",
browser = "firefox", browser = "firefox",
clipboard_command = "xclip -o", clipboard_command = "xclip -o",
display_limit = 6, display_limit = 6,
cursor_icon = "🠺", cursor_icon = "🠺",
font_size = 14, font_size = 14,
font_name = "JetBrains Mono" font_name = "JetBrains Mono",
download_quality = "720p",
download_directory = "~/Videos/YouTube"
} }
local colors = { local colors = {
@ -82,6 +85,7 @@ local function sleep(n) os.execute("sleep " .. tonumber(n)) end
local function print_osd_message(message, duration, s) local function print_osd_message(message, duration, s)
if not s then s = style.font .. "{" .. notransparent .. "}" end if not s then s = style.font .. "{" .. notransparent .. "}" end
if not duration then duration = MSG_DURATION end
mp.osd_message(styleOn .. s .. message .. style.reset .. styleOff .. "\n", mp.osd_message(styleOn .. s .. message .. style.reset .. styleOff .. "\n",
duration) duration)
end end
@ -91,6 +95,32 @@ local function print_current_video()
current_video.video_name, 3) current_video.video_name, 3)
end end
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
local function get_video_info(url) local function get_video_info(url)
local command = local command =
'yt-dlp --print channel_url --print uploader --print title --playlist-items 1 ' .. 'yt-dlp --print channel_url --print uploader --print title --playlist-items 1 ' ..
@ -175,7 +205,9 @@ function YouTubeQueue.prev_in_queue()
end end
function YouTubeQueue.is_in_queue(url) function YouTubeQueue.is_in_queue(url)
for _, v in ipairs(video_queue) do if v.url == url then return true end end for _, v in ipairs(video_queue) do
if v.video_url == url then return true end
end
return false return false
end end
@ -184,7 +216,7 @@ function YouTubeQueue.update_current_index()
local current_url = mp.get_property("path") local current_url = mp.get_property("path")
if #video_queue == 0 then return end if #video_queue == 0 then return end
for i, v in ipairs(video_queue) do for i, v in ipairs(video_queue) do
if v.url == current_url then if v.video_url == current_url then
index = i index = i
return return
end end
@ -321,7 +353,7 @@ local function play_next_in_queue()
style.error) style.error)
return return
end end
local next_video_url = next_video.url local next_video_url = next_video.video_url
local current_index = YouTubeQueue.get_current_index() local current_index = YouTubeQueue.get_current_index()
if YouTubeQueue.size() > 1 then if YouTubeQueue.size() > 1 then
mp.set_property_number("playlist-pos", current_index - 1) mp.set_property_number("playlist-pos", current_index - 1)
@ -356,7 +388,7 @@ local function add_to_queue()
end end
YouTubeQueue.add_to_queue({ YouTubeQueue.add_to_queue({
url = url, video_url = url,
video_name = video_name, video_name = video_name,
channel_url = channel_url, channel_url = channel_url,
channel_name = channel_name channel_name = channel_name
@ -384,15 +416,41 @@ local function play_previous_video()
sleep(MSG_DURATION) sleep(MSG_DURATION)
end end
local function open_url_in_browser(url) local function download_current_video()
local command = options.browser .. " " .. url if current_video and current_video ~= "" then
os.execute(command) local o = options
end 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
local function open_video_in_browser() open_url_in_browser(current_video.url) end -- 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()
local function open_channel_in_browser() mp.msg.log("info", "RESULTS: " .. result)
open_url_in_browser(current_video.channel_url) 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
end end
-- }}} -- }}}
@ -416,6 +474,8 @@ 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", mp.add_key_binding(options.open_channel_in_browser, "open_channel_in_browser",
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 -- Listen for the file-loaded event
mp.register_event("end-file", YouTubeQueue.on_end_file) mp.register_event("end-file", YouTubeQueue.on_end_file)