youtube-dl 获取频道/播放列表名称并将其传递给 bash 一行脚本
youtube-dl get channel / playlist name and pass it to bash one line script
我正在尝试下载整个 Youtube 频道并且成功了。
但是我的目录名称如下所示,因此我需要手动更改所有名称。
我需要一种方法来将频道/播放列表名称和 ID 传递给脚本,而不是获取 url。
我使用的脚本:
# get working/beginning directory
l=$(pwd);
clear;
# get playlists data from channel list
youtube-dl -j --flat-playlist \
"https://www.youtube.com/channel/UC-QDfvrRIDB6F0bIO4I4HkQ/playlists" \
|cut -d ' ' -f4 \
|cut -c 2-73 \
|while IFS= read -r line;
do;
# loop: do with every playlist link
# make a directory named by the playlist identifier in url
mkdir ${line:38:80};
# change directory to new directory
cd $l/${line:38:80};
# download playlist
youtube-dl -f mp4 "$line";
# print playlist absolute dir to user
pwd;
# change directory to beginning directory
cd $l;
done;
目录名称:
.
├── PLxl69kCRkiI0oIqgQW3gWjDfI-e7ooTUF
├── PLxl69kCRkiI0q0Ib8lm3ZJsG3HltLQDuQ
├── PLxl69kCRkiI1Ebm-yvZyUKnfoi6VVNdQ7
├── ...
└── PLxl69kCRkiI3u-k02uTpu7z4wzYLOE3sq
这不起作用:
https://github.com/ytdl-org/youtube-dl/issues/23442
# any playlist is seen as private
youtube-dl -J \
https://m.youtube.com/playlist?list=PL3GeP3YLZn5jOiHM8Js1_S0p_5HeS7TbY \
| jq -r '.title'
How to use youtube-dl from a python program?
我需要它用于 bash 脚本而不是 python
编辑:简单解释
如何从 bash youtube-dl 获取频道名称并将其替换为此脚本中文件名的列表 ID
考虑以下几点:
#!/usr/bin/env bash
# if we don't delete slashes from titles there are serious security issues here
slash=/
# note that this url, being in quotes, needs no backslash escaping.
url='https://www.youtube.com/playlist?list=PLXmMXHVSvS-CoYS177-UvMAQYRfL3fBtX'
# First, get the name for the whole playlist
playlist_title=$(youtube-dl -J --flat-playlist "$url" | jq -r .title) || exit
# ...and, for the rest of the script, work under a directory named by that title
mkdir -p -- "${playlist_title//$slash/}" || exit
cd "${playlist_title//$slash/}" || exit
# Finally, loop over the individual videos and download them one at a time.
# ...arguably, you could tell youtube-dl to do this itself; call it an exercise.
youtube-dl -j --flat-playlist "$url" | # one JSON document per playlist entry
jq -r '[.id, .title] | @tsv' | # write id, then title, tab-separated
while IFS=$'\t' read -r id title; do ( # read that id and title from jq
# because of the ()s, this is a subshell; exits just go to the next item
# ...which is also why exec doesn't end the entire script.
dir=${title//$slash/} # take slashes out of the title to form directory
mkdir -p -- "$dir" || exit
cd -- "$dir" || exit # if cd fails, do not download anything
exec youtube-dl "$id" # exec is a minor perf optimization; consume subshell
); done
注:
- 我们正在使用 jq 将 JSON 转换为更 safely-readable 的格式,我们可以在没有字节偏移的情况下进行解析。
- 多余的反斜杠已从 URL 中删除,以防止问题评论中描述的 404 错误。
- 将循环的 body 放在带括号的子 shell 中意味着当退出括号部分时,该子 shell 中的
cd
会自动反转。
- 我们不相信标题不包含斜线 -- 您不需要有人为他们的视频命名
/etc/sudoers.d/hi-there
或以其他方式将文件放在 arbitrarily-chosen 位置。
- 请注意
"$dir"
而不是 $dir
。这很重要;参见 shellcheck warning SC2086。
我正在尝试下载整个 Youtube 频道并且成功了。
但是我的目录名称如下所示,因此我需要手动更改所有名称。
我需要一种方法来将频道/播放列表名称和 ID 传递给脚本,而不是获取 url。
我使用的脚本:
# get working/beginning directory
l=$(pwd);
clear;
# get playlists data from channel list
youtube-dl -j --flat-playlist \
"https://www.youtube.com/channel/UC-QDfvrRIDB6F0bIO4I4HkQ/playlists" \
|cut -d ' ' -f4 \
|cut -c 2-73 \
|while IFS= read -r line;
do;
# loop: do with every playlist link
# make a directory named by the playlist identifier in url
mkdir ${line:38:80};
# change directory to new directory
cd $l/${line:38:80};
# download playlist
youtube-dl -f mp4 "$line";
# print playlist absolute dir to user
pwd;
# change directory to beginning directory
cd $l;
done;
目录名称:
.
├── PLxl69kCRkiI0oIqgQW3gWjDfI-e7ooTUF
├── PLxl69kCRkiI0q0Ib8lm3ZJsG3HltLQDuQ
├── PLxl69kCRkiI1Ebm-yvZyUKnfoi6VVNdQ7
├── ...
└── PLxl69kCRkiI3u-k02uTpu7z4wzYLOE3sq
这不起作用:
https://github.com/ytdl-org/youtube-dl/issues/23442
# any playlist is seen as private youtube-dl -J \ https://m.youtube.com/playlist?list=PL3GeP3YLZn5jOiHM8Js1_S0p_5HeS7TbY \ | jq -r '.title'
How to use youtube-dl from a python program?
我需要它用于 bash 脚本而不是 python
编辑:简单解释
如何从 bash youtube-dl 获取频道名称并将其替换为此脚本中文件名的列表 ID
考虑以下几点:
#!/usr/bin/env bash
# if we don't delete slashes from titles there are serious security issues here
slash=/
# note that this url, being in quotes, needs no backslash escaping.
url='https://www.youtube.com/playlist?list=PLXmMXHVSvS-CoYS177-UvMAQYRfL3fBtX'
# First, get the name for the whole playlist
playlist_title=$(youtube-dl -J --flat-playlist "$url" | jq -r .title) || exit
# ...and, for the rest of the script, work under a directory named by that title
mkdir -p -- "${playlist_title//$slash/}" || exit
cd "${playlist_title//$slash/}" || exit
# Finally, loop over the individual videos and download them one at a time.
# ...arguably, you could tell youtube-dl to do this itself; call it an exercise.
youtube-dl -j --flat-playlist "$url" | # one JSON document per playlist entry
jq -r '[.id, .title] | @tsv' | # write id, then title, tab-separated
while IFS=$'\t' read -r id title; do ( # read that id and title from jq
# because of the ()s, this is a subshell; exits just go to the next item
# ...which is also why exec doesn't end the entire script.
dir=${title//$slash/} # take slashes out of the title to form directory
mkdir -p -- "$dir" || exit
cd -- "$dir" || exit # if cd fails, do not download anything
exec youtube-dl "$id" # exec is a minor perf optimization; consume subshell
); done
注:
- 我们正在使用 jq 将 JSON 转换为更 safely-readable 的格式,我们可以在没有字节偏移的情况下进行解析。
- 多余的反斜杠已从 URL 中删除,以防止问题评论中描述的 404 错误。
- 将循环的 body 放在带括号的子 shell 中意味着当退出括号部分时,该子 shell 中的
cd
会自动反转。 - 我们不相信标题不包含斜线 -- 您不需要有人为他们的视频命名
/etc/sudoers.d/hi-there
或以其他方式将文件放在 arbitrarily-chosen 位置。 - 请注意
"$dir"
而不是$dir
。这很重要;参见 shellcheck warning SC2086。