Search code examples
pythonmoviepypytube

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 Attribute Error : NoneType object has no attribute write_audiofile


Solution

  • Figured it out. Actually streams.filter("480p") was giving me a video only stream with no audio that's why the nonetype error because it had no audio object. solved it by doing 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"))