目录
前言
笔者经常上 Youtube 去观注一些科技视频,也经常将一些有用的视频下载回来观看。有许多应用程序可用于下载 Youtube 视频,例如 ClipGrab、Streamlink 和 Mps-youtube 等等。
本文我们将向大家介绍一款非常流行、实用的,名为 youtube-dl 的 Youtube 音/视频下载专用程序。
如果经常下载Youtube视频的老司机,尤其是程序猿,应该都用过 youtube-dl。
Youtube-dl 是一款采用 Python 编写的免费、开源、命令行程序,可一次下载单个视频、多个视频或整个播放列表,且同时支持 GNU/Linux、macOS 和 Microsoft Windows 这三大主流操作系统平台。
小白注:
最近可能是youtube-dl的维护没有跟上Youtube规则的变更,下载速度出奇的慢……
此时,yt-dlp进入视野,而yt-dlp 是现在陷入困境的 youtube-dl 的一个分支,除了积极维护之外,yt-dlp 最舒服的地方应该是它遵循与 youtube-dl 类似的命令。
yt-dlp项目官方:https://github.com/yt-dlp/yt-dlp
操作
安装yt-dlp
#安装依赖
apt update -y && apt install -y ffmpeg
#二进制包安装
sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp
#pip安装
sudo apt update -y
sudo apt install -y python3-venv python3-pip
python3 -m pip install -U yt-dlp
#更新
yt-dlp -U
安装Youtube-dl工具
官方推荐的 Youtube-dl 安装方式非常简单,直接按您使用的操作系统平台将其保存到 PATH 路径中,即可执行并立即开始使用:
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
如果你的系统中没有 curl,可以使用 wget 替代:
sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
下载完成后,需要指定其执行权限:
sudo chmod a+rx /usr/local/bin/youtube-dl
或者,你也可以使用 pip 来安装它:
sudo -H pip install --upgrade youtube-dl
除了直接从官方下载以外,很多主流 Linux 发行版本的官方存储库或一些 PPA 中都收录了 youtube-dl,例如:
- Arch Linux sudo pacman -S youtube-dl
- Debian 或 Ubuntu sudo apt install youtube-dl
- Fedora sudo dnf install youtube-dl
详细教程与示例
鉴于 youtube-dl
下载速度有时候会过慢,因此以下示例皆用 yt-dlp
演示,也可把下面命令 yt-dlp
替换成
youtube-dl
1. 下载视频或播放列表
要从 Youtube 下载视频或整个视频播放列表,只需直接使用 URL 即可:
yt-dlp https://www.youtube.com/watch?v=xIFfO7sfOyY
输出如下:
root@localhost:~# yt-dlp https://www.youtube.com/watch?v=xIFfO7sfOyY
[youtube] xIFfO7sfOyY: Downloading webpage
[youtube] xIFfO7sfOyY: Downloading android player API JSON
[youtube] xIFfO7sfOyY: Downloading player 596ef930
[info] xIFfO7sfOyY: Downloading 1 format(s): 315+251
[download] Destination: K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) [xIFfO7sfOyY].f315.webm
[download] 100% of 395.99MiB in 00:07
[download] Destination: K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) [xIFfO7sfOyY].f251.webm
[download] 100% of 3.43MiB in 00:00
[Merger] Merging formats into "K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) [xIFfO7sfOyY].webm"
Deleting original file K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) [xIFfO7sfOyY].f315.webm (pass -k to keep)
Deleting original file K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) [xIFfO7sfOyY].f251.webm (pass -k to keep)
如果要指定视频下载之后的名称,可以使用如下方式:
yt-dlp -o 'K/DA - MORE 4K WITH 60 FPS.mp4' https://www.youtube.com/watch?v=xIFfO7sfOyY
输出如下:
root@localhost:~# yt-dlp -o 'K/DA - MORE 4K WITH 60 FPS.mp4' https://www.youtube.com/watch?v=xIFfO7sfOyY
[youtube] xIFfO7sfOyY: Downloading webpage
[youtube] xIFfO7sfOyY: Downloading android player API JSON
[info] xIFfO7sfOyY: Downloading 1 format(s): 315+251
[download] Destination: K/DA - MORE 4K WITH 60 FPS.mp4.f315.webm
[download] 100% of 395.99MiB in 00:07
[download] Destination: K/DA - MORE 4K WITH 60 FPS.mp4.f251.webm
[download] 100% of 3.43MiB in 00:00
[Merger] Merging formats into "K/DA - MORE 4K WITH 60 FPS.mp4.webm"
Deleting original file K/DA - MORE 4K WITH 60 FPS.mp4.f315.webm (pass -k to keep)
Deleting original file K/DA - MORE 4K WITH 60 FPS.mp4.f251.webm (pass -k to keep)
当然,你还可以在下载视频时附加更多详细信息,可用的参数就有:标题、上传者名称(频道名称)和视频上传日期等:
yt-dlp -o '%(title)s by %(uploader)s on %(upload_date)s in %(playlist)s.%(ext)s' https://www.youtube.com/watch?v=xIFfO7sfOyY
输出如下:
root@localhost:~# yt-dlp -o '%(title)s by %(uploader)s on %(upload_date)s in %(playlist)s.%(ext)s' https://www.youtube.com/watch?v=xIFfO7sfOyY
[youtube] xIFfO7sfOyY: Downloading webpage
[youtube] xIFfO7sfOyY: Downloading android player API JSON
[info] xIFfO7sfOyY: Downloading 1 format(s): 315+251
[download] Destination: K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) by Reynaldi Arya on 20201110 in NA.f315.webm
[download] 100% of 395.99MiB in 00:07
[download] Destination: K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) by Reynaldi Arya on 20201110 in NA.f251.webm
[download] 100% of 3.43MiB in 00:00
[Merger] Merging formats into "K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) by Reynaldi Arya on 20201110 in NA.webm"
Deleting original file K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) by Reynaldi Arya on 20201110 in NA.f315.webm (pass -k to keep)
Deleting original file K_DA - MORE 4K WITH 60 FPS (REMAKE MUSIC VIDEO) by Reynaldi Arya on 20201110 in NA.f251.webm (pass -k to keep)
2. 下载多个视频
有时,我们需要一次从 Youtube 上下载多个不同的视频,此时我们只需用 空格 将多个 URL 分隔开即可:
yt-dlp
如:
root@localhost:~# yt-dlp https://www.youtube.com/watch?v=yqtCGojXEpM https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] yqtCGojXEpM: Downloading webpage
[youtube] yqtCGojXEpM: Downloading android player API JSON
[info] yqtCGojXEpM: Downloading 1 format(s): 313+251
[download] Destination: IU(아이유) 'Blueming(블루밍)' 라이브??(밴드ver.) _ 가사 _ 스페셜클립 _ Special Clip _ LYRICS [4K] [yqtCGojXEpM].f313.webm
[download] 100% of 467.33MiB in 00:09
[download] Destination: IU(아이유) 'Blueming(블루밍)' 라이브??(밴드ver.) _ 가사 _ 스페셜클립 _ Special Clip _ LYRICS [4K] [yqtCGojXEpM].f251.webm
[download] 100% of 3.77MiB in 00:00
[Merger] Merging formats into "IU(아이유) 'Blueming(블루밍)' 라이브??(밴드ver.) _ 가사 _ 스페셜클립 _ Special Clip _ LYRICS [4K] [yqtCGojXEpM].webm"
Deleting original file IU(아이유) 'Blueming(블루밍)' 라이브??(밴드ver.) _ 가사 _ 스페셜클립 _ Special Clip _ LYRICS [4K] [yqtCGojXEpM].f313.webm (pass -k to keep)
Deleting original file IU(아이유) 'Blueming(블루밍)' 라이브??(밴드ver.) _ 가사 _ 스페셜클립 _ Special Clip _ LYRICS [4K] [yqtCGojXEpM].f251.webm (pass -k to keep)
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 315+251
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f315.webm
[download] 100% of 220.08MiB in 00:04
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f251.webm
[download] 100% of 3.49MiB in 00:00
[Merger] Merging formats into "[ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm"
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f251.webm (pass -k to keep)
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f315.webm (pass -k to keep)
或者,您可以将要下载视频的 URL 全部放在文本文件中,并将其作为参数传递给 Youtube-dl 也行:
yt-dlp -a url.txt
以上命令将下载 url.txt 文件中所有 URL 指向的视频。
3. 只下载(视频中的)音频
yt-dlp 允许我们仅从 Youtube 视频下载其音频,例如:
yt-dlp -x https://www.youtube.com/watch?v=9nTprPZ-WCI
样例:
root@localhost:~# yt-dlp -x https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 251
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm
[download] 100% of 3.49MiB in 00:00
[ExtractAudio] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].opus
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm (pass -k to keep)
默认情况下,yt-dlp 将以 Ogg(opus)格式保存音频,如果想以任何其他格式下载音频,例如 mp3 请运行:
yt-dlp -x --audio-format mp3 https://www.youtube.com/watch?v=iJvr0VPsn-s
输出:
root@localhost:~# yt-dlp -x --audio-format mp3 https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 251
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm
[download] 100% of 3.49MiB in 00:00
[ExtractAudio] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].mp3
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm (pass -k to keep)
此命令将从给定的视频/播放列表下载音频,将其转换为 MP3 并将其保存在当前目录中。
注意:您应该安装 ffmpeg 或 avconv 将文件转换为 mp3 格式。
4. 下载带有描述、元数据、注释、字幕和缩略图的视频
要下载视频及其他详细信息,如:说明、元数据、注释、字幕和缩略图等,请使用以下命令:
yt-dlp --write-description --write-info-json --write-annotations --write-sub --write-thumbnail https://www.youtube.com/watch?v=9nTprPZ-WCI
输出如下:
root@localhost:~# yt-dlp --write-description --write-info-json --write-annotations --write-sub --write-thumbnail https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 315+251
[info] Writing video description to: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].description
[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webp
[info] Writing video metadata as JSON to: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].info.json
WARNING: There are no annotations to write.
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f315.webm
[download] 100% of 220.08MiB in 00:04
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f251.webm
[download] 100% of 3.49MiB in 00:00
[Merger] Merging formats into "[ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm"
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f251.webm (pass -k to keep)
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f315.webm (pass -k to keep)
5. 列出所有可用的音/视频格式
Youtube 网站上的视频和音频会被自动转码成多种音/视频格式,要查看某个视频或播放列表所有可下载的音/视频格式,请使用以下命令:
yt-dlp --list-formats https://www.youtube.com/watch?v=9nTprPZ-WCI
输出如下:
root@localhost:~# yt-dlp --list-formats https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] Available formats for 9nTprPZ-WCI:
ID EXT RESOLUTION FPS │ FILESIZE TBR PROTO │ VCODEC VBR ACODEC ABR ASR MORE INFO
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sb2 mhtml 48x27 │ mhtml │ images storyboard
sb1 mhtml 80x45 │ mhtml │ images storyboard
sb0 mhtml 160x90 │ mhtml │ images storyboard
139 m4a audio only │ 1.29MiB 48k https │ audio only mp4a.40.5 48k 22050Hz low, m4a_dash
249 webm audio only │ 1.37MiB 51k https │ audio only opus 51k 48000Hz low, webm_dash
250 webm audio only │ 1.80MiB 68k https │ audio only opus 68k 48000Hz low, webm_dash
140 m4a audio only │ 3.42MiB 129k https │ audio only mp4a.40.2 129k 44100Hz medium, m4a_dash
251 webm audio only │ 3.49MiB 131k https │ audio only opus 131k 48000Hz medium, webm_dash
17 3gp 176x144 7 │ 2.08MiB 78k https │ mp4v.20.3 78k mp4a.40.2 0k 22050Hz 144p
160 mp4 256x144 30 │ 1.88MiB 71k https │ avc1.4d400c 71k video only 144p, mp4_dash
278 webm 256x144 30 │ 1.89MiB 71k https │ vp9 71k video only 144p, webm_dash
133 mp4 426x240 30 │ 4.07MiB 153k https │ avc1.4d4015 153k video only 240p, mp4_dash
242 webm 426x240 30 │ 2.66MiB 100k https │ vp9 100k video only 240p, webm_dash
134 mp4 640x360 30 │ 7.49MiB 283k https │ avc1.4d401e 283k video only 360p, mp4_dash
18 mp4 640x360 30 │ ~ 11.16MiB 411k https │ avc1.42001E 411k mp4a.40.2 0k 44100Hz 360p
243 webm 640x360 30 │ 4.00MiB 151k https │ vp9 151k video only 360p, webm_dash
135 mp4 854x480 30 │ 13.04MiB 493k https │ avc1.4d401f 493k video only 480p, mp4_dash
244 webm 854x480 30 │ 6.16MiB 233k https │ vp9 233k video only 480p, webm_dash
136 mp4 1280x720 30 │ 24.28MiB 919k https │ avc1.4d401f 919k video only 720p, mp4_dash
22 mp4 1280x720 30 │ ~ 28.39MiB 1047k https │ avc1.64001F 1047k mp4a.40.2 0k 44100Hz 720p
247 webm 1280x720 30 │ 10.71MiB 405k https │ vp9 405k video only 720p, webm_dash
298 mp4 1280x720 60 │ 35.38MiB 1339k https │ avc1.4d4020 1339k video only 720p60, mp4_dash
302 webm 1280x720 60 │ 16.46MiB 622k https │ vp9 622k video only 720p60, webm_dash
299 mp4 1920x1080 60 │ 70.05MiB 2651k https │ avc1.64002a 2651k video only 1080p60, mp4_dash
303 webm 1920x1080 60 │ 22.37MiB 846k https │ vp9 846k video only 1080p60, webm_dash
308 webm 2560x1440 60 │ 87.72MiB 3320k https │ vp9 3320k video only 1440p60, webm_dash
315 webm 3840x2160 60 │ 220.08MiB 8330k https │ vp9 8330k video only 2160p60, webm_dash
或者笔者常用的简写方式:
yt-dlp -F https://www.youtube.com/watch?v=9nTprPZ-WCI
输出如下:
root@localhost:~# yt-dlp -F https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] Available formats for 9nTprPZ-WCI:
ID EXT RESOLUTION FPS │ FILESIZE TBR PROTO │ VCODEC VBR ACODEC ABR ASR MORE INFO
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
sb2 mhtml 48x27 │ mhtml │ images storyboard
sb1 mhtml 80x45 │ mhtml │ images storyboard
sb0 mhtml 160x90 │ mhtml │ images storyboard
139 m4a audio only │ 1.29MiB 48k https │ audio only mp4a.40.5 48k 22050Hz low, m4a_dash
249 webm audio only │ 1.37MiB 51k https │ audio only opus 51k 48000Hz low, webm_dash
250 webm audio only │ 1.80MiB 68k https │ audio only opus 68k 48000Hz low, webm_dash
140 m4a audio only │ 3.42MiB 129k https │ audio only mp4a.40.2 129k 44100Hz medium, m4a_dash
251 webm audio only │ 3.49MiB 131k https │ audio only opus 131k 48000Hz medium, webm_dash
17 3gp 176x144 7 │ 2.08MiB 78k https │ mp4v.20.3 78k mp4a.40.2 0k 22050Hz 144p
160 mp4 256x144 30 │ 1.88MiB 71k https │ avc1.4d400c 71k video only 144p, mp4_dash
278 webm 256x144 30 │ 1.89MiB 71k https │ vp9 71k video only 144p, webm_dash
133 mp4 426x240 30 │ 4.07MiB 153k https │ avc1.4d4015 153k video only 240p, mp4_dash
242 webm 426x240 30 │ 2.66MiB 100k https │ vp9 100k video only 240p, webm_dash
134 mp4 640x360 30 │ 7.49MiB 283k https │ avc1.4d401e 283k video only 360p, mp4_dash
18 mp4 640x360 30 │ ~ 11.16MiB 411k https │ avc1.42001E 411k mp4a.40.2 0k 44100Hz 360p
243 webm 640x360 30 │ 4.00MiB 151k https │ vp9 151k video only 360p, webm_dash
135 mp4 854x480 30 │ 13.04MiB 493k https │ avc1.4d401f 493k video only 480p, mp4_dash
244 webm 854x480 30 │ 6.16MiB 233k https │ vp9 233k video only 480p, webm_dash
136 mp4 1280x720 30 │ 24.28MiB 919k https │ avc1.4d401f 919k video only 720p, mp4_dash
22 mp4 1280x720 30 │ ~ 28.39MiB 1047k https │ avc1.64001F 1047k mp4a.40.2 0k 44100Hz 720p
247 webm 1280x720 30 │ 10.71MiB 405k https │ vp9 405k video only 720p, webm_dash
298 mp4 1280x720 60 │ 35.38MiB 1339k https │ avc1.4d4020 1339k video only 720p60, mp4_dash
302 webm 1280x720 60 │ 16.46MiB 622k https │ vp9 622k video only 720p60, webm_dash
299 mp4 1920x1080 60 │ 70.05MiB 2651k https │ avc1.64002a 2651k video only 1080p60, mp4_dash
303 webm 1920x1080 60 │ 22.37MiB 846k https │ vp9 846k video only 1080p60, webm_dash
308 webm 2560x1440 60 │ 87.72MiB 3320k https │ vp9 3320k video only 1440p60, webm_dash
315 webm 3840x2160 60 │ 220.08MiB 8330k https │ vp9 8330k video only 2160p60, webm_dash
如上所示,Yyt-dlp列出了给定视频的所有可用格式,从左到右分别为:format code(视频格式代码)、extension(扩展名)、resolution(分辨率)和 note(注释deng )等。当您想要以特定质量或格式下载视频时,先查看一下有哪些可用,会非常便利。
6. 以某种质量和/或格式下载视频
默认情况下,yt-dlp将自主选择最佳质量的视频下载。 但是,也可以以特定的质量或格式来下载视频或播放列表。
yt-dlp 支持以下品质:
- best 选择最佳质量的音/视频文件
- worst 选择质量最差的格式(视频和音频)
- bestvideo 选择最佳质量的仅视频格式(例如DASH视频),可能无法使用。
- worstvideo 选择质量最差的纯视频格式,可能无法使用。
- bestaudio 选择最优质的音频格式,可能无法使用。
- worstaudio 选择质量最差的音频格式,可能无法使用。
例如,如果要自动选择并下载最佳质量格式(音频和视频),只需使用以下命令:
yt-dlp -f best https://www.youtube.com/watch?v=9nTprPZ-WCI
同样,要以最佳质量仅下载音频,可执行:
yt-dlp -f bestaudio https://www.youtube.com/watch?v=9nTprPZ-WCI
您还可以组合使用以下不同的格式选项:
yt-dlp -f bestvideo+bestaudio https://www.youtube.com/watch?v=9nTprPZ-WCI
上述命令将分别下载 最高质量的仅视频和最高质量的纯音频格式,再用 ffmpeg 或 avconv 合并成一个最佳质量的 mkv 文件;如果您不想合并,请将 + (加号)替换为 , (逗号)即可分别得到最高质量的音频和视频(两个文件),如下所示:
yt-dlp -f 'bestvideo,bestaudio' https://www.youtube.com/watch?v=9nTprPZ-WCI
输出:
root@localhost:~# yt-dlp -f 'bestvideo,bestaudio' https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 2 format(s): 315, 251
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm
[download] 100% of 220.08MiB in 00:04
[download] [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm has already been downloaded
[download] 100% of 220.08MiB
7. 通过视频代码下载文件(常用方法)
前面方法 5 已经提到过,所有 Youtube 视频都有格式代码,我们可以用它来下载特定质量的视频。
例如,先用方法 5 查看所有可用的音/视频格式及其对应的format code(视频格式代码):
yt-dlp -F https://www.youtube.com/watch?v=9nTprPZ-WCI
再通过代码来下载指定的音/视频格式,例如要下载 best 品质(格式代码为 315)的视频文件,则执行以下命令:
yt-dlp -f 315 https://www.youtube.com/watch?v=9nTprPZ-WCI
输出:
root@localhost:~# yt-dlp -f 315 https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 315
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].webm
[download] 100% of 220.08MiB in 00:05
从播放列表下载视频时,某些视频可能没有相同的格式。 在这种情况下,可以按首选顺序指定多个格式代码,例如:
yt-dlp -f 22/17/18
根据上面的示例,yt-dlp 将以格式 22 下载视频(如果可用);如果格式 22不可用,则它将下载格式 17(如果可用);如果格式 22 和 17 都不可用,最后尝试下载格式 18。如果所有格式代码都不匹配,Youtube-dl 会报出提示。还需要注意的是,斜杠是左关联的,即最左侧的格式代码是首选。
8. 通过文件扩展名下载音/视频
以您的首选格式下载视频,例如 MP4,只需执行:
yt-dlp --format mp4 https://www.youtube.com/watch?v=9nTprPZ-WCI
输出:
root@localhost:~# yt-dlp --format mp4 https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 22
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].mp4
[download] 100% of 27.67MiB in 00:00
或者
yt-dlp -f mp4 https://www.youtube.com/watch?v=iJvr0VPsn-s
输出:
root@localhost:~# yt-dlp -f mp4 https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 22
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].mp4
[download] 100% of 27.67MiB in 00:00
如我在上一节中已经提到的那样,某些视频可能无法以您的首选格式提供。 在这种情况下,yt-dlp 将下载其他最佳可用格式。 例如,此命令将下载最佳质量的 MP4 格式文件。 如果 MP4 格式不可用,则它将下载其他最佳可用格式。
yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' https://www.youtube.com/watch?v=iJvr0VPsn-s
输出:
root@localhost:~# yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' https://www.youtube.com/watch?v=9nTprPZ-WCI
[youtube] 9nTprPZ-WCI: Downloading webpage
[youtube] 9nTprPZ-WCI: Downloading android player API JSON
[info] 9nTprPZ-WCI: Downloading 1 format(s): 299+140
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f299.mp4
[download] 100% of 70.05MiB in 00:02
[download] Destination: [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f140.m4a
[download] 100% of 3.42MiB in 00:00
[Merger] Merging formats into "[ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].mp4"
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f140.m4a (pass -k to keep)
Deleting original file [ULTRA SMOOTH] IU(아이유) - 'eight(에잇)' (Feat. SUGA of BTS) MV [9nTprPZ-WCI].f299.mp4 (pass -k to keep)
9. 限制下载视频的大小
从 Youtube 播放列表下载多个视频时,您可能只想下载特定大小的视频。例如,此命令不会下载任何小于指定大小的视频,例如 100MB:
yt-dlp --min-filesize 100M
如果您不想下载大于给定大小的视频,可以这样:
yt-dlp --max-filesize 100M
我们还可以用组合格式,选择运算符来下载特定大小的视频。例如,以下命令将下载最佳视频格式但不大于 100MB 的视频:
yt-dlp -f 'best[filesize<100M]' https://www.youtube.com/watch?v=9nTprPZ-WCI
10. 按日期下载视频
Youtube-dl 允许我们按照上传日期来筛选和下载视频或播放列表,例如要下载 2019 年 8 月 1 日上传的视频,可以使用:
yt-dlp --date 20190801
下载在特定日期或之前上传的视频:
yt-dlp --datebefore 20190801
下载在特定日期或之后上传的视频:
yt-dlp --dateafter 20190101
仅下载过去 6 个月内上传的视频:
yt-dlp --dateafter now-6months
下载特定时间段内(例如 2018 年 1 月 1 日至 2019 年 1 月 1 日)上传的视频:
yt-dlp --dateafter 20180101 --datebefore 20190101
11. 从播放列表下载特定的视频
从播放列表下载特定的视频,是 Youtube-dl 的另一个非常有用的功能。例如,要从播放列表下载第 10 个文件,可使用:
yt-dlp --playlist-items 10
同样,要下载多个指定的文件,只需用逗号分隔:
yt-dlp --playlist-items 2,3,7,10
当然,也可以按序号来指定要下载范围,例如从第 10 个开始,直接下载完整个列表:
yt-dlp --playlist-start 10
或者在播放列表中仅下载从第 2 到第 5 的文件:
yt-dlp --playlist-start 2 --playlist-end 5
12. 仅下载适用于特定年龄的视频
Youtube-dl 的另一个特点是,允许我们只下载适合指定适用年龄的视频。例如,要从播放列表中下载所有未标记为「NSFW」或年龄限制为 7 岁儿童的「Let’s Play」视频,可使用:
yt-dlp --match-title "let's play" --age-limit 7 --reject-title "nsfw"
13. 使用帮助
通过上述示例的介绍,相信已经能够满足绝大多数用户对 Youtube 视频下载和 youtube-dl 的使用需求了。有关更多详细信息,请参阅 Youtube-dl 帮助:
yt-dlp --help