工具类 - 音视频

TOC

音视频下载工具

yt-dlp 是什么

yt-dlp 网址

yt-dlp is a feature-rich command-line audio/video downloader with support for thousands of sites.

简而言之就是下载音视频的命令行工具。

安装yt-dlp

使用homebrew安装

brew install yt-dlp

基本用法

yt-dlp [OPTIONS] URL [URL...]

1. 视频下载 - 基础

yt-dlp <视频URL>

eg:

xdl@MacBook-Air ~ % yt-dlp "https://www.youtube.com/watch?v=baZ5dM_tXI4"
[youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
[youtube] baZ5dM_tXI4: Downloading webpage
[youtube] baZ5dM_tXI4: Downloading tv client config
[youtube] baZ5dM_tXI4: Downloading player 69f581a5
[youtube] baZ5dM_tXI4: Downloading tv player API JSON
[youtube] baZ5dM_tXI4: Downloading ios player API JSON
[youtube] baZ5dM_tXI4: Downloading m3u8 information
[info] baZ5dM_tXI4: Downloading 1 format(s): 299+251
[download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f299.mp4
[download] 100% of   10.00MiB in 00:00:00 at 44.98MiB/s
[download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f251.webm
[download] 100% of    4.27MiB in 00:00:00 at 6.84MiB/s
[Merger] Merging formats into "周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].mkv"
Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f299.mp4 (pass -k to keep)
Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f251.webm (pass -k to keep)

这样就在当前所在文件夹下载好了这个视频,并且因为我已经brew install ffmpeg这个工具,所以下载好的音视频直接就进行了合并,这样这个视频就包含了音频在内。

我们来看看我输入命令之后的各项输出结果:

  • 获取 URL 地址;
  • 下载各项信息;
  • 下载格式:299+251
  • 开始下载:
    • f299.mp4 –> 视频
    • f251.webm –> 音频
  • 合并音视频 –> .mkv
  • 删除原始文件:
    • f299.mp4 –> 视频
    • f251.webm –> 音频

上边有几点需要说明下:

  • yt-dlp下载的音视频是分开的,我们看到一个格式:299+251,这里涉及音视频的格式,我们通过-F来查看。

    xdl@MacBook-Air ~ % yt-dlp -F "https://www.youtube.com/watch?v=baZ5dM_tXI4"
    [youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
    [youtube] baZ5dM_tXI4: Downloading webpage
    [youtube] baZ5dM_tXI4: Downloading tv client config
    [youtube] baZ5dM_tXI4: Downloading player 69f581a5
    [youtube] baZ5dM_tXI4: Downloading tv player API JSON
    [youtube] baZ5dM_tXI4: Downloading ios player API JSON
    [youtube] baZ5dM_tXI4: Downloading m3u8 information
    [info] Available formats for baZ5dM_tXI4:
    ID  EXT   RESOLUTION FPS CH │   FILESIZE  TBR PROTO │ VCODEC         VBR ACODEC      ABR ASR MORE INFO
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────
    sb3 mhtml 48x27        0    │                 mhtml │ images                                 storyboard
    sb2 mhtml 80x45        1    │                 mhtml │ images                                 storyboard
    sb1 mhtml 160x90       1    │                 mhtml │ images                                 storyboard
    sb0 mhtml 320x180      1    │                 mhtml │ images                                 storyboard
    233 mp4   audio only        │                 m3u8  │ audio only         unknown             Default
    234 mp4   audio only        │                 m3u8  │ audio only         unknown             Default
    140 m4a   audio only      2 │    4.10MiB 129k https │ audio only         mp4a.40.2  129k 44k medium, m4a_dash
    251 webm  audio only      2 │    4.27MiB 135k https │ audio only         opus       135k 48k medium, webm_dash
    269 mp4   256x144     30    │ ~  2.62MiB  83k m3u8  │ avc1.4D400C    83k video only
    160 mp4   256x144     30    │  526.46KiB  16k https │ avc1.4d400c    16k video only          144p, mp4_dash
    230 mp4   640x360     30    │ ~  7.58MiB 239k m3u8  │ avc1.4D401E   239k video only
    134 mp4   640x360     30    │    1.61MiB  51k https │ avc1.4d401e    51k video only          360p, mp4_dash
    18  mp4   640x360     30  2 │    3.11MiB  98k https │ avc1.42001E        mp4a.40.2       22k 360p
    605 mp4   640x360     30    │ ~  6.72MiB 212k m3u8  │ vp09.00.21.08 212k video only
    243 webm  640x360     30    │    1.24MiB  39k https │ vp9            39k video only          360p, webm_dash
    136 mp4   1280x720    30    │    5.34MiB 169k https │ avc1.64001f   169k video only          720p, mp4_dash
    311 mp4   1280x720    60    │ ~ 15.80MiB 498k m3u8  │ avc1.640020   498k video only
    298 mp4   1280x720    60    │    5.10MiB 161k https │ avc1.640020   161k video only          720p60, mp4_dash
    312 mp4   1920x1080   60    │ ~ 24.79MiB 782k m3u8  │ avc1.64002A   782k video only
    299 mp4   1920x1080   60    │   10.00MiB 316k https │ avc1.64002a   316k video only          1080p60, mp4_dash
    

    左边第一栏时数字ID,第二列是格式类型,第三列是分辨率,后边是其他信息。

    我们完全可以根据自己的需要进行选择,比如我们可以搭配:

    • 音频是m4a,对应的ID是140;
    • 视频我们选一个体积较小的mp4格式的,1920x1080,对应的数字ID就是312;

    那么最后我们选择格式就是:140+312

    xdl@MacBook-Air ~ % yt-dlp -f 140+312 "https://www.youtube.com/watch?v=baZ5dM_tXI4"
    [youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
    [youtube] baZ5dM_tXI4: Downloading webpage
    [youtube] baZ5dM_tXI4: Downloading tv client config
    [youtube] baZ5dM_tXI4: Downloading player 69f581a5
    [youtube] baZ5dM_tXI4: Downloading tv player API JSON
    [youtube] baZ5dM_tXI4: Downloading ios player API JSON
    [youtube] baZ5dM_tXI4: Downloading m3u8 information
    [info] baZ5dM_tXI4: Downloading 1 format(s): 140+312
    [download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f140.m4a
    [download] 100% of    4.10MiB in 00:00:02 at 1.94MiB/s
    [hlsnative] Downloading m3u8 manifest
    [hlsnative] Total fragments: 52
    [download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f312.mp4
    [download] 100% of   11.80MiB in 00:00:01 at 6.09MiB/s
    [Merger] Merging formats into "周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].mp4"
    Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f140.m4a (pass -k to keep)
    Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f312.mp4 (pass -k to keep)
    

    你当然也可以选择只下载某个格式的音频,或者视频,比如你只要m4a格式的音频,那么就是:

    xdl@MacBook-Air ~ % yt-dlp -f 140 "https://www.youtube.com/watch?v=baZ5dM_tXI4"
    [youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
    [youtube] baZ5dM_tXI4: Downloading webpage
    [youtube] baZ5dM_tXI4: Downloading tv client config
    [youtube] baZ5dM_tXI4: Downloading player 69f581a5
    [youtube] baZ5dM_tXI4: Downloading tv player API JSON
    [youtube] baZ5dM_tXI4: Downloading ios player API JSON
    [youtube] baZ5dM_tXI4: Downloading m3u8 information
    [info] baZ5dM_tXI4: Downloading 1 format(s): 140
    [download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].m4a
    [download] 100% of    4.10MiB in 00:00:01 at 3.93MiB/s
    [FixupM4a] Correcting container of "周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].m4a"
    
  • 所以我们知道如果不跟参数,那么youtube就会默认给你下载质量最好的音视频。

2. 视频下载 - 查询格式

我们整理一下上边说的格式查询的语法:

youtube -F <视频URL>
-F | --list-formats:
List available formats of each video.

3. 视频下载 - 自定义格式下载

官网链接

通过-F知道了有哪些格式可供下载,下一步就可以用-f进行搭配。

youtube -f FORMAT <视频URL>
-f | --format:
Video format code. See “FORMAT SELECTION” for more details.

navigate to format examples .

4. 音频下载与转换

官网链接

通过上边说的-f FORMAT我们选择自己需要的音频进行下载,也可以通过-x来提取音频。

youtube -x <视频URL>
-x, --extract-audio:
Convert video files to audio-only files (requires ffmpeg and ffprobe).

我们也可以跟上格式转换参数来转换音频格式,比如:

xdl@MacBook-Air ~ % yt-dlp -x --audio-format mp3 "https://www.youtube.com/watch?v=baZ5dM_tXI4"
[youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
[youtube] baZ5dM_tXI4: Downloading webpage
[youtube] baZ5dM_tXI4: Downloading tv client config
[youtube] baZ5dM_tXI4: Downloading player 69f581a5
[youtube] baZ5dM_tXI4: Downloading tv player API JSON
[youtube] baZ5dM_tXI4: Downloading ios player API JSON
[youtube] baZ5dM_tXI4: Downloading m3u8 information
[info] baZ5dM_tXI4: Downloading 1 format(s): 251
[download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].webm
[download] 100% of    4.27MiB in 00:00:00 at 13.46MiB/s
[ExtractAudio] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].mp3
Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].webm (pass -k to keep)

上边这个例子就是下载最好的音频格式,并且将最终的格式转换成我要的mp3格式。

当然也可以跟上质量参数。

--audio-quality QUALITY:
specify ffmpeg audio quality to use when converting the audio with -x. Insert a value between 0 (best) and 10 (worst) for VBR or a specific bitrate like 128K (default 5)

5. 播放列表相关

yt-dlp <播放列表URL>

eg:

yt-dlp "https://www.youtube.com/playlist?list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"

当然你可以跟上各种控制参数,比如格式啊之类的。

我再举个例子,注意这个例子的URL链接和上一个是不同的,上一个是纯播放列表。

yt-dlp --yes-playlist "https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"

这个链接指向了一个播放列表中的一个具体的视频,跟上--yes-playlist表示我还是要全部列表中的视频。

yt-dlp --no-playlist "https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"

同样,但是跟上--no-playlist表示我只要这个视频,不要列表中的其他视频,即使链接可以指向播放列表。

选择播放列表中的某几个:

-I, --playlist-items ITEM_SPEC:
Comma separated playlist_index of the items to download. You can specify a range using “[START]:[STOP][:STEP]”. For backward compatibility, START-STOP is also supported. Use negative indices to count from the right and negative STEP to download in reverse order. E.g. “-I 1:3,7,-5::2” used on a playlist of size 15 will download the items at index 1,2,3,7,11,13,15
xdl@MacBook-Air ~ % yt-dlp -x -I 4:6,12 "https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"
[youtube:tab] Extracting URL: https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt
[youtube:tab] Downloading playlist PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt - add --no-playlist to download just the video 7wHZ9IldjR8
[youtube:tab] PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt: Downloading webpage
[youtube:tab] Extracting URL: https://www.youtube.com/playlist?list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt
[youtube:tab] PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt: Downloading webpage
[youtube:tab] PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt: Redownloading playlist API JSON with unavailable videos
[download] Downloading playlist: 歌曲合集|周深
[youtube:tab] Playlist 歌曲合集|周深: Downloading 4 items of 16
[download] Downloading item 1 of 4
[youtube] Extracting URL: https://www.youtube.com/watch?v=-Kv4rwsPZgw
[youtube] -Kv4rwsPZgw: Downloading webpage
[youtube] -Kv4rwsPZgw: Downloading tv client config
[youtube] -Kv4rwsPZgw: Downloading player 69f581a5
[youtube] -Kv4rwsPZgw: Downloading tv player API JSON
[youtube] -Kv4rwsPZgw: Downloading ios player API JSON
[youtube] -Kv4rwsPZgw: Downloading m3u8 information
[info] -Kv4rwsPZgw: Downloading 1 format(s): 251
[download] Destination: 周深 - 达拉崩吧|歌词|无损音质 [-Kv4rwsPZgw].webm
[download] 100% of    3.64MiB in 00:00:02 at 1.80MiB/s
[ExtractAudio] Destination: 周深 - 达拉崩吧|歌词|无损音质 [-Kv4rwsPZgw].opus
Deleting original file 周深 - 达拉崩吧|歌词|无损音质 [-Kv4rwsPZgw].webm (pass -k to keep)
[download] Downloading item 2 of 4
[youtube] Extracting URL: https://www.youtube.com/watch?v=kldddF9_qYA
[youtube] kldddF9_qYA: Downloading webpage
[youtube] kldddF9_qYA: Downloading tv client config
[youtube] kldddF9_qYA: Downloading tv player API JSON
[youtube] kldddF9_qYA: Downloading ios player API JSON
[youtube] kldddF9_qYA: Downloading m3u8 information
[info] kldddF9_qYA: Downloading 1 format(s): 251
[download] Destination: 周深 - 璀璨冒险人|歌词|无损音质 [kldddF9_qYA].webm
[download] 100% of    4.35MiB in 00:00:00 at 20.27MiB/s
[ExtractAudio] Destination: 周深 - 璀璨冒险人|歌词|无损音质 [kldddF9_qYA].opus
Deleting original file 周深 - 璀璨冒险人|歌词|无损音质 [kldddF9_qYA].webm (pass -k to keep)
[download] Downloading item 3 of 4
[youtube] Extracting URL: https://www.youtube.com/watch?v=FxNn9W5ZOC0
[youtube] FxNn9W5ZOC0: Downloading webpage
[youtube] FxNn9W5ZOC0: Downloading tv client config
[youtube] FxNn9W5ZOC0: Downloading tv player API JSON
[youtube] FxNn9W5ZOC0: Downloading ios player API JSON
[youtube] FxNn9W5ZOC0: Downloading m3u8 information
[info] FxNn9W5ZOC0: Downloading 1 format(s): 251
[download] Destination: 周深 - 花开忘忧|歌词|无损音质 [FxNn9W5ZOC0].webm
[download] 100% of    4.12MiB in 00:00:00 at 14.04MiB/s
[ExtractAudio] Destination: 周深 - 花开忘忧|歌词|无损音质 [FxNn9W5ZOC0].opus
Deleting original file 周深 - 花开忘忧|歌词|无损音质 [FxNn9W5ZOC0].webm (pass -k to keep)
[download] Downloading item 4 of 4
[youtube] Extracting URL: https://www.youtube.com/watch?v=ob3sjSL9uKE
[youtube] ob3sjSL9uKE: Downloading webpage
[youtube] ob3sjSL9uKE: Downloading tv client config
[youtube] ob3sjSL9uKE: Downloading tv player API JSON
[youtube] ob3sjSL9uKE: Downloading ios player API JSON
[youtube] ob3sjSL9uKE: Downloading m3u8 information
[info] ob3sjSL9uKE: Downloading 1 format(s): 251
[download] Destination: 周深 - 敬时光|歌词|无损音质 [ob3sjSL9uKE].webm
[download] 100% of    4.83MiB in 00:00:02 at 1.65MiB/s
[ExtractAudio] Destination: 周深 - 敬时光|歌词|无损音质 [ob3sjSL9uKE].opus
Deleting original file 周深 - 敬时光|歌词|无损音质 [ob3sjSL9uKE].webm (pass -k to keep)
[download] Finished downloading playlist: 歌曲合集|周深

上边这个,我就是想下载第4到第6个,还有第12个,一共这4个音频,我检查了,是完全正确的。

6. 字幕

官网链接

--write-subs                    Write subtitle file
--no-write-subs                 Do not write subtitle file (default)
--write-auto-subs               Write automatically generated subtitle file
                                (Alias: --write-automatic-subs)
--no-write-auto-subs            Do not write auto-generated subtitles
                                (default) (Alias: --no-write-automatic-subs)
--list-subs                     List available subtitles of each video.
                                Simulate unless --no-simulate is used
--sub-format FORMAT             Subtitle format; accepts formats preference
                                separated by "/", e.g. "srt" or "ass/srt/best"
--sub-langs LANGS               Languages of the subtitles to download (can
                                be regex) or "all" separated by commas, e.g.
                                --sub-langs "en.*,ja" (where "en.*" is a
                                regex pattern that matches "en" followed by
                                0 or more of any character). You can prefix
                                the language code with a "-" to exclude it
                                from the requested languages, e.g. --sub-
                                langs all,-live_chat. Use --list-subs for a
                                list of available language tags

eg:

xdl@MacBook-Air ~ % yt-dlp --no-playlist --list-subs "https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"
[youtube:tab] Extracting URL: https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt
[youtube:tab] Downloading just the video 7wHZ9IldjR8 because of --no-playlist
[youtube] Extracting URL: https://www.youtube.com/watch?v=7wHZ9IldjR8
[youtube] 7wHZ9IldjR8: Downloading webpage
[youtube] 7wHZ9IldjR8: Downloading tv client config
[youtube] 7wHZ9IldjR8: Downloading player 69f581a5
[youtube] 7wHZ9IldjR8: Downloading tv player API JSON
[youtube] 7wHZ9IldjR8: Downloading ios player API JSON
[youtube] 7wHZ9IldjR8: Downloading m3u8 information
[info] Available automatic captions for 7wHZ9IldjR8:
Language        Name                                             Formats
zh-HK                                                            vtt
zh-Hans                                                          vtt
zh-Hant                                                          vtt
zh-TW                                                            vtt
ab-zh-HK        Abkhazian from Chinese (Hong Kong)               vtt, ttml, srv3, srv2, srv1, json3
... # 这里太多自动翻译的内容了,我给省略掉
[info] Available subtitles for 7wHZ9IldjR8:
Language Name                  Formats
zh-HK    Chinese (Hong Kong)   vtt, ttml, srv3, srv2, srv1, json3
zh-Hans  Chinese (Simplified)  vtt, ttml, srv3, srv2, srv1, json3
zh-Hant  Chinese (Traditional) vtt, ttml, srv3, srv2, srv1, json3
zh-TW    Chinese (Taiwan)      vtt, ttml, srv3, srv2, srv1, json3

这里,我就是查看了这个视频有哪些字幕可供下载,所以现在我就可以根据上边的信息来下载。

xdl@MacBook-Air ~ % yt-dlp --no-playlist --write-subs --sub-lang zh-HK --sub-format vtt "https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt"
[youtube:tab] Extracting URL: https://www.youtube.com/watch?v=7wHZ9IldjR8&list=PLKbGySt1xT03OW5tX38OPDElIUe6-XPIt
[youtube:tab] Downloading just the video 7wHZ9IldjR8 because of --no-playlist
[youtube] Extracting URL: https://www.youtube.com/watch?v=7wHZ9IldjR8
[youtube] 7wHZ9IldjR8: Downloading webpage
[youtube] 7wHZ9IldjR8: Downloading tv client config
[youtube] 7wHZ9IldjR8: Downloading player 69f581a5
[youtube] 7wHZ9IldjR8: Downloading tv player API JSON
[youtube] 7wHZ9IldjR8: Downloading ios player API JSON
[youtube] 7wHZ9IldjR8: Downloading m3u8 information
[info] 7wHZ9IldjR8: Downloading subtitles: zh-HK
[info] 7wHZ9IldjR8: Downloading 1 format(s): 299+251
[info] Writing video subtitles to: 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].zh-HK.vtt
[download] Destination: 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].zh-HK.vtt
[download] 100% of    2.45KiB in 00:00:00 at 49.87KiB/s
[download] Destination: 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].f299.mp4
[download] 100% of    8.69MiB in 00:00:00 at 31.17MiB/s
[download] Destination: 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].f251.webm
[download] 100% of    3.29MiB in 00:00:00 at 14.47MiB/s
[Merger] Merging formats into "周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].mkv"
Deleting original file 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].f299.mp4 (pass -k to keep)
Deleting original file 周深 - 消散人潮|歌词|无损音质 [7wHZ9IldjR8].f251.webm (pass -k to keep)

可以看到:

  • 我首先声明我只要这个视频,不是要下载播放列表;
  • 其次,我注明我需要下载字幕 --write-subs;
  • 我选择了我要的字幕语言 zh-HK
  • 选择了我要的字幕格式 vtt

7. 缩略图

官网链接

--write-thumbnail               Write thumbnail image to disk
--no-write-thumbnail            Do not write thumbnail image to disk (default)
--write-all-thumbnails          Write all thumbnail image formats to disk
--list-thumbnails               List available thumbnails of each video. Simulate unless --no-simulate is used

eg:

xdl@MacBook-Air ~ % yt-dlp --write-thumbnail "https://www.youtube.com/watch?v=baZ5dM_tXI4" 
[youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
[youtube] baZ5dM_tXI4: Downloading webpage
[youtube] baZ5dM_tXI4: Downloading tv client config
[youtube] baZ5dM_tXI4: Downloading player 69f581a5
[youtube] baZ5dM_tXI4: Downloading tv player API JSON
[youtube] baZ5dM_tXI4: Downloading ios player API JSON
[youtube] baZ5dM_tXI4: Downloading m3u8 information
[info] baZ5dM_tXI4: Downloading 1 format(s): 299+251
Deleting existing file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].webp
[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].webp
[download] 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].mkv has already been downloaded

8. 批量下载

yt-dlp -a urls.txt

eg:

xdl@MacBook-Air ~ % ls
Applications	Documents	Library		Music		Public		my
Desktop		Downloads	Movies		Pictures	batch.txt	my music
xdl@MacBook-Air ~ % cat batch.txt
https://www.youtube.com/watch?v=baZ5dM_tXI4
https://www.youtube.com/watch?v=0eF1EOOah_I%  
xdl@MacBook-Air ~ % yt-dlp -a batch.txt
[youtube] Extracting URL: https://www.youtube.com/watch?v=baZ5dM_tXI4
[youtube] baZ5dM_tXI4: Downloading webpage
[youtube] baZ5dM_tXI4: Downloading tv client config
[youtube] baZ5dM_tXI4: Downloading player 69f581a5
[youtube] baZ5dM_tXI4: Downloading tv player API JSON
[youtube] baZ5dM_tXI4: Downloading ios player API JSON
[youtube] baZ5dM_tXI4: Downloading m3u8 information
[info] baZ5dM_tXI4: Downloading 1 format(s): 299+251
[download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f299.mp4
[download] 100% of   10.00MiB in 00:00:00 at 32.18MiB/s
[download] Destination: 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f251.webm
[download] 100% of    4.27MiB in 00:00:00 at 39.47MiB/s
[Merger] Merging formats into "周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].mkv"
Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f251.webm (pass -k to keep)
Deleting original file 周深 - 好风起|歌词|无损音质 [baZ5dM_tXI4].f299.mp4 (pass -k to keep)
[youtube] Extracting URL: https://www.youtube.com/watch?v=0eF1EOOah_I
[youtube] 0eF1EOOah_I: Downloading webpage
[youtube] 0eF1EOOah_I: Downloading tv client config
[youtube] 0eF1EOOah_I: Downloading tv player API JSON
[youtube] 0eF1EOOah_I: Downloading ios player API JSON
[youtube] 0eF1EOOah_I: Downloading m3u8 information
[info] 0eF1EOOah_I: Downloading 1 format(s): 299+251
[download] Destination: 闻人听書_ - 虞兮叹|歌词|无损音质 [0eF1EOOah_I].f299.mp4
[download] 100% of    6.96MiB in 00:00:00 at 7.34MiB/s
[download] Destination: 闻人听書_ - 虞兮叹|歌词|无损音质 [0eF1EOOah_I].f251.webm
[download] 100% of    3.34MiB in 00:00:00 at 7.79MiB/s
[Merger] Merging formats into "闻人听書_ - 虞兮叹|歌词|无损音质 [0eF1EOOah_I].mkv"
Deleting original file 闻人听書_ - 虞兮叹|歌词|无损音质 [0eF1EOOah_I].f251.webm (pass -k to keep)
Deleting original file 闻人听書_ - 虞兮叹|歌词|无损音质 [0eF1EOOah_I].f299.mp4 (pass -k to keep)

我这里随便选了两个视频的链接,放到了batch.txt这个文件里,每个链接一行。然后yt-dlp就下载好了这两个视频。

9. 其他

  1. 限制下载速度

    yt-dlp --limit-rate 1M <视频URL>
    

    比如你在同时看视频,下载视频本身也占用宽带,所以你可限制一下,不过我不怕,哈哈,我现在是2500M的网速。

  2. 查找帮助文档

xdl@MacBook-Air ~ % yt-dlp --help | grep -E -- "--*format"
                                  "-S=aext:{0},abr -x --audio-format {0}"
                                  "-S=aext:ARG0,abr -x --audio-format ARG0".
  --ignore-no-formats-error       Ignore "No video formats" error. Useful for
  --no-ignore-no-formats-error    Throw error when no downloadable video
  -f, --format FORMAT             Video format code, see "FORMAT SELECTION"
  -S, --format-sort SORTORDER     Sort the formats by the fields given, see
  --format-sort-force             Force user specified sort order to have
  --no-format-sort-force          Some fields have precedence over the user
  --prefer-free-formats           Prefer video formats with free containers
  --no-prefer-free-formats        Don't give any special preference to free
  --check-formats                 Make sure formats are selected only from
  --check-all-formats             Check all formats for whether they are
  --no-check-formats              Do not check that the formats are actually
  -F, --list-formats              List available formats of each video.
  --merge-output-format FORMAT    Containers that may be used when merging
  --sub-format FORMAT             Subtitle format; accepts formats preference
  --audio-format FORMAT           Format to convert the audio to when -x is
                                  "video" (after --format; before
                            

借助grep命令可以精确查找内容,避免太多其他内容出现。