YouTube 视频下载工具 - youtube-dl

youtube-dl 是一款开源的视频下载工具,通过它我们可以很方便的下载 YouTube 上的视频,还可以进行各种选项设置。

安装

本人使用的是 python3, 安装使用 pip :

pip install youtube-dl

使用

最简单的用法:

youtube-dl https://www.youtube.com/watch?v=kcMaxo0OaZo&index=3&list=PLC6RoM2FxOWMvFnGZ4a15MKa-iSaadkdp&t=0s

指定音频视频的质量

如果要指定视频的解析度,比如选择最好的:

youtube-dl -f best https://www.youtube.com/watch?v=kcMaxo0OaZo&index=3&list=PLC6RoM2FxOWMvFnGZ4a15MKa-iSaadkdp&t=0s

如果想自定义下载最好的音视频, 那么, 先列出所有的格式再选择:

youtube-dl -F https://www.youtube.com/watch?v=M2NIfqE4H1o

youtube-dl -F https://www.youtube.com/watch?v=M2NIfqE4H1o
[youtube] M2NIfqE4H1o: Downloading webpage
[youtube] M2NIfqE4H1o: Downloading video info webpage
[info] Available formats for M2NIfqE4H1o:
format code extension resolution note
249 webm audio only DASH audio 72k , opus @ 50k, 1.81MiB
250 webm audio only DASH audio 93k , opus @ 70k, 2.37MiB
171 webm audio only DASH audio 110k , vorbis@128k, 2.96MiB
140 m4a audio only DASH audio 130k , m4a_dash container, mp4a.40.2@128k, 3.61MiB
251 webm audio only DASH audio 172k , opus @160k, 4.46MiB
160 mp4 256x144 144p 84k , avc1.4d400c, 24fps, video only, 1.46MiB
278 webm 256x144 144p 96k , webm container, vp9, 24fps, video only, 2.46MiB
133 mp4 426x240 240p 152k , avc1.4d4015, 24fps, video only, 2.69MiB
242 webm 426x240 240p 206k , vp9, 24fps, video only, 4.18MiB
134 mp4 640x360 360p 356k , avc1.4d401e, 24fps, video only, 6.74MiB
243 webm 640x360 360p 389k , vp9, 24fps, video only, 7.85MiB
135 mp4 854x480 480p 679k , avc1.4d401e, 24fps, video only, 13.21MiB
244 webm 854x480 480p 683k , vp9, 24fps, video only, 13.45MiB
136 mp4 1280x720 720p 1225k , avc1.4d401f, 24fps, video only, 24.05MiB
247 webm 1280x720 720p 1328k , vp9, 24fps, video only, 27.04MiB
137 mp4 1920x1080 1080p 2159k , avc1.640028, 24fps, video only, 42.73MiB
248 webm 1920x1080 1080p 2445k , vp9, 24fps, video only, 49.17MiB
271 webm 2560x1440 1440p 6899k , vp9, 24fps, video only, 117.11MiB
17 3gp 176x144 small 78k , mp4v.20.3, mp4a.40.2@ 24k (22050Hz), 2.19MiB
36 3gp 320x180 small 216k , mp4v.20.3, mp4a.40.2 (22050Hz), 6.02MiB
18 mp4 640x360 medium 525k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 14.64MiB (best)

接着只要指定 format code 即可.

youtube-dl -f 137+140 --merge-output-format mp4 https://www.youtube.com/watch?v=M2NIfqE4H1o

常用的组合:

组合:
137+140

音频
140 m4a audio only DASH audio 128k , m4a_dash container, mp4a.40.2@128k

视频
134 mp4 640x360 360p
135 mp4 854x480 480p
137

由于上面是 video only + audio 两个文件 , 我们需要合并 video 和 audio

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' --merge-output-format mp4 'http://www.youtube.com/watch?v=M2NIfqE4H1o'
[youtube] M2NIfqE4H1o: Downloading webpage
[youtube] M2NIfqE4H1o: Downloading video info webpage
WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
[download] Destination: My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f137.mp4
[download] 100% of 42.73MiB in 02:07
[download] Destination: My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f140.m4a
[download] 100% of 3.61MiB in 00:10

上面提示 ffmpeg or avconv are not installed, 因此需要安装 ffmpeg.
安装完成后, 加上 --merge-output-format mp4 参数(其实不加也可以,会自动合并的), 重新执行

youtube-dl -f 160+140 --merge-output-format mp4 https://www.youtube.com/watch?v=M2NIfqE4H1o
[youtube] M2NIfqE4H1o: Downloading webpage
[youtube] M2NIfqE4H1o: Downloading video info webpage
[download] My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f160.mp4 has already been downloaded
[download] 100% of 1.46MiB
[download] My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f140.m4a has already been downloaded
[download] 100% of 3.61MiB
[ffmpeg] Merging formats into "My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.mp4"
Deleting original file My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f160.mp4 (pass -k to keep)
Deleting original file My Funny Valentine (Richard Rodgers) - Alexandra Whittingham-M2NIfqE4H1o.f140.m4a (pass -k to keep)

其他 -f 格式参数:

# 常用
# 下载质量最好的 mp4 格式, 如果没有则下载其他格式质量最好的
'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'


# Download best format available but not better that 480p
# 下载质量最好的格式, 但不要超过 480p
'bestvideo[height<=480]+bestaudio/best[height<=480]'


# 最好mp4
youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4' url

test niko 常用:

135+140, mp4 文件(480p):

'bestvideo[height<=480][ext=mp4]+bestaudio/best[height<=480]'

480p 格式大小的(手机常用): (不太好用,输出是.mkv文件)

'bestvideo[height<=480][ext=mp4]+bestaudio/best[height<=480]'

更多的功能可参考下面的文档。

选择字幕

Subtitle Options:
--write-sub Write subtitle file
--write-auto-sub Write automatically generated subtitle file
(YouTube only)
--all-subs Download all the available subtitles of the
video
--list-subs List all available subtitles for the video
--sub-format FORMAT Subtitle format, accepts formats
preference, for example: "srt" or
"ass/srt/best"
--sub-lang LANGS Languages of the subtitles to download
(optional) separated by commas, use --list-
subs for available language tags

--write-sub 这个貌似是把字幕内容写到一个字幕文件里(vtt或者srt等等)

查询所有的:

youtube-dl --list-subs https://www.youtube.com/watch\?v\=rqoxYKtEWEc                
[youtube] rqoxYKtEWEc: Downloading webpage
[youtube] rqoxYKtEWEc: Looking for automatic captions
Available automatic captions for rqoxYKtEWEc:
...
zh-Hans vtt, ttml, srv3, srv2, srv1
zh-Hant vtt, ttml, srv3, srv2, srv1
en vtt, ttml, srv3, srv2, srv1

Available subtitles for rqoxYKtEWEc:
Language formats
zh-Hans vtt, ttml, srv3, srv2, srv1
en vtt, ttml, srv3, srv2, srv1
fr vtt, ttml, srv3, srv2, srv1
de vtt, ttml, srv3, srv2, srv1
iw vtt, ttml, srv3, srv2, srv1
ja vtt, ttml, srv3, srv2, srv1
pt-BR vtt, ttml, srv3, srv2, srv1
es-419 vtt, ttml, srv3, srv2, srv1

下载视频

貌似没有办法嵌入字幕到视频文件里
--write-sub 是写到独立字幕文件.
--sub-format srt 指定字幕文件的格式.

youtube-dl --write-sub --sub-format srt --sub-lang zh-Hans -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' "https://www.youtube.com/watch?v=rqoxYKtEWEc"

多字幕:(逗号分割)
youtube-dl --write-sub --sub-lang zh-Hans,en -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' "https://www.youtube.com/watch?v=rqoxYKtEWEc"

niko collect

下载最低质量的:
youtube-dl -f worst url

参考


https://github.com/rg3/youtube-dl/blob/master/README.md#readme
command line - How to select video quality from youtube-dl? - Ask Ubuntu