YouTube视频下载程序python

2024-04-19 07:06:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个youtube视频下载管理器。它下载了一个视频,但当我下载同一个视频时,我面临一个问题,它不会再下载。我怎样才能用相同的标题(如pic.png)再次下载它并发送pic1.png。我该怎么做

def Download(self):
    video_url = self.lineEdit.text()
    save_location = self.lineEdit_2.text()
    if video_url == '' or save_location == '':
        QMessageBox.warning(self, "Data Error", "Provide a Valid Video URL or save Location")

    else:
        video = pafy.new(video_url)
        video_stream = video.streams
        video_quality = self.comboBox.currentIndex()

        download = video_stream[video_quality].download(filepath=save_location, callback=self.Handel_Progress, )

Tags: ortextselfurl管理器stream视频png
1条回答
网友
1楼 · 发布于 2024-04-19 07:06:04

好的,这个很有趣

真正的问题从这里开始

    download = video_stream[video_quality].download(filepath=save_location, callback=self.Handel_Progress, )

这里,您正在调用video_stream对象的download函数,该函数将filepath作为文件位置的参数,但不使用文件名,因为显然,文件将以实际名称保存

问题的根本原因:

If you look into the definition of download function, you would find that if a file exists with the same name, it would not download the file at all.

现在是部分,您如何确保它下载,无论是什么:

您需要做两件事:

  1. 检查是否存在同名文件,如果存在,则在文件名末尾的扩展名之前添加1。因此,如果存在abc.mp4,则保存abc1.mp4。 [我将告诉您当abc.mp4abc1.mp4等存在时如何处理该场景,但现在,让我们回到问题上来。]

  2. 如何将文件名(abc1.mp4)传递给下载方法

下面的代码将处理这两个问题。 我添加了一些评论供您理解

import os
import re

import pafy
from pafy.util import xenc


# this function is used by pafy to generate file name while saving,
# so im using the same function to get the file name which I will use to check
# if file exists or not
# DO NOT CHANGE IT
def generate_filename(title, extension):
    max_length = 251

    """ Generate filename. """
    ok = re.compile(r'[^/]')

    if os.name == "nt":
        ok = re.compile(r'[^\\/:*?"<>|]')

    filename = "".join(x if ok.match(x) else "_" for x in title)

    if max_length:
        max_length = max_length + 1 + len(extension)
        if len(filename) > max_length:
            filename = filename[:max_length - 3] + '...'

    filename += "." + extension
    return xenc(filename)


def get_file_name_for_saving(save_location, full_name):
    file_path_with_name = os.path.join(save_location, full_name)

    # file exists, add 1 in the end, otherwise return filename as it is
    if os.path.exists(file_path_with_name):
        split = file_path_with_name.split(".")
        file_path_with_name = ".".join(split[:-1]) + "1." + split[-1]

    return file_path_with_name


def Download(self):
    video_url = self.lineEdit.text()
    save_location = self.lineEdit_2.text()
    if video_url == '' or save_location == '':
        QMessageBox.warning(self, "Data Error", "Provide a Valid Video URL or save Location")

    else:
        # video file
        video = pafy.new(video_url)

        # available video streams
        video_stream = video.streams
        video_quality = self.comboBox.currentIndex()

        # video title/name
        video_name = video.title

        # take out the extension of the file from video stream
        extension = video_stream[video_quality].extension

        # fullname with extension
        full_name = generate_filename(video_name, extension)

        final_path_with_file_name = get_file_name_for_saving(save_location, full_name)

        download = video_stream[video_quality].download(filepath=final_path_with_file_name,
                                                        callback=self.Handel_Progress, )

如果您遇到任何问题,请告诉我

相关问题 更多 >