核心实现方案
import yt_dlp
import importlib
def video_downloader(url, quality='best'):
"""
视频下载核心函数
:param url: 视频链接
:param quality: 画质选择(best/worst/自定义格式)
:return: 下载状态
"""
ydl_opts = {
'format': quality,
'outtmpl': '%(title)s.%(ext)s',
'quiet': True,
'no_warnings': True
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
return True
except Exception as e:
print(f"下载失败: {str(e)}")
return False
import subprocess
import sys
import os
def get_executable_path():
"""获取可执行文件路径(适配打包环境)"""
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS if hasattr(sys, '_MEIPASS') else os.path.dirname(sys.executable)
else:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, 'yt-dlp.exe')
def download_with_subprocess(url):
"""通过子进程调用实现"""
try:
subprocess.run([get_executable_path(), url], check=True)
return True
except subprocess.CalledProcessError as e:
print(f"进程调用失败: {e}")
return False
def check_and_update():
"""模块热更新实现"""
try:
import requests
from packaging import version
# 获取当前版本
current_ver = yt_dlp.version.__version__
# 获取最新版本
resp = requests.get(
"https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest",
timeout=5
)
latest_ver = resp.json()["tag_name"].strip("v")
if version.parse(current_ver) < version.parse(latest_ver):
# 执行静默更新
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
try:
yt_dlp.YoutubeDL({'quiet': True}).update()
importlib.reload(yt_dlp)
return True
finally:
sys.stdout = original_stdout
return False
except Exception:
return False
import urllib3
http = urllib3.PoolManager(maxsize=10)
from concurrent.futures import ThreadPoolExecutor
def batch_download(urls):
with ThreadPoolExecutor(max_workers=4) as executor:
results = executor.map(video_downloader, urls)
return all(results)
from functools import lru_cache
@lru_cache(maxsize=128)
def get_video_info(url):
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
return ydl.extract_info(url, download=False)
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('yt_dlp') + collect_data_files('certifi')
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=datas,
hiddenimports=['yt_dlp', 'urllib3'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=1
)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。