首页 > 解决方案 > 第二次下载我写的代码中下载并保存的文件时,我可以更改名称吗?

问题描述

今天我决定写一个 Youtube 视频下载器,我管理它没有任何问题。我有一个问题,当我在下面给出的代码中下载视频或声音时,我无法访问第一个下载的文件,因为我第二次没有下载的文件的名称是相同的。有什么办法可以解决这个问题吗?

from pytube import YouTube

def main():
    while True:
        link = input("Please enter the Youtube link you want to view : ") 
        file_path = input("Enter the file path for your downloads:")
        yt = YouTube(link)
        continuity = True
        while continuity:
            print("To download in high quality = 1'i\t\b\bTo download in low quality = 2'yi\nTo download as an audio file = 3'ü\t\b\bIf you want to enter another link, please press = 4\n\nTo terminate the program = q")
            entry = input()
            if entry == "1":
                high = yt.streams.get_highest_resolution()
                high.download(file_path)
                print("Video downloads in high quality...")
            elif entry == "2":
                low = yt.streams.get_lowest_resolution()
                low.download(file_path)
                print("Video downloads in low quality...")
            elif entry == "3":
                audio = yt.streams.get_audio_only()
                audio.download(file_path)
                print("Downloading audio file...")
            elif entry == "4":
                print("Please wait...")
                continuity = False
            elif entry.lower() == "q":
                print("Thank you for using our application. \nhttps://github.com/Batuhanaydn")
                exit()
            else:
                print("You entered an unknown login")
                break

main()

标签: pythonpytube

解决方案


您可以更改正在下载的文件名

stream.download(filename='something')

写入文件名时,不需要包含后缀,例如:.mp4 .mp3


推荐阅读