进行下边操作之前,要按照使用youtube-dl下载国外视频中下载ffmpeg和配置环境变量。及下载youtube-dl。
https://yt-dl.org/
youtube-dl的开源地址: https://github.com/rg3/youtube-dl
可以通过该地址查看youtube-dl的用法。后续研发也可以用到。
查看视频所有类型,只看不下载
youtube-dl -F [url]
举例:
youtube-dl -F https://www.youtube.com/watch?v=AbvjWY6tMyg
运行之后:
>youtube-dl -F https://www.youtube.com/watch?v=AbvjWY6tMyg
[youtube] AbvjWY6tMyg: Downloading webpage
[youtube] AbvjWY6tMyg: Downloading video info webpage
[info] Available formats for AbvjWY6tMyg:
format code extension resolution note
249 webm audio only DASH audio 65k , opus @ 50k, 4.31MiB
250 webm audio only DASH audio 85k , opus @ 70k, 5.67MiB
140 m4a audio only DASH audio 128k , m4a_dash container, mp4a.4
0.2@128k, 9.99MiB
171 webm audio only DASH audio 146k , vorbis@128k, 10.04MiB
251 webm audio only DASH audio 159k , opus @160k, 11.07MiB
278 webm 256x144 144p 100k , webm container, vp9, 30fps, vide
o only, 7.06MiB
160 mp4 256x144 144p 116k , avc1.4d400c, 30fps, video only,
5.24MiB
242 webm 426x240 240p 237k , vp9, 30fps, video only, 15.04MiB
133 mp4 426x240 240p 255k , avc1.4d4015, 30fps, video only,
11.18MiB
243 webm 640x360 360p 532k , vp9, 30fps, video only, 28.06MiB
134 mp4 640x360 360p 737k , avc1.4d401e, 30fps, video only,
32.50MiB
244 webm 854x480 480p 944k , vp9, 30fps, video only, 51.11MiB
135 mp4 854x480 480p 1389k , avc1.4d401f, 30fps, video only,
66.91MiB
247 webm 1280x720 720p 1955k , vp9, 30fps, video only, 105.76Mi
B
136 mp4 1280x720 720p 2791k , avc1.4d401f, 30fps, video only,
134.21MiB
248 webm 1920x1080 1080p 3341k , vp9, 30fps, video only, 187.42M
iB
137 mp4 1920x1080 1080p 5020k , avc1.640028, 30fps, video only,
239.78MiB
271 webm 2560x1440 1440p 9026k , vp9, 30fps, video only, 579.91M
iB
313 webm 3840x2160 2160p 18262k , vp9, 30fps, video only, 1.24Gi
B
272 webm 7680x4320 4320p 25530k , vp9, 30fps, video only, 1.68Gi
B
17 3gp 176x144 small , mp4v.20.3, mp4a.40.2@ 24k, 6.00MiB
36 3gp 320x180 small , mp4v.20.3, mp4a.40.2, 17.72MiB
18 mp4 640x360 medium , avc1.42001E, mp4a.40.2@ 96k, 54.60Mi
B
43 webm 640x360 medium , vp8.0, vorbis@128k, 63.72MiB
22 mp4 1280x720 hd720 , avc1.64001F, mp4a.40.2@192k (best)
出现audio only的只有音频。
出现video only的只有视频。
否则音视频都有。
下载指定文件
比如要下载8k的视频
通过上一步获取到:
272 webm 7680x4320 4320p 25530k , vp9, 30fps, video only, 1.68GiB
可见8k格式的index是272.
可以用以下命令:
youtube-dl -f 272 https://www.youtube.com/watch?v=AbvjWY6tMyg
运行结果:
[youtube] AbvjWY6tMyg: Downloading webpage
[youtube] AbvjWY6tMyg: Downloading video info webpage
[download] Destination: Amazing 8K Ultra-HD-AbvjWY6tMyg.webm
[download] 100% of 1.68GiB in 04:34
下载指定文件并合并
251 webm audio only DASH audio 159k , opus @160k, 11.07MiB
...
272 webm 7680x4320 4320p 25530k , vp9, 30fps, video only, 1.68GiB
如果想要以上的视频和音频合并,可以用一下命令。(前提已经下载了ffmpeg并配置了环境变量,过程参见https://www.jianshu.com/p/8817a7b0c8d6)
youtube-dl -f 272+251 https://www.youtube.com/watch?v=AbvjWY6tMyg
注意: 视频在前 音频在后 272是视频的编号 251是音频的编号。
运行结果:
[youtube] AbvjWY6tMyg: Downloading webpage
[youtube] AbvjWY6tMyg: Downloading video info webpage
[download] Destination: Amazing 8K Ultra-HD-AbvjWY6tMyg.f272.webm
[download] 100% of 1.68GiB in 03:37
[download] Destination: Amazing 8K Ultra-HD-AbvjWY6tMyg.f251.webm
[download] 100% of 11.07MiB in 00:02
[ffmpeg] Merging formats into "Amazing 8K Ultra-HD-AbvjWY6tMyg.webm"
Deleting original file Amazing 8K Ultra-HD-AbvjWY6tMyg.f272.webm (pass -k to keep)
Deleting original file Amazing 8K Ultra-HD-AbvjWY6tMyg.f251.webm (pass -k to keep)
可见下载完之后,原始的视频和音频文件被删除了。
如果想要保存单独的视频和音频文件,可以加上-k参数。
网友评论