首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >YouTube视频下载器

YouTube视频下载器
EN

Stack Overflow用户
提问于 2020-03-30 03:20:53
回答 1查看 358关注 0票数 0

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

代码语言:javascript
复制
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, )
EN

回答 1

Stack Overflow用户

发布于 2020-03-30 05:13:37

好的,这一条很有趣。

真正的问题从这里开始。

代码语言:javascript
复制
    download = video_stream[video_quality].download(filepath=save_location, callback=self.Handel_Progress, )

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

问题的根本原因:

如果你查看download函数的定义,你会发现如果存在一个同名的文件,它根本不会下载该文件。

现在来了,你如何确保它被下载,不管怎样:

您需要做两件事:

  1. 检查是否存在同名文件,如果存在,则在文件名末尾的扩展名之前添加1。因此,如果abc.mp4存在,则保存abc1.mp4。我将告诉您如何处理abc.mp4abc1.mp4等存在的情况,但现在,让我们回到问题上来。
  2. 如何将文件名(abc1.mp4)传递给download方法?

下面这段代码可以同时处理这两种情况。我已经添加了一些评论,以供您理解。

代码语言:javascript
复制
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, )

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60919567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档