1

Every time I try to download a video using Pytube, it generates this error:

FileNotFoundError: [Errno 2] No such file or directory:

Recently it has been showing this error. Before, it worked completely fine. Here's my code:

def vid_only():
    for stream in yt.streams.filter(only_video=True, adaptive=True, file_extension="mp4"):
        res_avail.add((stream.resolution))
    r = ", ".join(res_avail)
    print(f"Available resolutions:  {r}")

    res_choose = input("Input your desired resolution: ")
    print("Downloading . . .")
    vid_download = yt.streams.filter(res=res_choose.lower()).first()
    vid_download.download(filename=yt.title + " VIDEO ONLY.mp4")

link = input("Input youtube link: ")
yt = YouTube(link)
res_avail = set()
stream_type = int(input("Types: \n1. Video only \n2. Audio only \n3. Video with audio \nEnter the number of the type you want to download: "))

if stream_type == 1:
    vid_only()

Is there something wrong with this line?

vid_download.download(filename=yt.title + " VIDEO ONLY.mp4")
bliss
  • 23
  • 5

1 Answers1

0

I had this same error, in my case the file contained illegal "filename" characters... as the script above simply passes yt.title to the download call without making it "safe".

So I changed the filename line to...

filename = "".join([c for c in yt.title if c.isalpha() or c.isdigit() or c==' ']).rstrip()

... based on SO Answer (https://stackoverflow.com/a/7406369/1327508)

George 2.0 Hope
  • 583
  • 1
  • 6
  • 21