File extension changes when I try to rename Pytube video while downloading

Viewed 29

I'm trying to rename my downloaded Pytube video but when I try to change the filename, the file extension changes from mp4 to file.

Here's my code:


yt = YouTube("https://youtu.be/Igo8pUqvFMc")
stream_type = int(input("Types: \n1. Video only \n2. Audio only \n3. Video awith audio \nEnter the number of the type you want to download: "))

if stream_type == 1:
    videos = yt.streams.filter(only_video=True, adaptive=True, file_extension="mp4")
    vid = list(enumerate(videos))
    for i in vid:
        print(i)
    stream_no = int(input("Enter: "))
    videos[stream_no].download(filename=yt.title + " VIDEO ONLY")
1 Answers

The call to download() passes in a filename with no file extension. This is the file name used to save the file. Instead, add the mp4 extension at the end:

yt = YouTube("https://youtu.be/Igo8pUqvFMc")
stream_type = int(input("Types: \n1. Video only \n2. Audio only \n3. Video awith audio \nEnter the number of the type you want to download: "))

if stream_type == 1:
    videos = yt.streams.filter(only_video=True, adaptive=True, file_extension="mp4")
    vid = list(enumerate(videos))
    for i in vid:
        print(i)
    stream_no = int(input("Enter: "))
    videos[stream_no].download(filename=yt.title + " VIDEO ONLY.mp4")
Related