如何让它询问用户是否想要转换另一个视频?

发布于 2025-01-13 11:39:31 字数 992 浏览 3 评论 0原文

我有以下代码,这是一个简单的 YouTube 到 MP4 转换器,但我想实现询问用户是否要转换另一个视频的功能,而不仅仅是“按按钮退出”

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏林苍穹下 2025-01-20 11:39:31

在这种情况下,您可以做的是有一个 while 循环,其中包含您的代码,然后您可以询问用户是否希望继续。
您可以按照要求用户提供 YouTube 链接的方式,使用输入来完成此操作。然后您可以检查答案是是还是否,以及他们是否不想继续退出 while 循环并完成您的程序。

一个例子是:

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 

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 
谁的新欢旧爱 2025-01-20 11:39:31

将整个代码放在 while True 中,在过程结束时,询问用户是否转换另一个视频,如果用户说,则中断循环。< br>
最终的代码应该是这样的:

while True:
    """
       Your codes
    """
    user_input = input("Do you want to convert another video? [y/n]")
    if user_input not in ["y", "yes", "Y", "Yes"]:
        break

这是这个想法的简单实现!:)
也许你会带来更好的!

Put the whole code in a while True, at the end of the process, ask the user for converting another video, and if the user said no, break the loop.
The final code should be sth like this:

while True:
    """
       Your codes
    """
    user_input = input("Do you want to convert another video? [y/n]")
    if user_input not in ["y", "yes", "Y", "Yes"]:
        break

It's a simple implementation of the idea!:)
Maybe you come with a better one!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文