Windows下的YouTube-dl与FFmpeg下载安装配置

YouTube-dl

下载地址

http://ytdl-org.github.io/youtube-dl/download.html

在这里插入图片描述
先点击下载并安装环境包 Microsoft Visual C++ 2010 Service Pack 1 Redistributable Package (x86)
然后点击下载 Windows.exe
下载完成之后,放到你指定的目录下
例如:
在这里插入图片描述
WIN+R输入cmd切换至youtube-dl文件目录下
输入以下命令

D:\Youtubedl>youtube-dl --version
2021.12.17

D:\Youtubedl>youtube-dl --help
Usage: youtube-dl [OPTIONS] URL [URL...]

Options:
  General Options:
    -h, --help                           Print this help text and exit
    --version                            Print program version and exit
    -U, --update                         Update this program to latest version. Make sure that you have sufficient permissions (run with
                                         sudo if needed)
    -i, --ignore-errors                  Continue on download errors, for example to skip unavailable videos in a playlist
    --abort-on-error                     Abort downloading of further videos (in the playlist or the command line) if an error occurs
    --dump-user-agent                    Display the current browser identification
    --list-extractors                    List all supported extractors
    --extractor-descriptions             Output descriptions of all supported extractors
    --force-generic-extractor            Force extraction to use the generic extractor
    --default-search PREFIX              Use this prefix for unqualified URLs. For example "gvsearch2:" downloads two videos from google
                                         videos for youtube-dl "large apple". Use the value "auto" to let youtube-dl 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                      Do not read configuration files. When given in the global configuration file /etc/youtube-dl.conf:
                                         Do not read the user configuration in ~/.config/youtube-dl/config (%APPDATA%/youtube-dl/config.txt
                                         on Windows)
    --config-location PATH               Location of the configuration file; either the path to the config or its containing directory.
    --flat-playlist                      Do not extract the videos of a playlist, only list them.
    --mark-watched                       Mark videos watched (YouTube only)
    --no-mark-watched                    Do not mark videos watched (YouTube only)
    --no-color                           Do not emit color codes in output

  Network Options:
    --proxy URL                          Use the specified HTTP/HTTPS/SOCKS proxy. To enable SOCKS proxy, specify a proper scheme. For
                                         example socks5://127.0.0.1:1080/. Pass in an empty string (--proxy "") for direct connection
    --socket-timeout SECONDS             Time to wait before giving up, in seconds
    --source-address IP                  Client-side IP address to bind to
    -4, --force-ipv4                     Make all connections via IPv4
    -6, --force-ipv6                     Make all connections via IPv6

  Geo Restriction:
    --geo-verification-proxy URL         Use this proxy to verify the IP address for some geo-restricted sites. The default proxy specified
                                         by --proxy (or none, if the option is not present) is used for the actual downloading.
    --geo-bypass                         Bypass geographic restriction via faking X-Forwarded-For HTTP header
    --no-geo-bypass                      Do not bypass geographic restriction via faking X-Forwarded-For HTTP header
    --geo-bypass-country CODE            Force bypass geographic restriction with explicitly provided two-letter ISO 3166-2 country code
    --geo-bypass-ip-block IP_BLOCK       Force bypass geographic restriction with explicitly provided IP block in CIDR notation

  Video Selection:
    --playlist-start NUMBER              Playlist video to start at (default is 1)
    --playlist-end NUMBER                Playlist video to end at (default is last)
    --playlist-items ITEM_SPEC           Playlist video items to download. Specify indices of the videos in the playlist separated by
                                         commas like: "--playlist-items 1,2,5,8" if you want to download videos indexed 1, 2, 5, 8 in the
                                         playlist. You can specify range: "--playlist-items 1-3,7,10-13", it will download the videos at
                                         index 1, 2, 3, 7, 10, 11, 12 and 13.
    --match-title REGEX                  Download only matching titles (regex or caseless sub-string)
    --reject-title REGEX                 Skip download for matching titles (regex or caseless sub-string)
    --max-downloads NUMBER               Abort after downloading NUMBER files
    --min-filesize SIZE                  Do not download any videos smaller than SIZE (e.g. 50k or 44.6m)
    --max-filesize SIZE                  Do not download any videos larger than SIZE (e.g. 50k or 44.6m)
    --date DATE                          Download only videos uploaded in this date
    --datebefore DATE                    Download only videos uploaded on or before this date (i.e. inclusive)
    --dateafter DATE                     Download only videos uploaded on or after this date (i.e. inclusive)
    --min-views COUNT                    Do not download any videos with less than COUNT views
    --max-views COUNT                    Do not download any videos with more than COUNT views
    --match-filter FILTER                Generic video filter. Specify any key (see the "OUTPUT TEMPLATE" for a list of available keys) to
                                         match if the key is present, !key to check if the key is not present, key > NUMBER (like
                                         "comment_count > 12", also works with >=, <, <=, !=, =) to compare against a number, key =
                                         'LITERAL' (like "uploader = 'Mike Smith'", also works with !=) to match against a string literal
                                         and & to require multiple matches. Values which are not known are excluded unless you put a
                                         question mark (?) after the operator. For example, to only match videos that have been liked more
                                         than 100 times and disliked less than 50 times (or the dislike functionality is not available at
                                         the given service), but who also have a description, us
### 如何使用 yt-dlp 下载 YouTube 视频 #### 准备工作 为了顺利运行 `yt-dlp`,需确认已安装 Python 版本不低于 3.7,并建议一并部署 FFmpeg 和 ffprobe 工具以便于后期处理视频文件[^2]。 #### 安装 yt-dlp 可以通过 pip 命令轻松安装最新版本的 `yt-dlp`: ```bash pip install yt-dlp ``` #### 编写下载脚本 下面提供了一个简单的 Python 脚本来展示如何利用 `yt-dlp` 来获取指定 URL 的 YouTube 视频。此代码片段定义了名为 `download_video()` 的函数,接受两个参数——目标链接 (`url`) 及保存路径模式 (`output_path`);默认情况下会依据最佳质量自动选择音频流或视频流进行下载[^4]。 ```python import yt_dlp def download_video(url, output_path='%(title)s.%(ext)s'): ydl_opts = { 'format': 'bestvideo+bestaudio/best', 'outtmpl': output_path, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) # 使用示例 video_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' download_video(video_url) ``` 上述例子中设置了 `'format': 'bestvideo+bestaudio/best'` 参数组合,旨在优先选取最高画质独立的最佳音轨相结合的方式完成下载任务,如果无法满足条件则退而求其次采用单一最优格式。 #### 实现带进度条的功能 对于希望实时跟踪下载状态的应用场景而言,在原有基础上稍作修改即可引入进度显示机制。通过向选项字典内加入回调钩子 (progress_hooks),每当有新的进展发生时都会触发相应的事件处理器来刷新 UI 上的状态指示器[^3]。 ```python from tqdm import tqdm class MyProgressBar(tqdm): def update_to(self, b=1, bsize=1, tsize=None): if tsize is not None: self.total = tsize self.update(b * bsize - self.n) def my_hook(d): if d['status'] == 'downloading': filename = d.get('_filename') total_bytes = int(d.get('total_bytes_estimate')) downloaded_bytes = int(d.get('downloaded_bytes')) pbar = MyProgressBar(total=total_bytes, unit='B', unit_scale=True, desc=f'Downloading {filename}') pbar.update(downloaded_bytes) ydl_opts = { 'format': 'bestvideo+bestaudio/best', 'outtmpl': '%(title)s.%(ext)s', 'progress_hooks': [my_hook], } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download(['https://www.youtube.com/watch?v=dQw4w9WgXcQ']) ``` 这段增强后的代码实现了更加友好的用户体验,允许用户直观地看到当前操作的具体情况和发展趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值