python实现音视频下载器

一、环境准备

确保当前系统已安装了wxPython 、 yt-dlp 和FFmpeg。当前主要支持下载youtube音视频

1、安装wxPython

pip install wxPython

2、安装yt-dp

pip install wxPython yt-dlp

3、安装FFmpeg

在Windows 10上通过命令行安装FFmpeg,最简便的方式是使用包管理器,比如Scoop或Chocolatey。以下是使用这两种工具的步骤:

① 使用Scoop安装FFmpeg

如果还没有安装Scoop,首先需要按照Scoop官网上的说明进行安装。打开PowerShell并运行以下命令来安装Scoop(如果尚未安装):

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
irm get.scoop.sh | iex

安装完Scoop后,你可以通过以下命令安装FFmpeg:

scoop install ffmpeg

② 使用Chocolatey安装FFmpeg

如果更倾向于使用Chocolatey,首先确保已经安装了Chocolatey。可以通过Chocolatey官网获取安装指南。
在这里插入图片描述

通常,可以在管理员模式下的PowerShell中运行以下命令来安装Chocolatey:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

在这里插入图片描述

安装完Chocolatey后,可以通过以下命令安装FFmpeg:

choco install ffmpeg

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
无论你选择哪种方式安装FFmpeg,完成后都可以通过命令行输入以下命令来验证是否安装成功:

ffmpeg -version

在这里插入图片描述

如果你看到FFmpeg的版本信息以及其他相关细节,那么说明FFmpeg已经正确安装,并且可以通过命令行使用了。

请注意,为了使这些命令在任何目录下都能生效,可能需要重启计算机或者重新打开一个新的命令提示符窗口。此外,确保你的环境变量Path包含了FFmpeg的安装路径,这样系统才能识别ffmpeg命令。通过上述方法安装时,通常会自动处理好这个问题。

二、代码编写

编写名字为downloader.py的python代码文件

import wx
import yt_dlp
import threading

class DownloaderFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title="Downloader", size=(400, 350))

        self.panel = wx.Panel(self)

        self.source_label = wx.StaticText(self.panel, label="Audio or video URL:")
        self.source_text = wx.TextCtrl(self.panel)
        self.source_label.SetBackgroundColour(wx.Colour(173, 216, 230))  # 设置背景为浅蓝色
        self.source_label.SetForegroundColour(wx.Colour(0, 0, 128))  # 设置文本颜色为黑色,确保对比度良好

        self.folder_label = wx.StaticText(self.panel, label="Save Folder:")
        self.folder_text = wx.TextCtrl(self.panel)
        self.folder_label.SetBackgroundColour(wx.Colour(173, 216, 230))  # 设置背景为浅蓝色
        self.folder_label.SetForegroundColour(wx.Colour(0, 0, 128))  # 设置文本颜色为黑色,确保对比度良好
        self.folder_button = wx.Button(self.panel, label="Browse...")
        self.folder_button.Bind(wx.EVT_BUTTON, self.browse_folder)

        self.quality_label = wx.StaticText(self.panel, label="Quality:")
        self.quality_choice = wx.ComboBox(self.panel, choices=[], style=wx.CB_READONLY)
        self.quality_label.SetBackgroundColour(wx.Colour(173, 216, 230))  # 设置背景为浅蓝色
        self.quality_label.SetForegroundColour(wx.Colour(0, 0, 128))  # 设置文本颜色为黑色,确保对比度良好

        self.start_button = wx.Button(self.panel, label="Start Download")
        self.start_button.Bind(wx.EVT_BUTTON, self.start)

        self.process_label = wx.StaticText(self.panel, label="Percentage:")
        self.process_bar = wx.Gauge(self.panel, range=100)
        self.process_label.SetBackgroundColour(wx.Colour(173, 216, 230))  # 设置背景为浅蓝色
        self.process_label.SetForegroundColour(wx.Colour(0, 0, 128))  # 设置文本颜色为黑色,确保对比度良好

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.source_label, 0, wx.ALL, 5)
        self.sizer.Add(self.source_text, 0, wx.EXPAND | wx.ALL, 5)
        self.sizer.Add(self.folder_label, 0, wx.ALL, 5)
        self.sizer.Add(self.folder_text, 0, wx.EXPAND | wx.ALL, 5)
        self.sizer.Add(self.folder_button, 0, wx.ALL, 5)
        self.sizer.Add(self.quality_label, 0, wx.ALL, 5)
        self.sizer.Add(self.quality_choice, 0, wx.EXPAND | wx.ALL, 5)
        self.sizer.Add(self.start_button, 0, wx.ALL, 5)
        self.sizer.Add(self.process_label, 0, wx.ALL, 5)
        self.sizer.Add(self.process_bar, 0, wx.EXPAND | wx.ALL, 5)

        self.panel.SetSizer(self.sizer)
        self.Show()

    def browse_folder(self, event):
        dlg = wx.DirDialog(self, "Choose a directory", style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.folder_text.SetValue(dlg.GetPath())
        dlg.Destroy()

    def get_video_formats(self, url):
        with yt_dlp.YoutubeDL({}) as ydl:
            info_dict = ydl.extract_info(url, download=False)
            #print(info_dict)  # 添加此行来查看所有可用格式
            formats = info_dict.get('formats', [])
            #print("Available formats:", formats)  # 打印所有可用格式以供调试

            self.quality_choice.Clear()
            best_format_id = None  # 初始化为None
            has_1080p = False

            # 首先尝试找到1080p或更高的视频格式
            for f in formats:
                if 'height' in f and 'format_id' in f and f['vcodec'] != 'none' and f['acodec'] != 'none':
                    display_str = f"{f['format_id']} - {f['height']}p"
                    if f['height'] >= 1080:  # 检查是否为1080p或更高
                        if not has_1080p or f['height'] > int(best_format_id.split(" - ")[1][:-1]):
                            best_format_id = display_str
                            has_1080p = True

            # 如果没有找到1080p或更高的格式,则选择其他可用的最高分辨率
            if not has_1080p:
                for f in formats:
                    if 'height' in f and 'format_id' in f and f['vcodec'] != 'none' and f['acodec'] != 'none':
                        display_str = f"{f['format_id']} - {f['height']}p"
                        if best_format_id is None or (
                                'height' in f and f['height'] > int(best_format_id.split(" - ")[1][:-1])):
                            best_format_id = display_str

            # 添加所有满足条件的格式到下拉列表中供用户选择
            for f in formats:
                if 'height' in f and 'format_id' in f and f['vcodec'] != 'none' and f['acodec'] != 'none':
                    self.quality_choice.Append(f"{f['format_id']} - {f['height']}p")

            if self.quality_choice.GetCount() > 0:
                self.quality_choice.SetSelection(0)
            return best_format_id

    def start(self, event):
        url = self.source_text.GetValue()
        save_folder = self.folder_text.GetValue()

        # 获取并填充清晰度选项
        best_format = self.get_video_formats(url)
        #print(best_format)

        if not best_format:  # 如果没有找到任何格式
            wx.MessageBox("No suitable video format found.", "Error", wx.OK | wx.ICON_ERROR)
            return

        download_thread = threading.Thread(target=self.download_Audio_or_video, args=(url, save_folder, best_format))
        download_thread.start()

    def download_Audio_or_video(self, url, save_folder, best_format):
        selected_format = self.quality_choice.GetStringSelection()
        format_id = selected_format.split(" - ")[0] if selected_format else best_format.split(" - ")[0]  # 提取format id

        options = {
            'outtmpl': f'{save_folder}/%(title)s.%(ext)s',
            #'format': 'best',  # 使用最佳质量
            'format': format_id,
            'progress_hooks': [self.update_progress],
        }
        try:
            with yt_dlp.YoutubeDL(options) as ydl:
                ydl.download([url])
            wx.CallAfter(wx.MessageBox, 'Download completed!', 'Info', wx.OK | wx.ICON_INFORMATION)
        except Exception as e:
            wx.CallAfter(wx.MessageBox, f'Download error occurred: {str(e)}', 'Error', wx.OK | wx.ICON_ERROR)

    def update_progress(self, progress ):
        if 'total_bytes' in progress and 'downloaded_bytes' in progress:
            percentage = int(progress['downloaded_bytes'] * 100 / progress['total_bytes'])
            wx.CallAfter(self.process_bar.SetValue, percentage)


app = wx.App(False)
frame = DownloaderFrame()
app.MainLoop()

三、代码运行测试

1.命令行运行测试

1.python downloader.py
在这里插入图片描述

三、其他方式

或者直接利用exe下载工具来进行命令行下载
在这里插入图片描述
参考文件:
https://blog.csdn.net/qq_31339083/article/details/132195733
https://blog.csdn.net/winniezhang/article/details/132127382

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月如琉璃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值