编辑
2024-01-30
CS_杂项
0
请注意,本文编写于 304 天前,最后修改于 304 天前,其中某些信息可能已经过时。

目录

yt-dlp 使用向说明
安装
油管下载视频测试
视频下载参数
General Options:
Video Format Options:
Subtitle Options:

yt-dlp 使用向说明

需要在油管上下载一些视频,并且最好能够带有字幕,yt-dlp工具刚好就能满足我的需求。主要介绍安装以及一些基础的使用。其实 BiliBili 的视频也可以用该软件下载,替换成不同的链接即可。

安装

yt-dlp GitHub: https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#subtitle-options

在仓库首页 INSTALLATION 提供了不同平台的安装方式,如下所示。

image.png

对于 Windows,点击第一个蓝色按钮,下载.exe文件,并且将其放入在任意一个系统路径的文件夹中就行。

安装成功的话,进入cmd窗口,输入yt-dlp --version显示如下:

image.png

油管下载视频测试

以 OpenAI 的 A Survey of Techniques for Maximizing LLM Performance 视频为例: https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI

基础操作:yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI"

image.png

可以看到这样就能够直接下载视频了,但是也发现了这样太过于简单,甚至不能指定分辨率等。

进阶操作:

  • 首先查看当前视频支持的所有类型 yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI" -F

    image.png

    可以看到该视频有很多支持的格式,其中部分是audio onlyvideo only 以及没有特殊标识的即为audio+video文件。

  • 下载合并的文件ID:22 yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI" --format 22

    image.png

    可以看到该文件即是之前下载的文件。

  • 下载audio only文件ID:251 & video only文件ID:616

    往往更高分辨率的视频和音频是分开成两个文件的,于是我们下载两个文件,以期望或等更高的分辨率。

    yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI" --format 251,616

    image.png

    可以看到现在下载的两个文件audio only & video only,使用ffmpeg能够方便的将这两个文件进行合并。 ffmpeg -i [path to video] -i [path to audio] -c:v copy -c:a aac output.mp4

    在这里命令为:

    ffmpeg -i "A Survey of Techniques for Maximizing LLM Performance [ahnGLM-RC1Y].mp4" -i "A Survey of Techniques for Maximizing LLM Performance [ahnGLM-RC1Y].webm" -c:v copy -c:a aac output.mp4

    image.png

    最后生成的output.mp4就是合并后的文件了。

  • 下载字幕 油管上的字幕也是一个十分好的功能,尽管是机翻的。 查看当前视频支持的字幕语言yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI" --list-subs

    image.png

    可以看到有许多的字幕文件,现在我们需要下载简体中文的字幕,即红框处。

    yt-dlp "https://www.youtube.com/watch?v=ahnGLM-RC1Y&ab_channel=OpenAI" --write-auto-subs --sub-langs "zh-Hans"

    image.png

    这便是所有的下载文件了,我播放器使用的是PotPlayer能够非常方便的加入字幕文件,整个使用介绍就差不多over了🫠。

视频下载参数

在这里,我主要列出一些个人觉得比较常用的参数,完整的参数列表可以查看yt-dlp GitHub 仓库参数

命令格式:yt-dlp [OPTIONS] [--] URL [URL...]

General Options:

-h, --help Print this help text and exit --version Print program version and exit -U, --update Update this program to the latest version --no-update Do not check for updates (default) --update-to [CHANNEL]@[TAG] Upgrade/downgrade to a specific version. CHANNEL can be a repository as well. CHANNEL and TAG default to "stable" and "latest" respectively if omitted; See "UPDATE" for details. Supported channels: stable, nightly, master -i, --ignore-errors Ignore download and postprocessing errors. The download will be considered successful even if the postprocessing fails --no-abort-on-error Continue with next video on download errors; e.g. to skip unavailable videos in a playlist (default) --abort-on-error Abort downloading of further videos if an error occurs (Alias: --no-ignore-errors) --dump-user-agent Display the current user-agent and exit --list-extractors List all supported extractors and exit --extractor-descriptions Output descriptions of all supported extractors and exit --use-extractors NAMES Extractor names to use separated by commas. You can also use regexes, "all", "default" and "end" (end URL matching); e.g. --ies "holodex.*,end,youtube". Prefix the name with a "-" to exclude it, e.g. --ies default,-generic. Use --list-extractors for a list of extractor names. (Alias: --ies) --default-search PREFIX Use this prefix for unqualified URLs. E.g. "gvsearch2:python" downloads two videos from google videos for the search term "python". Use the value "auto" to let yt-dlp guess ("auto_warning" to emit a warning when guessing). "error" just throws an error. The default value "fixup_error" repairs broken URLs, but emits an error if this is not possible instead of searching --ignore-config Don't load any more configuration files except those given by --config-locations. For backward compatibility, if this option is found inside the system configuration file, the user configuration is not loaded. (Alias: --no-config) --no-config-locations Do not load any custom configuration files (default). When given inside a configuration file, ignore all previous --config-locations defined in the current file --config-locations PATH Location of the main configuration file; either the path to the config or its containing directory ("-" for stdin). Can be used multiple times and inside other configuration files --flat-playlist Do not extract the videos of a playlist, only list them --no-flat-playlist Fully extract the videos of a playlist (default) --live-from-start Download livestreams from the start. Currently only supported for YouTube (Experimental) --no-live-from-start Download livestreams from the current time (default) --wait-for-video MIN[-MAX] Wait for scheduled streams to become available. Pass the minimum number of seconds (or range) to wait between retries --no-wait-for-video Do not wait for scheduled streams (default) --mark-watched Mark videos watched (even with --simulate) --no-mark-watched Do not mark videos watched (default) --color [STREAM:]POLICY Whether to emit color codes in output, optionally prefixed by the STREAM (stdout or stderr) to apply the setting to. Can be one of "always", "auto" (default), "never", or "no_color" (use non color terminal sequences). Can be used multiple times --compat-options OPTS Options that can help keep compatibility with youtube-dl or youtube-dlc configurations by reverting some of the changes made in yt-dlp. See "Differences in default behavior" for details --alias ALIASES OPTIONS Create aliases for an option string. Unless an alias starts with a dash "-", it is prefixed with "--". Arguments are parsed according to the Python string formatting mini-language. E.g. --alias get-audio,-X "-S=aext:{0},abr -x --audio-format {0}" creates options "--get-audio" and "-X" that takes an argument (ARG0) and expands to "-S=aext:ARG0,abr -x --audio-format ARG0". All defined aliases are listed in the --help output. Alias options can trigger more aliases; so be careful to avoid defining recursive options. As a safety measure, each alias may be triggered a maximum of 100 times. This option can be used multiple times

Video Format Options:

-f, --format FORMAT Video format code, see "FORMAT SELECTION" for more details -S, --format-sort SORTORDER Sort the formats by the fields given, see "Sorting Formats" for more details --format-sort-force Force user specified sort order to have precedence over all fields, see "Sorting Formats" for more details (Alias: --S-force) --no-format-sort-force Some fields have precedence over the user specified sort order (default) --video-multistreams Allow multiple video streams to be merged into a single file --no-video-multistreams Only one video stream is downloaded for each output file (default) --audio-multistreams Allow multiple audio streams to be merged into a single file --no-audio-multistreams Only one audio stream is downloaded for each output file (default) --prefer-free-formats Prefer video formats with free containers over non-free ones of same quality. Use with "-S ext" to strictly prefer free containers irrespective of quality --no-prefer-free-formats Don't give any special preference to free containers (default) --check-formats Make sure formats are selected only from those that are actually downloadable --check-all-formats Check all formats for whether they are actually downloadable --no-check-formats Do not check that the formats are actually downloadable -F, --list-formats List available formats of each video. Simulate unless --no-simulate is used --merge-output-format FORMAT Containers that may be used when merging formats, separated by "/", e.g. "mp4/mkv". Ignored if no merge is required. (currently supported: avi, flv, mkv, mov, mp4, webm)

Subtitle Options:

--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, 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". 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

本文作者:Geaming

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!