Search code examples
pythonconverterspytube

How do I make it ask the user if they wish to convert another video?


I have the following code, which is a simple YouTube to MP4 Converter, but I would like to implement the function that asks the user if they want to convert another video, not just "Press a button to exit"

from pytube import YouTube
from pytube.cli import on_progress
from click import pause

# ask for the link from user
link = input("Enter the link of the YouTube video you want to download:  ")
yt = YouTube(link, on_progress_callback=on_progress)
print("\n---------------------Video Details---------------------------------------")
# Showing details
print("\nTitle: ", yt.title, "\n")
print("Number of views: ", yt.views, "\n")
print("Length of video: ", yt.length, "\n")
print("Rating of video: ", yt.rating, "\n")
print("-----------------------Video Download---------------------------------------")
# Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()

# Starting download and exiting
print("\n")
pause("Press any key to download...")
print("Downloading...")
ys.download()
print("Download completed!!")
print("\n")
pause("Thank you! Press any key to exit...")`

Solution

  • what you can do in this case is to have a while loop that has your code inside of it and then you can ask the user if they wish to continue. You can do that the same way you ask from the user to give you the youtube link, using input. Then you can check if the answer is yes or no and if they don't wish to continue exit the while loop and finish your program.

    an example would be:

    while True:
        # Download video code
        
        wish_to_continue = input("Do you want to continue with another video?")
        
        if wish_to_continue == "no":
            # exit the while loop and the program
            break