0
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

SagarRawat
  • 135
  • 2
  • 10

3 Answers3

1

Try:

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"))

The path needs to be built up in os.path by using comma not by "//".

ashwinjoseph
  • 359
  • 3
  • 12
0

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

You might be better off using ffmpeg for converting videos or opencv for all video management.

Gil Gvirts
  • 46
  • 3
  • The point of using `os.path.join` is to pass each part of the path in as a separate argument, not to connect them all with `+` beforehand. – Tom Burrows Jul 03 '20 at 02:06
  • That's right of course, I copy pasted original line only removed the `.audio` part, – Gil Gvirts Jul 03 '20 at 13:21
0

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"))
SagarRawat
  • 135
  • 2
  • 10