我知道spotDL是一个用python构建的命令行应用程序,但是有什么方法可以在脚本中使用它吗?
你可以像这样使用 os.popen :
import os
os.popen("spotdl https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M")
或操作系统:
os.system("spotdl https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M")
您会看到,spotDL 是一个命令行应用程序,需要使用命令行参数。在 python 中,没有办法(据我所知)来设置这些参数。
我相信您问的是如何使用导入并直接使用诸如
spotdl.download(spotifylink)
之类的功能,但是,该应用程序仅设计用于命令行使用。那么我们就这么做吧!
好吧,我们可以使用
subprocess
模块通过 python 可执行文件启动 Spotdl。
import subprocess
import sys # for sys.executable (The file path of the currently using python)
from spotdl import __main__ as spotdl # To get the location of spotdl
spotifylink = "Whatever you want to download"
subprocess.check_call([sys.executable, spotdl.__file__, spotifylink])
编辑:有更好的方法来做到这一点(需要pytube)
from spotdl import __main__ as start # To initialize
from spotdl.search.songObj import SongObj
from pytube import YouTube
spotifylink = "Whatever you want to download"
song = SongObj.from_url(spotifylink)
url = song.get_youtube_link() # Yay you have a youtube link!
yt = YouTube(url)
yts = yt.steams.get_audio_only()
fname = yts.download()
print(f"Downloaded to {fname}")
实际上 spotDL 有一个 API,这里是 DOCS。
这是一个基本示例,说明您可以根据给定的示例之一做什么。
from spotdl.command_line.core import Spotdl
args = {
"no_encode": True,
}
with Spotdl(args) as spotdl_handler:
spotdl_handler.download_track("https://open.spotify.com/track/2lfPecqFbH8X4lHSpTxt8l")
print("Downloading 2nd track.")
spotdl_handler.download_track("ncs spectre")
print("Downloading from file.")
spotdl_handler.download_tracks_from_file("file_full_of_tracks.txt")
示例和链接适用于 spotDL 的 v2,使用 v3(与 PyPI 安装的常见版本)将不包含这些功能。
您可以从这里下载v2。
导入spotdl 从 Spotdl 导入 Spotdl
obj = Spotdl(client_id="-", client_secret="", no_cache=True) Song_objs = obj.search(["歌曲链接"]) 打印(歌曲_objs) obj.download_songs(song_objs)