.lua-files/video-dl.lua

78 lines
2.1 KiB
Lua
Raw Permalink Normal View History

2024-01-13 11:04:00 +00:00
#!/usr/bin/env luajit
2024-01-14 01:13:57 +00:00
local help = [[Usage:
2024-01-13 11:04:00 +00:00
2024-02-18 21:24:01 +00:00
video-dl.lua [action] [--file] <url>
2024-01-13 11:04:00 +00:00
[action]: What is desired.
video (default): Highest quality video (maximum 720p).
backup, clone, copy: English subtitles (including automatic
subtitles), thumbnail, description, highest quality video
(maximum 720p).
music, audio: Highest quality audio only.
metadata, meta: English subtitles (including automatic
subtitles), thumbnail, description.
2024-02-18 21:24:01 +00:00
[--file]: <url> is actually a file of URLs to open and execute [action]
on each.
2024-01-13 11:04:00 +00:00
<url>: Source. YouTube URL expected, but should work with anything
yt-dlp works with.
]]
if os.execute("where yt-dlp") ~= 0 then
2024-01-13 23:37:46 +00:00
error("yt-dlp must be installed and in the path")
end
2024-01-13 11:04:00 +00:00
local action, url
if #arg < 2 then
if arg[1]:find("help") then
print(help)
return false
end
action = "video"
url = arg[1]
else
action = arg[1]
url = arg[2]
2024-02-18 21:24:01 +00:00
-- "--file" is handled just before execution
2024-01-13 11:04:00 +00:00
end
2024-11-10 01:05:54 +00:00
local core_command = "yt-dlp --retries 100 "
local metadata_options = "--write-sub --write-auto-sub --sub-lang \"en.*\" --write-thumbnail --write-description "
local quality_ceiling_720 = "-f \"bestvideo[height<=720]+bestaudio/best[height<=720]\" "
2024-01-13 11:04:00 +00:00
local execute = {
2024-02-18 21:24:01 +00:00
backup = function(url)
2024-11-10 01:05:54 +00:00
os.execute(core_command .. metadata_options .. quality_ceiling_720 .. url:enquote())
2024-01-13 11:04:00 +00:00
end,
2024-02-18 21:24:01 +00:00
music = function(url)
2024-11-10 01:05:54 +00:00
os.execute(core_command .. "-x --audio-quality 0 " .. url:enquote())
2024-01-13 11:04:00 +00:00
end,
2024-02-18 21:24:01 +00:00
metadata = function(url)
2024-11-10 01:05:54 +00:00
os.execute(core_command .. metadata_options .. "--skip-download " .. url:enquote())
2024-01-13 11:04:00 +00:00
end,
2024-02-18 21:24:01 +00:00
video = function(url)
2024-11-10 01:05:54 +00:00
os.execute(core_command .. quality_ceiling_720 .. url:enquote())
2024-01-13 11:04:00 +00:00
end,
}
execute.clone = execute.backup
execute.copy = execute.backup
execute.audio = execute.music
execute.meta = execute.metadata
if execute[action] then
2024-02-18 21:24:01 +00:00
if url == "--file" then
pcall(function()
for line in io.lines(arg[3]) do
execute[action](line)
end
end)
else
execute[action](url)
end
2024-01-13 11:04:00 +00:00
else
2024-01-14 02:40:46 +00:00
print("Invalid [action]")
2024-01-13 11:04:00 +00:00
print("Received:", "action", action, "url", url)
2024-02-18 21:24:01 +00:00
return false
2024-01-13 11:04:00 +00:00
end