下载视频/音频的python脚本,使用youtube dl构建
vidl的Python项目详细描述
视界
vidl是一个脚本,旨在使用youtube dl从任何人轻松下载视频/音频。它自动将缩略图嵌入到mp3/mp4/m4a文件中。
如果找到mp3文件,vidl将向其添加元数据。--no-md
选项关闭此选项。
title
、artist
和year
元数据被添加,但是如果url是播放列表,它也会添加album
、album artist
、track number
、track count
。
如果标题包含“-”,vidl通常将其前后的内容分别用作艺术家和标题。--dont-extract-md
选项关闭此行为。
安装
- 安装python(建议3.7)
- 安装ffmpeg and ffprobe
- 运行
pip install vidl
。如果不起作用,请尝试pip3
而不是pip
。 - 如果您不在macos或windows上,则需要指定vidl将文件下载到vidl配置文件中的位置。运行
vidl --config-path
查看配置文件的位置。 如果你在macos上,我建议setting up shortcuts for vidl
更新
通过运行pip install vidl --upgrade --upgrade-strategy eager
更新vidl。如果不起作用,请尝试pip3
而不是pip
。
如果vidl无法下载url,可能是因为youtube dl已经过时了。--upgrade-strategy eager
部分更新youtube dl。
卸载
运行pip uninstall vidl
。如果不起作用,请尝试pip3
而不是pip
。
要完全卸载vidl,请转到vidl的配置文件(运行vidl --config-path
查看它的位置)并删除它所在的文件夹。
用法
示例:
vidl https://www.youtube.com/watch?v=ta_ZVS7HkwI
- 以MP3格式下载视频,并添加它检测到的元数据。
vidl mp3 https://www.youtube.com/watch?v=ta_ZVS7HkwI --no-md
- 以MP3格式下载视频,无需添加元数据。
vidl
- 打印Vidl的帮助菜单,如下所示:
Download Usage: vidl [format] [options] <URL> Download Options: format mp3, mp4, wav or m4a. Default mp3. --no-md Don't add metadata to downloaded files. --no-smart-md Don't extract artist and song name from title. --no-dl Don't download anything. Usually used with -v -v, --verbose Display all logs. Global Options: --version Display vidl version. "vidl -v" also works. -h, --help Display this help message. --config-path Display the location of vidl's configuration file.
配置
vidl有一个配置文件,您可以通过运行vidl --config-path
找到其位置。在其中,您可以设置下载文件夹和文件名模板。
如果你搞砸了,可以删除文件,下次运行vidl时将重新创建默认文件。
自定义元数据分析
vidl有一个user_md_parser.py
文件。默认情况下,它什么也不做,但是您可以配置它来操作您下载的歌曲的元数据。在我的例子中,如果标题以“[ncs release]结尾,我将“comment”元数据设置为“ncs”。
相关文档可以在文件中找到。该文件与vidl的配置文件位于同一文件夹中,可以通过运行vidl --config-path
找到该文件。如果你搞砸了,可以删除文件,下次运行vidl时将重新创建默认文件。
Set up shortcuts for vidl (macOS)
You'll be able to select any piece of text, press your chosen shortcut and the link(s) in your selected text will be downloaded! A little tedious to set up, but well worth it.
First, we need to create a macOS Service:
Open the Automator app.
Choose File > New, and select Service.
(TLDR; Add ^{
}) In the window that just popped up, there are two columns on the left (if not, click the ^{ } button in the status bar). Select ^{ } in the first column, and in the second column, drag ^{ } into the main part of the window. Make your settings match these:
If you want the shortcut to only work in one app, select that app instead of ^{
}. In the text box in the "Run Shell Script" box, paste in the following script:
^{pr 2}$Replace ^{
} with the path you get from running ^{ } in the terminal. Replace ^{ } with the path you get from running ^{ } in the terminal. Replace ^{ } with the path you get from running ^{ } in the terminal. Choose File > Save. Type in vidl.
Almost done, you just need to tie a shortcut to the macOS Service you just created:
- Open the System Preferences app.
- Go to Keyboard and select the Shortcuts tab.
- Select Services from the left column, and locate vidl (should be under Internet). Add your preferred shortcut.
Dev Instructions
Installation
- Install Python. You may want to install it using pyenv为了管理python版本(如果poetry没有检测到正确的版本,可以使用pyenv修复它)。
- 安装ffmpeg and ffprobe
- 安装Poetry
- 运行
poetry install
以安装python包依赖项。 - 如果您不在macos或windows上,则需要指定vidl将文件下载到vidl配置文件中的位置。运行
vidl --config-path
查看配置文件的位置。
我建议运行poetry config settings.virtualenvs.in-project true
。此命令使poetry在项目文件夹中创建python虚拟环境,因此您可以轻松地删除它。此外,如果您在工作区(或全局)设置中将python.pythonPath
设置设置设置为${workspaceFolder}/.venv/bin/python
,它还允许vscode的python扩展检测虚拟环境。
运行
poetry run vidl
替代poetry run <command>
的方法是运行poetry shell
以进入虚拟环境的bash cli,然后自己运行命令(例如vidl
)。
待办事项
- 可选通知。告诉您下载何时开始、何时完成以及应用的元数据
- 对于将来的配置可能性,将
--no-md
替换为--md
和--!md
。也许叫它defaults
而不是config
?- 将所有配置添加为选项,例如添加下载文件夹选项。
- 将所有选项添加为configs,例如add md option。
- 为个人ME添加配置/选项塔达
- 允许直接传递youtube dl参数