前言

最近在搬运youtube上Polar的相关视频时发现传统口碑较好的 youtube-dl 在下载时无论如何操作也不能突破77KiB/s,外网的讨论认为可能 Google 对于 youtube-dl 有一些反制措施。经过一番琢磨发现 yt-dlp 在当前可能是更为理想的选择。下面把今晚的尝试开列如下:

实例

D:\youtube>yt-dlp -F --proxy socks5://127.0.0.1:8080  https://www.youtube.com/watch?v=rrQJvPaPbFM
[youtube] rrQJvPaPbFM: Downloading webpage
[youtube] rrQJvPaPbFM: Downloading android player API JSON
[info] Available formats for rrQJvPaPbFM:
ID  EXT  RESOLUTION FPS |  FILESIZE   TBR PROTO | VCODEC        VBR ACODEC     ABR  ASR    MORE INFO
--- ---- ---------- --- - --------- ----- ----- - ----------- ----- --------- ---- ------- -----------------
139 m4a  audio only     |  1.25MiB    48k https |                   mp4a.40.5  48k 22050Hz low, m4a_dash
249 webm audio only     |  1.32MiB    51k https |                   opus       51k 48000Hz low, webm_dash
250 webm audio only     |  1.76MiB    68k https |                   opus       68k 48000Hz low, webm_dash
140 m4a  audio only     |  3.32MiB   129k https |                   mp4a.40.2 129k 44100Hz medium, m4a_dash
251 webm audio only     |  3.53MiB   137k https |                   opus      137k 48000Hz medium, webm_dash
17  3gp  176x144    6   |  1.98MiB    77k https | mp4v.20.3     77k mp4a.40.2   0k 22050Hz 144p
160 mp4  256x144    25  |  2.34MiB    91k https | avc1.4d400c   91k                        144p, mp4_dash
278 webm 256x144    25  |  2.33MiB    90k https | vp9           90k                        144p, webm_dash
133 mp4  426x240    25  |  5.24MiB   204k https | avc1.4d4015  204k                        240p, mp4_dash
242 webm 426x240    25  |  4.03MiB   157k https | vp9          157k                        240p, webm_dash
134 mp4  640x360    25  |  10.53MiB  410k https | avc1.4d401e  410k                        360p, mp4_dash
18  mp4  640x360    25  |  12.97MiB  505k https | avc1.42001E  505k mp4a.40.2   0k 44100Hz 360p
243 webm 640x360    25  |  6.97MiB   271k https | vp9          271k                        360p, webm_dash
135 mp4  854x480    25  |  20.45MiB  797k https | avc1.4d401e  797k                        480p, mp4_dash
244 webm 854x480    25  |  12.25MiB  477k https | vp9          477k                        480p, webm_dash
136 mp4  1280x720   25  |  39.90MiB 1555k https | avc1.64001f 1555k                        720p, mp4_dash
22  mp4  1280x720   25  |           1684k https | avc1.64001F 1684k mp4a.40.2   0k 44100Hz 720p
247 webm 1280x720   25  |  24.14MiB  941k https | vp9          941k                        720p, webm_dash
137 mp4  1920x1080  25  |  85.85MiB 3347k https | avc1.640028 3347k                        1080p, mp4_dash
248 webm 1920x1080  25  |  43.23MiB 1685k https | vp9         1685k                        1080p, webm_dash

D:\youtube>yt-dlp -f 137+140 --proxy socks5://127.0.0.1:8080 https://www.youtube.com/watch?v=rrQJvPaPbFM --merge-output-format mp4 --external-downloader aria2c --downloader-args aria2c:"-x 16 -k 1M"
[youtube] rrQJvPaPbFM: Downloading webpage
[youtube] rrQJvPaPbFM: Downloading android player API JSON
[info] rrQJvPaPbFM: Downloading 1 format(s): 137+140
[download] Destination: Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f137.mp4
[download] 100% of 85.85MiB in 00:53
[download] Destination: Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f140.m4a
[download] 100% of 3.32MiB in 00:04
[Merger] Merging formats into "Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].mp4"
Deleting original file Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f140.m4a (pass -k to keep)
Deleting original file Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f137.mp4 (pass -k to keep)

实际测试85.85MiB的视频部分下载时间为53s,合1.62MiB/s的速度。不算狂暴但优于先前 youtube-dl 的77KiB/s。

基本安装

默认拥有python3环境,此处不展开。

ffmpeg 安装

用于对视频和音轨的合并。请参考:win10安装使用ffmpeg+python中使用ffmpy – 程嘿嘿 – 博客园的前半部分。

aria2 安装

作为外部下载器加速下载进程。但我个人感觉 aria2c 在 yt-dlp 上没有体现足够的加持。请参考:aria2 使用简介 ,将aria2c加入环境变量即可。

yt-dlp 安装

在cmd内输入

pip install yt-dlp

开始下载

查看视频链接格式构成

yt-dlp -F [视频链接]
-F #查看视频格式构成
--proxy [系统代理] #填写你的系统代理设置,如果使用全局模式可不使用此语句。

效果1

yt-dlp -F --proxy socks5://127.0.0.1:8080  https://www.youtube.com/watch?v=rrQJvPaPbFM
[youtube] rrQJvPaPbFM: Downloading webpage
[youtube] rrQJvPaPbFM: Downloading android player API JSON
[info] Available formats for rrQJvPaPbFM:
ID  EXT  RESOLUTION FPS |  FILESIZE   TBR PROTO | VCODEC        VBR ACODEC     ABR  ASR    MORE INFO
--- ---- ---------- --- - --------- ----- ----- - ----------- ----- --------- ---- ------- -----------------
139 m4a  audio only     |  1.25MiB    48k https |                   mp4a.40.5  48k 22050Hz low, m4a_dash
249 webm audio only     |  1.32MiB    51k https |                   opus       51k 48000Hz low, webm_dash
250 webm audio only     |  1.76MiB    68k https |                   opus       68k 48000Hz low, webm_dash
140 m4a  audio only     |  3.32MiB   129k https |                   mp4a.40.2 129k 44100Hz medium, m4a_dash
251 webm audio only     |  3.53MiB   137k https |                   opus      137k 48000Hz medium, webm_dash
17  3gp  176x144    6   |  1.98MiB    77k https | mp4v.20.3     77k mp4a.40.2   0k 22050Hz 144p
160 mp4  256x144    25  |  2.34MiB    91k https | avc1.4d400c   91k                        144p, mp4_dash
278 webm 256x144    25  |  2.33MiB    90k https | vp9           90k                        144p, webm_dash
133 mp4  426x240    25  |  5.24MiB   204k https | avc1.4d4015  204k                        240p, mp4_dash
242 webm 426x240    25  |  4.03MiB   157k https | vp9          157k                        240p, webm_dash
134 mp4  640x360    25  |  10.53MiB  410k https | avc1.4d401e  410k                        360p, mp4_dash
18  mp4  640x360    25  |  12.97MiB  505k https | avc1.42001E  505k mp4a.40.2   0k 44100Hz 360p
243 webm 640x360    25  |  6.97MiB   271k https | vp9          271k                        360p, webm_dash
135 mp4  854x480    25  |  20.45MiB  797k https | avc1.4d401e  797k                        480p, mp4_dash
244 webm 854x480    25  |  12.25MiB  477k https | vp9          477k                        480p, webm_dash
136 mp4  1280x720   25  |  39.90MiB 1555k https | avc1.64001f 1555k                        720p, mp4_dash
22  mp4  1280x720   25  |           1684k https | avc1.64001F 1684k mp4a.40.2   0k 44100Hz 720p
247 webm 1280x720   25  |  24.14MiB  941k https | vp9          941k                        720p, webm_dash
137 mp4  1920x1080  25  |  85.85MiB 3347k https | avc1.640028 3347k                        1080p, mp4_dash
248 webm 1920x1080  25  |  43.23MiB 1685k https | vp9         1685k                        1080p, webm_dash

从结果中可知最佳画质视频ID为mp4格式的137和webm格式的248(纯视频无音轨),最佳音轨ID为m4a格式的140和webm格式的251(纯音轨无视频)。如果你认为720p已经满足需求,则只下载ID22也可(已经包含视频和音轨)。这里我们先选择mp4的视频和m4a的音轨进行合并。

下载及合并

yt-dlp -f [下载ID] [代理配置] [视频链接] [合并语句] [外部下载器选择] [下载器参数]
-f [id] #选择下载内容,注意和 -F 区分。例子中使用137+140,如果你只下载720p则填写22就好,后面的合并语句可不填写。
--proxy #代理配置 见前文
--merge-output-format [合并输出格式] #例子中选择mp4作为输出格式
--external-downloader [下载器名称] #下载器选择 例子中选择aria2c
--downloader-args [下载器名称]:"[下载器配置]" #下载器配置语句 例子中 x 16 代表16线程下载 -k 1M 代表块大小为1M 但youtube不支持分块故此句可忽略

效果2

D:\youtube>yt-dlp -f 137+140 --proxy socks5://127.0.0.1:8080 https://www.youtube.com/watch?v=rrQJvPaPbFM --merge-output-format mp4 --external-downloader aria2c --downloader-args aria2c:"-x 16 -k 1M"
[youtube] rrQJvPaPbFM: Downloading webpage
[youtube] rrQJvPaPbFM: Downloading android player API JSON
[info] rrQJvPaPbFM: Downloading 1 format(s): 137+140
[download] Destination: Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f137.mp4
[download] 100% of 85.85MiB in 00:53
[download] Destination: Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f140.m4a
[download] 100% of 3.32MiB in 00:04
[Merger] Merging formats into "Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].mp4"
Deleting original file Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f140.m4a (pass -k to keep)
Deleting original file Polar Grit X Pro _ Route And Elevation Profiles [rrQJvPaPbFM].f137.mp4 (pass -k to keep)

下载完成后ffmpeg合并视频和音轨,输出至你的cmd的当前路径。