for*_*rin 4 youtube search youtube-dl
是否有一个实用程序可以从命令行搜索 YouTube,然后根据用户输入查看或下载搜索结果?
\n\n$ youtube-search madonna\n\n1 Madonna - Hung Up (Official Music Video)\nmadonna \xe2\x99\xa9 180M views 9 years ago\nhttps://www.youtube.com/watch?v=EDwb9jOVRtU\n"Hung Up" by Madonna from Confessions On A Dance Floor, available now.\n\n2 Madonna - Like A Prayer (Official Music Video)\nmadonna \xe2\x99\xa9 69M views 9 years ago\nhttps://www.youtube.com/watch?v=79fzeNUqQbQ\n2006 WMG Like A Prayer.\n\netc.\n
Run Code Online (Sandbox Code Playgroud)\n\n然后你可以输入:
\n\n到目前为止我尝试过的:
\n\ngoogler部分与 YouTube 配合使用,但由于某种原因,在搜索“麦当娜”时仅显示两个搜索结果。此外,也无法在查看和下载之间进行选择。
\n\nyoutube-dl有搜索功能,但似乎不打印搜索结果也不接受用户输入。youtube-dl -j ytsearch:madonna
列出有关搜索结果的元数据,但似乎不包含所需的视频链接、标题和说明。
首先,你需要ytsearchN:
询问N
结果。其次,确保您拥有最新版本youtube-dl
(我对旧版本有一些问题)。
以下基本脚本将获取 5 个结果,显示它们的标题和 URL,并询问下载哪些结果。使其响应命令“vN”和“dN”将很简单(“dN”实际上已经实现);不过,我不确定你如何获得下一页的结果。
#!/bin/bash
tempfile=$(mktemp)
youtube_dl_log=$(mktemp)
youtube-dl -j "ytsearch5:$*" > $tempfile
# workaround for lack of mapfile in bash < 4
# https://stackoverflow.com/a/41475317/6598435
while IFS= read -r line
do
youtube_urls+=("$line")
done < <(cat $tempfile | jq '.webpage_url' | tr -d '"' )
# # for bash >= 4
# mapfile -t youtube_urls < <(cat $tempfile | jq '.webpage_url' | tr -d '"' )
cat $tempfile | jq '.fulltitle, .webpage_url'
while :
do
echo "Enter video number to download."
read i
# don't download anything if you just press enter
if [ ! x"$i" == x"" ]
then
# to make numbering of videos more intuitive (start from 1 not 0)
youtube-dl --no-progress ${youtube_urls[$i - 1]} &
fi
done
Run Code Online (Sandbox Code Playgroud)
您可能希望将输出重定向youtube-dl
到文件(或/dev/null
),尽管它也可能被认为有用。