我正在尝试使用 MoviePy 模块将视频 mp4 文件转换为音频 mp3 格式,但它显示 Nonetype 错误

I am trying to convert an video mp4 file to Audio mp3 format by using MoviePy module but it is showing me Nonetype error

def download_audio():
#try:
    b2.config(text="Please wait...")
    b2.config(state=DISABLED)
    stream = yt.streams.filter(res="480p")
    path = filedialog.askdirectory()
    if path == None:
        return
    stream[0].download(path)
    for i in os.listdir(path):
        os.rename(os.path.join(path,i),os.path.join(path,i.replace(' ','_')))
    title = yt.title.replace(' ','_')
    print(title)
    print(path)
    video = VideoFileClip(os.path.join(path+"//"+title+".mp4"))
    print(video)
    video.audio.write_audiofile(os.path.join(path+"//"+title+".mp3"))

This is the Error message 属性错误:NoneType 对象没有属性 write_audiofile

尝试video.write_audiofile(os.path.join(path+"//"+title+".mp3"))

您最好使用 ffmpeg 转换视频或使用 opencv 进行所有视频管理。

尝试:

def download_audio():
#try:
    b2.config(text="Please wait...")
    b2.config(state=DISABLED)
    stream = yt.streams.filter(res="480p")
    path = filedialog.askdirectory()
    if path == None:
        return
    stream[0].download(path)
    for i in os.listdir(path):
        os.rename(os.path.join(path,i),os.path.join(path,i.replace(' ','_')))
    title = yt.title.replace(' ','_')
    print(title)
    print(path)
    video = VideoFileClip(os.path.join(path,title+".mp4"))
    print(video)
    video.audio.write_audiofile(os.path.join(path,title+".mp3"))

需要在 os.path 中使用逗号而不是“//”构建路径。

想通了。 实际上 streams.filter("480p") 给我一个没有音频的纯视频流,这就是为什么出现非类型错误,因为它没有音频对象。 通过 streams.filter(progressive=True).

解决了它
def download_audio():
#try:
b2.config(text="Please wait...")
b2.config(state=DISABLED)
stream = yt.streams.filter(progressive=True)
path = filedialog.askdirectory()
if path == None:
    return
stream[0].download(path)
for i in os.listdir(path):
    os.rename(os.path.join(path,i),os.path.join(path,i.replace(' ','_')))
title = yt.title.replace(' ','_')
print(title)
print(path)
video = VideoFileClip(os.path.join(path+"//"+title+".mp4"))
print(video)
video.audio.write_audiofile(os.path.join(path+"//"+title+".mp3"))