【发布时间】:2021-10-30 20:53:34
【问题描述】:
我正在尝试将整个 Youtube 播放列表下载为 MP3,下面的这段代码给了我一个TypeError: 'DeferredGeneratorList' object is not callable,我尝试用谷歌搜索它,我根本没有发现类似的错误,我做错了什么吗?和/或,还有其他方法可以实现吗?
代码:
import os
import subprocess
from pytube import Playlist, YouTube
def run(pl):
# get parent directory; VERY IMPORTANT!!
# INCLUDE LAST SLASH AFTER FOLDER NAME
# e.g. /home/username/Folder/ or C:\Users\Username\Folder\
filepath = input("Please enter the filepath of the directory where this script is located:\n")
# get linked list of links in the playlist
links = pl.video_urls()
# download each item in the list
for l in links:
# converts the link to a YouTube object
yt = YouTube(l)
# takes first stream; since ffmpeg will convert to mp3 anyway
music = yt.streams.first()
# gets the filename of the first audio stream
default_filename = music.default_filename
print("Downloading " + default_filename + "...")
# downloads first audio stream
music.download()
# creates mp3 filename for downloaded file
new_filename = default_filename[0:-3] + "mp3"
print("Converting to mp3....")
# converts mp4 audio to mp3 audio
subprocess.run(['ffmpeg', '-i',
os.path.join(filepath, default_filename),
os.path.join(filepath, new_filename)
])
print("Download finished.")
if __name__ == "__main__":
url = input("Please enter the url of the playlist you wish to download: ")
pl = Playlist(url)
run(pl)
错误:
Traceback (most recent call last):
File "C:/Users/Totenkopf/Downloads/test/main.py", line 40, in <module>
run(pl)
File "C:/Users/Totenkopf/Downloads/test/main.py", line 13, in run
links = pl.video_urls()
TypeError: 'DeferredGeneratorList' object is not callable
【问题讨论】:
-
你让我们猜测错误在哪里。请更新问题以包含整个错误回溯消息。
-
哪行代码给出了错误?你能用冰淇淋或打印语句来检查代码失败的地方吗?
-
抱歉,我把整个错误信息都包含在内了
-
看起来
video_urls是一个普通属性,而不是一个可调用函数。你为什么叫它? -
没错,我的错,谢谢各位。