0%

youtube-dl

youtube-dl

youtube-dl是由某大佬开发的专攻YouTube视频下载的python脚本,使用简单、功能齐全、体积小巧

安装python,youtube-dl

该脚本基于python语言编写,自然需要安装python。
安装youtube-dl

1
pip install youtube-dl
安装FFmpeg

YouTube 1080P及以上清晰度视频,视频轨、音频轨及字幕是分离的,下载到本地由播放器直接进行混流播放。这也是许多在线下载网站,抓取高清晰度视频需要下载其本地程序的原因,多数程序都是自带FFmpeg,并在下载后利用它封装。

Windows系统前往FFmpeg-Builds releases - BtbN Github,下载win64-gpl版本压缩包(N为Nightly版本,n为正式版,二者均可),解压至任意目录。随后在 系统属性→高级→环境变量→系统变量·PATH 中,将解压目录里的Bin文件夹路径添加进去。

运行cmd,输入命令ffmpeg运行,看是否安装成功。

下载

设置代理

1
2
export http_proxy=http://172.168.2.106:1080
export https_proxy=http://172.168.2.106:1080

直接下载

1
youtube-dl https://www.youtube.com/watch?v=_F5qXji9RTY -o 下载地址

获取视频所有下载流

1
youtube-dl --list-formats https://www.youtube.com/watch?v=_F5qXji9RTY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[youtube] zr6qzJfh148: Downloading webpage
[info] Available formats for zr6qzJfh148:
format code extension resolution note
249 webm audio only tiny 46k , webm_dash container, opus @ 46k (48000Hz), 1.24MiB
250 webm audio only tiny 61k , webm_dash container, opus @ 61k (48000Hz), 1.64MiB
251 webm audio only tiny 122k , webm_dash container, opus @122k (48000Hz), 3.29MiB
140 m4a audio only tiny 129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 3.47MiB
394 mp4 256x144 144p 68k , mp4_dash container, av01.0.00M.08@ 68k, 24fps, video only, 1.83MiB
160 mp4 256x144 144p 80k , mp4_dash container, avc1.4D400C@ 80k, 24fps, video only, 2.16MiB
278 webm 256x144 144p 80k , webm_dash container, vp9@ 80k, 24fps, video only, 2.17MiB
395 mp4 426x240 240p 132k , mp4_dash container, av01.0.00M.08@ 132k, 24fps, video only, 3.55MiB
242 webm 426x240 240p 161k , webm_dash container, vp9@ 161k, 24fps, video only, 4.33MiB
133 mp4 426x240 240p 171k , mp4_dash container, avc1.4D4015@ 171k, 24fps, video only, 4.60MiB
396 mp4 640x360 360p 256k , mp4_dash container, av01.0.01M.08@ 256k, 24fps, video only, 6.87MiB
243 webm 640x360 360p 335k , webm_dash container, vp9@ 335k, 24fps, video only, 8.98MiB
134 mp4 640x360 360p 339k , mp4_dash container, avc1.4D401E@ 339k, 24fps, video only, 9.11MiB
397 mp4 854x480 480p 438k , mp4_dash container, av01.0.04M.08@ 438k, 24fps, video only, 11.75MiB
135 mp4 854x480 480p 550k , mp4_dash container, avc1.4D401E@ 550k, 24fps, video only, 14.75MiB
244 webm 854x480 480p 578k , webm_dash container, vp9@ 578k, 24fps, video only, 15.51MiB
398 mp4 1280x720 720p 788k , mp4_dash container, av01.0.05M.08@ 788k, 24fps, video only, 21.14MiB
136 mp4 1280x720 720p 926k , mp4_dash container, avc1.4D401F@ 926k, 24fps, video only, 24.83MiB
247 webm 1280x720 720p 1066k , webm_dash container, vp9@1066k, 24fps, video only, 28.59MiB
399 mp4 1920x1080 1080p 1322k , mp4_dash container, av01.0.08M.08@1322k, 24fps, video only, 35.45MiB
248 webm 1920x1080 1080p 1791k , webm_dash container, vp9@1791k, 24fps, video only, 48.02MiB
137 mp4 1920x1080 1080p 2415k , mp4_dash container, avc1.640028@2415k, 24fps, video only, 64.76MiB
400 mp4 2560x1440 1440p 3682k , mp4_dash container, av01.0.12M.08@3682k, 24fps, video only, 98.73MiB
271 webm 2560x1440 1440p 4116k , webm_dash container, vp9@4116k, 24fps, video only, 110.36MiB
401 mp4 3840x2160 2160p 6882k , mp4_dash container, av01.0.12M.08@6882k, 24fps, video only, 184.51MiB
313 webm 3840x2160 2160p 10092k , webm_dash container, vp9@10092k, 24fps, video only, 270.56MiB
18 mp4 640x360 360p 626k , avc1.42001E, 24fps, mp4a.40.2 (44100Hz), 16.79MiB
22 mp4 1280x720 720p 1055k , avc1.64001F, 24fps, mp4a.40.2 (44100Hz) (best)

获取某一码率的流

1
2
3
youtube-dl -f 249(编号) -g https://www.youtube.com/watch?v=_F5qXji9RTY
youtube-dl -f 137+141 https://www.youtube.com/watch?v=zr6qzJfh148 //下载多个
youtube-dl -f best https://www.youtube.com/watch?v=zr6qzJfh148 //best 最佳质量视频源 + 最佳音频源
  1. best 最佳质量视频源 + 最佳音频源
  2. bestvideo 最佳质量视频源
  3. bestaudio 最佳质量音频源
  4. worst 最低质量视频源 + 最低音频源
  5. worstvideo 最低质量视频源
  6. worstaudio 最低质量音频源

错误

1
2
3
4
5
ERROR: unable to download video data: HTTP Error 403: Forbidden
执行:
youtube-dl --rm-cache-dir
ERROR: unable to open for writing: [Errno 36] File name too long:
-o "20221003" 指定文件名