上传完整项目文件

This commit is contained in:
xhgit committed 2026-06-21 14:26:15 +08:00
1 parent e63d58df8d
commit aa0dd45839
8663 files changed
+2393125

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
"""下载器模块
提供视频下载、播放列表解析、字幕下载、封面下载、
FFmpeg 合并、FFmpeg 自动安装和队列管理等核心下载功能。
"""
from .format_parser import FormatParser
from .ffmpeg_merger import FFmpegMerger
from .ffmpeg_installer import FFmpegInstaller, auto_install_ffmpeg
from .video_downloader import VideoDownloader
from .playlist_downloader import PlaylistDownloader
from .subtitle_downloader import SubtitleDownloader
from .thumbnail_downloader import ThumbnailDownloader
from .download_queue import DownloadQueue
__all__ = [
"FormatParser",
"FFmpegMerger",
"FFmpegInstaller",
"auto_install_ffmpeg",
"VideoDownloader",
"PlaylistDownloader",
"SubtitleDownloader",
"ThumbnailDownloader",
"DownloadQueue",
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
+466
View File
@@ -0,0 +1,466 @@
"""下载队列管理模块
使用 ThreadPoolExecutor 实现并发下载管理,
支持任务排队、暂停、恢复、取消和重试功能。
"""
import logging
import threading
import time
from concurrent.futures import ThreadPoolExecutor, Future, as_completed
from queue import Queue, PriorityQueue
from typing import Dict, List, Optional, Callable
from ..models.task import DownloadTask, TaskStatus, DownloadProgress
logger = logging.getLogger(__name__)
class DownloadQueue:
"""下载队列管理器
基于 ThreadPoolExecutor 实现多线程并发下载,
提供任务队列管理、进度追踪和状态控制。
特性:
- 支持 1-32 线程并发
- 任务优先级排序
- 实时进度回调
- 暂停/恢复/取消操作
- 失败任务重试
"""
def __init__(self, max_workers: int = 4) -> None:
"""初始化下载队列
Args:
max_workers: 最大并发下载线程数 (1-32)
"""
self._max_workers = max(1, min(max_workers, 32))
self._executor: Optional[ThreadPoolExecutor] = None
self._futures: Dict[str, Future] = {} # task_id -> Future
self._tasks: Dict[str, DownloadTask] = {} # task_id -> DownloadTask
# 暂停队列中等待执行的任务
self._pending_tasks: List[DownloadTask] = []
# 回调
self._on_task_started: Optional[Callable[[DownloadTask], None]] = None
self._on_task_progress: Optional[Callable[[DownloadTask, DownloadProgress, str], None]] = None
self._on_task_completed: Optional[Callable[[DownloadTask], None]] = None
self._on_task_failed: Optional[Callable[[DownloadTask], None]] = None
self._on_all_completed: Optional[Callable[[], None]] = None
# 状态
self._is_paused = False
self._is_running = False
self._lock = threading.RLock()
# ========== 属性 ==========
@property
def max_workers(self) -> int:
"""最大并发数"""
return self._max_workers
@max_workers.setter
def max_workers(self, value: int) -> None:
"""设置最大并发数"""
self._max_workers = max(1, min(value, 32))
@property
def is_running(self) -> bool:
"""队列是否正在运行"""
return self._is_running
@property
def is_paused(self) -> bool:
"""队列是否已暂停"""
return self._is_paused
@property
def task_count(self) -> int:
"""队列中的总任务数"""
return len(self._tasks)
@property
def active_tasks(self) -> List[DownloadTask]:
"""获取所有活动中的任务"""
with self._lock:
return [
t for t in self._tasks.values()
if t.status in (TaskStatus.DOWNLOADING, TaskStatus.MERGING)
]
@property
def pending_tasks(self) -> List[DownloadTask]:
"""获取所有等待中的任务"""
with self._lock:
return [
t for t in self._tasks.values()
if t.status == TaskStatus.WAITING
]
@property
def completed_tasks(self) -> List[DownloadTask]:
"""获取所有已完成的任务"""
with self._lock:
return [
t for t in self._tasks.values()
if t.status == TaskStatus.COMPLETED
]
@property
def failed_tasks(self) -> List[DownloadTask]:
"""获取所有失败的任务"""
with self._lock:
return [
t for t in self._tasks.values()
if t.status == TaskStatus.FAILED
]
# ========== 回调设置 ==========
def set_callbacks(
self,
on_started: Optional[Callable[[DownloadTask], None]] = None,
on_progress: Optional[Callable[[DownloadTask, DownloadProgress, str], None]] = None,
on_completed: Optional[Callable[[DownloadTask], None]] = None,
on_failed: Optional[Callable[[DownloadTask], None]] = None,
on_all_completed: Optional[Callable[[], None]] = None,
) -> None:
"""设置回调函数
Args:
on_started: 任务开始回调
on_progress: 任务进度回调
on_completed: 任务完成回调
on_failed: 任务失败回调
on_all_completed: 全部完成回调
"""
self._on_task_started = on_started
self._on_task_progress = on_progress
self._on_task_completed = on_completed
self._on_task_failed = on_failed
self._on_all_completed = on_all_completed
# ========== 任务管理 ==========
def add_task(self, task: DownloadTask) -> None:
"""添加下载任务到队列
Args:
task: 下载任务对象
"""
with self._lock:
task.status = TaskStatus.WAITING
self._tasks[task.task_id] = task
logger.info(f"任务已添加: {task.task_id} - {task.url[:60]}")
def add_tasks(self, tasks: List[DownloadTask]) -> None:
"""批量添加下载任务
Args:
tasks: 下载任务列表
"""
for task in tasks:
self.add_task(task)
def remove_task(self, task_id: str) -> bool:
"""移除下载任务
Args:
task_id: 任务 ID
Returns:
是否成功移除
"""
with self._lock:
if task_id not in self._tasks:
return False
task = self._tasks[task_id]
# 不能移除正在下载的任务
if task.status in (TaskStatus.DOWNLOADING, TaskStatus.MERGING):
return False
del self._tasks[task_id]
logger.info(f"任务已移除: {task_id}")
return True
def clear_completed(self) -> int:
"""清空已完成的任务
Returns:
清除的任务数量
"""
with self._lock:
completed_ids = [
tid for tid, t in self._tasks.items()
if t.status == TaskStatus.COMPLETED
]
for tid in completed_ids:
del self._tasks[tid]
logger.info(f"已清除 {len(completed_ids)} 个已完成任务")
return len(completed_ids)
def clear_failed(self) -> int:
"""清空失败的任务
Returns:
清除的任务数量
"""
with self._lock:
failed_ids = [
tid for tid, t in self._tasks.items()
if t.status == TaskStatus.FAILED
]
for tid in failed_ids:
del self._tasks[tid]
logger.info(f"已清除 {len(failed_ids)} 个失败任务")
return len(failed_ids)
def clear_all(self) -> None:
"""清空所有任务(不会取消正在下载的任务)"""
with self._lock:
active_ids = {
tid for tid, t in self._tasks.items()
if t.status in (TaskStatus.DOWNLOADING, TaskStatus.MERGING)
}
# 保留活动任务
self._tasks = {
tid: t for tid, t in self._tasks.items()
if tid in active_ids
}
# ========== 队列控制 ==========
def start(self, download_func: Callable[[DownloadTask], bool]) -> None:
"""启动下载队列
创建线程池并开始处理队列中的任务。
Args:
download_func: 下载函数,接收 DownloadTask 返回 bool
"""
if self._is_running:
logger.warning("队列已在运行中")
return
with self._lock:
self._is_running = True
self._is_paused = False
# 创建线程池
self._executor = ThreadPoolExecutor(max_workers=self._max_workers)
# 提交所有等待中的任务
waiting_tasks = self.pending_tasks
for task in waiting_tasks:
self._submit_task(task, download_func)
# 提交暂存的任务
for task in self._pending_tasks:
self._submit_task(task, download_func)
self._pending_tasks.clear()
logger.info(f"下载队列已启动 (workers={self._max_workers})")
# 启动监控线程(用于检测全部完成)
threading.Thread(
target=self._monitor_completion,
daemon=True,
name="queue-monitor",
).start()
def stop(self) -> None:
"""停止下载队列"""
with self._lock:
self._is_running = False
# 取消所有未开始的任务
for task in self._tasks.values():
if task.status == TaskStatus.WAITING:
task.status = TaskStatus.CANCELLED
# 关闭线程池
if self._executor:
self._executor.shutdown(wait=False, cancel_futures=True)
self._executor = None
logger.info("下载队列已停止")
def pause_all(self) -> None:
"""暂停所有下载"""
self._is_paused = True
logger.info("所有下载已暂停")
def resume_all(self) -> None:
"""恢复所有下载"""
self._is_paused = False
logger.info("所有下载已恢复")
def cancel_task(self, task_id: str) -> bool:
"""取消指定任务
Args:
task_id: 任务 ID
Returns:
是否成功取消
"""
with self._lock:
if task_id not in self._tasks:
return False
task = self._tasks[task_id]
future = self._futures.get(task_id)
if task.status == TaskStatus.WAITING and future:
future.cancel()
task.status = TaskStatus.CANCELLED
return True
if task.status in (TaskStatus.DOWNLOADING, TaskStatus.MERGING):
# 标记为取消,实际取消由下载器处理
task.status = TaskStatus.CANCELLED
return True
return False
def retry_failed(self, download_func: Callable[[DownloadTask], bool]) -> int:
"""重试所有失败的任务
Args:
download_func: 下载函数
Returns:
重试的任务数量
"""
with self._lock:
failed = self.failed_tasks
for task in failed:
task.reset_progress()
task.status = TaskStatus.WAITING
if self._is_running and self._executor:
self._submit_task(task, download_func)
logger.info(f"重试 {len(failed)} 个失败任务")
return len(failed)
# ========== 内部方法 ==========
def _submit_task(
self,
task: DownloadTask,
download_func: Callable[[DownloadTask], bool],
) -> None:
"""提交任务到线程池执行
Args:
task: 下载任务
download_func: 实际下载函数
"""
if self._executor is None:
return
future = self._executor.submit(self._execute_task, task, download_func)
self._futures[task.task_id] = future
def _execute_task(
self,
task: DownloadTask,
download_func: Callable[[DownloadTask], bool],
) -> None:
"""在线程池中执行单个下载任务
Args:
task: 下载任务
download_func: 实际下载函数
"""
task.status = TaskStatus.DOWNLOADING
if self._on_task_started:
self._on_task_started(task)
try:
success = download_func(task)
if task.status == TaskStatus.CANCELLED:
if self._on_task_failed:
self._on_task_failed(task)
elif success:
task.status = TaskStatus.COMPLETED
if self._on_task_completed:
self._on_task_completed(task)
else:
task.status = TaskStatus.FAILED
if self._on_task_failed:
self._on_task_failed(task)
except Exception as e:
logger.error(f"任务执行异常 {task.task_id}: {e}")
task.status = TaskStatus.FAILED
task.error_message = str(e)[:500]
if self._on_task_failed:
self._on_task_failed(task)
def _monitor_completion(self) -> None:
"""监控所有任务是否完成"""
while self._is_running:
time.sleep(1)
with self._lock:
if not self._tasks:
continue
# 检查是否所有任务都已完成(不是等待中或下载中)
active_statuses = {
TaskStatus.WAITING,
TaskStatus.DOWNLOADING,
TaskStatus.MERGING,
TaskStatus.PAUSED,
}
has_active = any(
t.status in active_statuses
for t in self._tasks.values()
)
if not has_active:
self._is_running = False
if self._executor:
self._executor.shutdown(wait=False)
self._executor = None
if self._on_all_completed:
self._on_all_completed()
break
def get_task(self, task_id: str) -> Optional[DownloadTask]:
"""获取指定任务
Args:
task_id: 任务 ID
Returns:
DownloadTask 或 None
"""
return self._tasks.get(task_id)
def get_all_tasks(self) -> List[DownloadTask]:
"""获取所有任务"""
return list(self._tasks.values())
def get_tasks_by_status(self, status: TaskStatus) -> List[DownloadTask]:
"""按状态获取任务
Args:
status: 任务状态
Returns:
符合条件的任务列表
"""
return [t for t in self._tasks.values() if t.status == status]
+328
View File
@@ -0,0 +1,328 @@
"""FFmpeg 自动安装模块
当系统未检测到 FFmpeg 时,自动从官方源下载 Windows 版本
并解压到本地目录,无需用户手动配置。
"""
import logging
import os
import shutil
import subprocess
import tempfile
import urllib.request
import zipfile
from pathlib import Path
from typing import Optional, Callable
logger = logging.getLogger(__name__)
# FFmpeg Windows 构建下载地址(gyan.dev 稳定版)
FFMPEG_DOWNLOAD_URL = (
"https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
)
# 备用下载地址(BtbN 构建)
FFMPEG_DOWNLOAD_URL_BACKUP = (
"https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/"
"ffmpeg-master-latest-win64-gpl.zip"
)
# 默认安装目录
DEFAULT_INSTALL_DIR = Path.home() / ".youtube_downloader_pro" / "ffmpeg"
class FFmpegInstaller:
"""FFmpeg 自动安装器
负责下载、解压和配置 FFmpeg。
支持进度回调和取消操作。
"""
def __init__(self, install_dir: Optional[Path] = None) -> None:
"""初始化安装器
Args:
install_dir: 安装目录,默认为用户目录下的 .youtube_downloader_pro/ffmpeg
"""
self._install_dir = install_dir or DEFAULT_INSTALL_DIR
self._cancel_flag = False
self._progress_callback: Optional[Callable[[str, float], None]] = None
@property
def install_dir(self) -> Path:
"""获取安装目录"""
return self._install_dir
@property
def ffmpeg_exe_path(self) -> Path:
"""获取安装后的 ffmpeg.exe 路径"""
return self._install_dir / "bin" / "ffmpeg.exe"
def is_installed(self) -> bool:
"""检查 FFmpeg 是否已在本地安装目录中
Returns:
已安装返回 True
"""
return self.ffmpeg_exe_path.exists()
def set_progress_callback(
self, callback: Optional[Callable[[str, float], None]]
) -> None:
"""设置进度回调
Args:
callback: 回调函数,接收 (状态描述, 百分比 0-100)
"""
self._progress_callback = callback
def cancel(self) -> None:
"""取消安装"""
self._cancel_flag = True
def install(self) -> bool:
"""执行自动安装
下载 FFmpeg 压缩包,解压到安装目录,
并验证安装结果。
Returns:
安装成功返回 True
"""
self._cancel_flag = False
try:
# 确保安装目录存在
self._install_dir.mkdir(parents=True, exist_ok=True)
# 步骤 1:下载
zip_path = self._download()
if not zip_path:
return False
# 步骤 2:解压
if not self._extract(zip_path):
return False
# 步骤 3:验证
if not self._verify():
return False
# 步骤 4:清理
self._cleanup(zip_path)
self._report_progress("FFmpeg 安装完成!", 100.0)
logger.info(f"FFmpeg 安装成功: {self.ffmpeg_exe_path}")
return True
except Exception as e:
logger.error(f"FFmpeg 安装失败: {e}")
self._report_progress(f"安装失败: {str(e)}", 0.0)
return False
def _download(self) -> Optional[Path]:
"""下载 FFmpeg 压缩包
Returns:
下载的压缩包路径,失败返回 None
"""
self._report_progress("正在下载 FFmpeg...", 5.0)
zip_path = self._install_dir / "ffmpeg_temp.zip"
# 尝试主下载地址
urls = [FFMPEG_DOWNLOAD_URL, FFMPEG_DOWNLOAD_URL_BACKUP]
for url in urls:
try:
logger.info(f"下载 FFmpeg: {url}")
self._report_progress(f"正在下载 FFmpeg... (这可能需要几分钟)", 10.0)
# 使用 urllib 下载,支持进度回调
def _progress_hook(block_count, block_size, total_size):
if self._cancel_flag:
raise InterruptedError("用户取消安装")
if total_size > 0:
percent = 10.0 + (block_count * block_size / total_size) * 60.0
downloaded = block_count * block_size
self._report_progress(
f"正在下载 FFmpeg... ({downloaded / 1024 / 1024:.1f} MB / {total_size / 1024 / 1024:.1f} MB)",
min(percent, 70.0),
)
urllib.request.urlretrieve(
url, str(zip_path), reporthook=_progress_hook
)
if zip_path.exists() and zip_path.stat().st_size > 100000:
logger.info(f"FFmpeg 下载完成 ({zip_path.stat().st_size / 1024 / 1024:.1f} MB)")
return zip_path
else:
logger.warning("下载文件太小,尝试备用地址")
zip_path.unlink(missing_ok=True)
except InterruptedError:
logger.info("用户取消 FFmpeg 下载")
zip_path.unlink(missing_ok=True)
return None
except Exception as e:
logger.warning(f"下载地址失败 {url}: {e}")
zip_path.unlink(missing_ok=True)
continue
logger.error("所有下载地址均失败")
self._report_progress("下载失败,请检查网络连接", 0.0)
return None
def _extract(self, zip_path: Path) -> bool:
"""解压 FFmpeg 压缩包
解压后重命名内部目录,使 ffmpeg.exe 位于 bin/ 子目录下。
Args:
zip_path: 压缩包路径
Returns:
解压成功返回 True
"""
self._report_progress("正在解压 FFmpeg...", 75.0)
try:
with zipfile.ZipFile(str(zip_path), "r") as zf:
# 获取压缩包内顶层目录名
names = zf.namelist()
top_dirs = set()
for name in names:
parts = name.split("/")
if parts[0]:
top_dirs.add(parts[0])
# 解压到临时目录
extract_dir = self._install_dir / "_extract_temp"
if extract_dir.exists():
shutil.rmtree(str(extract_dir))
extract_dir.mkdir(parents=True, exist_ok=True)
self._report_progress("正在解压 FFmpeg... (文件较多,请稍候)", 80.0)
zf.extractall(str(extract_dir))
# 查找 bin/ffmpeg.exe
ffmpeg_exe = None
for root, dirs, files in os.walk(str(extract_dir)):
if "ffmpeg.exe" in files:
ffmpeg_exe = Path(root) / "ffmpeg.exe"
break
if ffmpeg_exe is None:
logger.error("解压后未找到 ffmpeg.exe")
return False
# 移动文件到标准位置
bin_dir = self._install_dir / "bin"
if bin_dir.exists():
shutil.rmtree(str(bin_dir))
bin_dir.mkdir(parents=True, exist_ok=True)
# 复制 bin 目录下所有文件
ffmpeg_bin_dir = ffmpeg_exe.parent
for item in ffmpeg_bin_dir.iterdir():
dest = bin_dir / item.name
if item.is_file():
shutil.copy2(str(item), str(dest))
elif item.is_dir():
if dest.exists():
shutil.rmtree(str(dest))
shutil.copytree(str(item), str(dest))
# 清理临时目录
shutil.rmtree(str(extract_dir))
self._report_progress("解压完成", 90.0)
logger.info(f"FFmpeg 解压完成: {bin_dir}")
return True
except zipfile.BadZipFile:
logger.error("FFmpeg 压缩包损坏")
return False
except Exception as e:
logger.error(f"解压 FFmpeg 失败: {e}")
return False
def _verify(self) -> bool:
"""验证安装结果
Returns:
FFmpeg 可正常执行为 True
"""
self._report_progress("正在验证 FFmpeg 安装...", 95.0)
ffmpeg_exe = str(self.ffmpeg_exe_path)
try:
result = subprocess.run(
[ffmpeg_exe, "-version"],
capture_output=True,
text=True,
timeout=15,
creationflags=(
subprocess.CREATE_NO_WINDOW
if os.name == "nt"
else 0
),
)
if result.returncode == 0:
version_line = result.stdout.split("\n")[0] if result.stdout else "OK"
logger.info(f"FFmpeg 验证成功: {version_line}")
return True
return False
except Exception as e:
logger.error(f"FFmpeg 验证失败: {e}")
return False
def _cleanup(self, zip_path: Path) -> None:
"""清理临时文件
Args:
zip_path: 下载的压缩包路径
"""
try:
zip_path.unlink(missing_ok=True)
except OSError:
pass
def _report_progress(self, message: str, percent: float) -> None:
"""报告安装进度
Args:
message: 状态描述
percent: 进度百分比 (0-100)
"""
if self._progress_callback:
self._progress_callback(message, percent)
def auto_install_ffmpeg(
progress_callback: Optional[Callable[[str, float], None]] = None,
install_dir: Optional[Path] = None,
) -> Optional[str]:
"""自动安装 FFmpeg 的便捷函数
Args:
progress_callback: 进度回调 (状态消息, 百分比)
install_dir: 安装目录,默认自动选择
Returns:
安装成功返回 ffmpeg.exe 路径,失败返回 None
"""
installer = FFmpegInstaller(install_dir)
installer.set_progress_callback(progress_callback)
if installer.is_installed():
logger.info("FFmpeg 已安装,跳过下载")
return str(installer.ffmpeg_exe_path)
if installer.install():
return str(installer.ffmpeg_exe_path)
return None
+238
View File
@@ -0,0 +1,238 @@
"""FFmpeg 音视频合并模块
负责调用 FFmpeg 进行音视频流合并,支持进度回调和错误处理。
"""
import logging
import os
import subprocess
import shutil
from pathlib import Path
from typing import Optional, Callable
logger = logging.getLogger(__name__)
class FFmpegMerger:
"""FFmpeg 合并器
负责将分离的视频流和音频流合并为单个文件,
支持自动检测 FFmpeg 安装路径和进度回调。
"""
def __init__(self, ffmpeg_path: str = "") -> None:
"""初始化合并器
Args:
ffmpeg_path: FFmpeg 可执行文件路径,为空时自动检测
"""
self._ffmpeg_path = ffmpeg_path or self._auto_detect_ffmpeg()
self._available = self._check_ffmpeg()
@property
def is_available(self) -> bool:
"""FFmpeg 是否可用"""
return self._available
@property
def ffmpeg_path(self) -> str:
"""获取 FFmpeg 路径"""
return self._ffmpeg_path
@ffmpeg_path.setter
def ffmpeg_path(self, path: str) -> None:
"""设置 FFmpeg 路径并重新检测"""
self._ffmpeg_path = path
self._available = self._check_ffmpeg()
def _auto_detect_ffmpeg(self) -> str:
"""自动检测系统 FFmpeg 安装
按以下顺序查找:
1. 环境变量 PATH
2. 应用自带安装目录 (~/.youtube_downloader_pro/ffmpeg)
3. 项目目录下的 FFmpeg 文件夹
4. Windows 常见安装目录
5. 系统 PATH 搜索
Returns:
FFmpeg 可执行文件路径,未找到返回 "ffmpeg"
"""
# 1. 检查 PATH 中是否存在
if shutil.which("ffmpeg"):
return "ffmpeg"
if shutil.which("ffmpeg.exe"):
return "ffmpeg.exe"
# 2. 应用自带安装目录
app_ffmpeg = Path.home() / ".youtube_downloader_pro" / "ffmpeg" / "bin" / "ffmpeg.exe"
if app_ffmpeg.exists():
return str(app_ffmpeg)
# 3. 项目目录下的 FFmpeg 文件夹(用户可能手动放置)
project_root = Path(__file__).parent.parent # youtube_downloader/
for candidate in [
project_root / "FFmpeg" / "bin" / "ffmpeg.exe",
project_root / "ffmpeg" / "bin" / "ffmpeg.exe",
project_root / "FFmpeg" / "ffmpeg.exe",
project_root.parent / "FFmpeg" / "bin" / "ffmpeg.exe",
]:
if candidate.exists():
return str(candidate)
# 4. Windows 常见安装目录
common_paths = [
Path("C:/ffmpeg/bin/ffmpeg.exe"),
Path("C:/Program Files/ffmpeg/bin/ffmpeg.exe"),
Path("C:/Program Files (x86)/ffmpeg/bin/ffmpeg.exe"),
Path.home() / "ffmpeg/bin/ffmpeg.exe",
Path.home() / "AppData/Local/ffmpeg/bin/ffmpeg.exe",
]
for path in common_paths:
if path.exists():
return str(path)
return "ffmpeg"
def _check_ffmpeg(self) -> bool:
"""检查 FFmpeg 是否可用
Returns:
FFmpeg 可用返回 True
"""
try:
result = subprocess.run(
[self._ffmpeg_path, "-version"],
capture_output=True,
text=True,
timeout=10,
creationflags=(
subprocess.CREATE_NO_WINDOW
if os.name == "nt"
else 0
),
)
if result.returncode == 0:
logger.debug(f"FFmpeg 检测成功: {self._ffmpeg_path}")
return True
return False
except (FileNotFoundError, subprocess.TimeoutExpired, PermissionError):
logger.debug(f"FFmpeg 不可用: {self._ffmpeg_path}")
return False
def merge(
self,
video_path: str,
audio_path: str,
output_path: str,
progress_callback: Optional[Callable[[str], None]] = None,
) -> bool:
"""合并视频和音频文件
使用 FFmpeg 将视频流和音频流合并为单个 MP4 文件。
Args:
video_path: 视频文件路径
audio_path: 音频文件路径
output_path: 输出文件路径
progress_callback: 进度回调函数,接收状态描述字符串
Returns:
合并成功返回 True,失败返回 False
"""
if not self._available:
logger.error("FFmpeg 不可用,无法合并")
if progress_callback:
progress_callback("FFmpeg 不可用,请先安装 FFmpeg")
return False
# 验证输入文件存在
if not os.path.exists(video_path):
logger.error(f"视频文件不存在: {video_path}")
return False
if not os.path.exists(audio_path):
logger.error(f"音频文件不存在: {audio_path}")
return False
# 确保输出目录存在
output_dir = os.path.dirname(output_path)
Path(output_dir).mkdir(parents=True, exist_ok=True)
if progress_callback:
progress_callback("正在合并音视频...")
# 构建 FFmpeg 命令
cmd = [
self._ffmpeg_path,
"-i", video_path, # 输入:视频文件
"-i", audio_path, # 输入:音频文件
"-c:v", "copy", # 复制视频流(不重新编码)
"-c:a", "aac", # 音频编码为 AAC
"-b:a", "192k", # 音频比特率 192kbps
"-movflags", "+faststart", # 支持流媒体快速启动
"-y", # 覆盖输出文件
output_path,
]
logger.info(f"执行 FFmpeg 命令: {' '.join(cmd)}")
try:
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
creationflags=(
subprocess.CREATE_NO_WINDOW
if os.name == "nt"
else 0
),
)
# 等待完成(超时 30 分钟)
stdout, stderr = process.communicate(timeout=1800)
if process.returncode == 0:
if progress_callback:
progress_callback("合并完成")
# 验证输出文件
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
logger.info(f"合并成功: {output_path}")
return True
else:
logger.error("合并后输出文件无效")
return False
else:
logger.error(f"FFmpeg 合并失败 (返回码 {process.returncode}):\n{stderr[:500]}")
if progress_callback:
progress_callback(f"合并失败: FFmpeg 返回错误码 {process.returncode}")
return False
except subprocess.TimeoutExpired:
logger.error("FFmpeg 合并超时 (30分钟)")
process.kill()
if progress_callback:
progress_callback("合并超时")
return False
except Exception as e:
logger.error(f"FFmpeg 合并异常: {e}")
if progress_callback:
progress_callback(f"合并失败: {str(e)}")
return False
@staticmethod
def clean_temp_files(*paths: str) -> None:
"""清理临时文件
Args:
*paths: 要删除的文件路径列表
"""
for path in paths:
try:
if path and os.path.exists(path):
os.remove(path)
logger.debug(f"已删除临时文件: {path}")
except (PermissionError, OSError) as e:
logger.warning(f"删除临时文件失败 {path}: {e}")
+581
View File
@@ -0,0 +1,581 @@
"""视频格式解析模块
使用 yt-dlp 提取视频信息,解析所有可用的视频/音频格式,
并输出结构化的格式数据供 UI 展示和下载选择。
"""
import logging
from pathlib import Path
from typing import List, Optional, Dict, Any
import yt_dlp
from ..models.task import VideoInfo, VideoFormat, SubtitleInfo
from ..utils.user_agents import get_random_ua
logger = logging.getLogger(__name__)
# 自动回退的浏览器列表(按优先级)
_DEFAULT_FALLBACK_BROWSERS = ["chrome", "firefox", "edge", "brave", "opera"]
def _is_cookie_error(msg: str) -> bool:
"""判断错误是否为 Cookie 相关(可回退到下一个浏览器)
Args:
msg: 错误消息
Returns:
True 表示可以尝试下一个浏览器
"""
msg_lower = msg.lower()
return (
"sign in to confirm" in msg_lower
or "failed to load cookies" in msg_lower
or "could not copy" in msg_lower
or "cookie" in msg_lower
)
def _parse_browser_list(raw: str) -> List[str]:
"""解析逗号分隔的浏览器列表
Args:
raw: 如 "chrome,firefox,edge" 或 "auto"
Returns:
浏览器名称列表
"""
if not raw or not raw.strip():
return []
stripped = raw.strip()
if stripped == "auto":
return list(_DEFAULT_FALLBACK_BROWSERS)
browsers = [b.strip() for b in stripped.split(",") if b.strip()]
return browsers or []
class FormatParser:
"""视频格式解析器
负责通过 yt-dlp 提取视频元数据和可用格式列表,
支持视频信息获取、格式过滤和字幕列表获取。
Cookie 回退机制:当配置的浏览器 Cookie 读取失败时,
自动尝试列表中的下一个浏览器,直到成功或全部失败。
"""
# 视频分辨率优先级 (从低到高)
RESOLUTION_ORDER = [
"144p", "240p", "360p", "480p", "720p",
"1080p", "1440p", "2160p", "4320p",
]
# 音频格式代码映射
AUDIO_FORMAT_MAP = {
"mp3": {"ext": "mp3", "acodec": "mp3", "preference": 1},
"m4a": {"ext": "m4a", "acodec": "mp4a", "preference": 2},
"aac": {"ext": "aac", "acodec": "aac", "preference": 3},
"opus": {"ext": "opus", "acodec": "opus", "preference": 4},
}
def __init__(self, cookies_from_browser: str = "", cookies_file: str = "") -> None:
"""初始化格式解析器
Args:
cookies_from_browser: 浏览器名称(或逗号分隔的多个名称),如 "chrome,firefox"
cookies_file: Cookie 文件路径(Netscape 格式),优先于浏览器 Cookie
"""
self._cookies_file = cookies_file
self._browsers = _parse_browser_list(cookies_from_browser)
# 基础选项(不含 Cookie 配置,每次 _build_ydl_opts 时动态添加)
self._base_opts: Dict[str, Any] = {
"quiet": True,
"no_warnings": True,
"extract_flat": False,
"skip_download": True,
"ignoreerrors": True,
"socket_timeout": 30,
"http_headers": {"User-Agent": get_random_ua()},
# 跳过播放列表 tab 的认证检查(video URL 带 ?list= 时触发)
"extractor_args": {"youtubetab": {"skip": ["authcheck"]}},
# 启用 Node.js 作为 JS 运行时 + 远程挑战求解器(解析现代 YouTube 页面)
"js_runtimes": {"node": {}, "deno": {}},
"remote_components": ["ejs:github"],
}
self._last_error: str = ""
def _build_ydl_opts(self, browser_idx: int = -1) -> Dict[str, Any]:
"""构建 yt-dlp 选项(包含 Cookie 配置)
Args:
browser_idx: 浏览器列表索引,-1 表示仅使用 cookies_file
Returns:
yt-dlp 选项字典
"""
opts = self._base_opts.copy()
# 每次构建都刷新 User-Agent
opts["http_headers"] = {"User-Agent": get_random_ua()}
# Cookie 文件优先
if self._cookies_file and Path(self._cookies_file).exists():
opts["cookiefile"] = self._cookies_file
elif 0 <= browser_idx < len(self._browsers):
browser = self._browsers[browser_idx]
opts["cookiesfrombrowser"] = (browser,)
return opts
def _try_extract_info(self, url: str):
"""调用 yt-dlp extract_info,Cookie 失败时自动回退到下一个浏览器
回退流程:
1. 优先使用 cookies_file(如果存在)
2. 依次尝试浏览器列表中的每个浏览器
3. 遇到 Cookie 相关错误时自动跳到下一个浏览器
4. 成功后缓存工作浏览器,后续调用直接使用
5. 全部失败则抛出最后一个错误
Args:
url: YouTube URL
Returns:
yt-dlp info dict
Raises:
RuntimeError: 所有浏览器都失败
"""
errors: List[str] = []
# 第一步:尝试 cookies_file
if self._cookies_file and Path(self._cookies_file).exists():
try:
opts = self._build_ydl_opts(browser_idx=-1)
with yt_dlp.YoutubeDL(opts) as ydl:
return ydl.extract_info(url, download=False)
except Exception as e:
msg = str(e)
logger.warning(f"Cookie 文件模式失败: {msg[:100]}")
errors.append(f"cookies_file: {msg[:120]}")
# 非 Cookie 错误也不 raise,继续回退到浏览器或无 Cookie 模式
# 第二步:依次尝试浏览器列表
for idx, browser in enumerate(self._browsers):
try:
opts = self._build_ydl_opts(browser_idx=idx)
with yt_dlp.YoutubeDL(opts) as ydl:
info = ydl.extract_info(url, download=False)
if info is not None:
logger.info(f"Cookie 来源: {browser}")
return info
except Exception as e:
msg = str(e)
logger.warning(
f"浏览器 {browser} Cookie 失败,尝试下一个: {msg[:100]}"
)
errors.append(f"{browser}: {msg[:120]}")
if not _is_cookie_error(msg) or idx + 1 >= len(self._browsers):
break # 非 Cookie 错误或已是最后一个浏览器,跳出循环
# 第三步(最终回退):不使用任何 Cookie 直接请求
logger.warning(
"Cookie 来源均失败或有错误,尝试无 Cookie 模式(可能触发 YouTube 验证)"
)
try:
opts = self._base_opts.copy()
opts["http_headers"] = {"User-Agent": get_random_ua()}
with yt_dlp.YoutubeDL(opts) as ydl:
return ydl.extract_info(url, download=False)
except Exception as e:
msg = str(e)
errors.append(f"no_cookies: {msg[:120]}")
error_summary = "; ".join(errors)
raise RuntimeError(f"所有方式均失败: {error_summary}")
def fetch_video_info(self, url: str) -> Optional[VideoInfo]:
"""获取视频基本信息
Args:
url: YouTube 视频 URL
Returns:
VideoInfo 对象,失败返回 None(可通过 last_error 获取错误详情)
"""
try:
info = self._try_extract_info(url)
if info is None:
msg = f"无法获取视频信息: {url}"
logger.error(msg)
self._last_error = msg
return None
return VideoInfo(
title=info.get("title", "未知标题"),
author=info.get("uploader", info.get("channel", "未知作者")),
duration=info.get("duration", 0) or 0,
upload_date=str(info.get("upload_date", "")) if info.get("upload_date") else "",
thumbnail_url=info.get("thumbnail", ""),
description=info.get("description", "") or "",
webpage_url=info.get("webpage_url", url),
view_count=info.get("view_count", 0) or 0,
like_count=info.get("like_count", 0) or 0,
)
except Exception as e:
msg = str(e)
logger.error(f"获取视频信息异常: {msg}")
self._last_error = self._format_error(msg)
return None
@property
def last_error(self) -> str:
"""获取最后一次错误的详细信息"""
return self._last_error
@staticmethod
def _format_error(msg: str) -> str:
"""格式化 yt-dlp 错误信息为用户友好的提示
Args:
msg: 原始错误信息
Returns:
格式化后的错误信息
"""
if "Sign in to confirm" in msg:
return (
"YouTube 要求验证身份。\n\n"
"解决方法:\n"
" 1. 设置 → 高级 → Cookie 文件 → 选择 cookies.txt\n"
" (推荐:使用浏览器扩展 Get cookies.txt LOCALLY 导出)\n"
" 2. 设置 → 高级 → 浏览器 Cookie → 选择浏览器\n"
" (注意:Chrome/Edge 运行时可能被锁定,请先关闭浏览器)\n\n"
"详见 yt-dlp 文档: https://git.io/JUwuq"
)
if "failed to load cookies" in msg.lower():
return (
f"无法读取浏览器 Cookie(已自动尝试多个浏览器)。\n\n"
f"可能原因:所有浏览器均在运行中导致数据库被锁定。\n\n"
f"解决方法:\n"
f" 1. 关闭浏览器后重试\n"
f" 2. 使用 Cookie 文件方式:设置 → 高级 → Cookie 文件\n"
f" (推荐:使用浏览器扩展 Get cookies.txt LOCALLY 导出)\n\n"
f"原始错误: {msg[:200]}"
)
if "所有方式均失败" in msg:
return (
"所有 Cookie 来源和无 Cookie 模式均失败。\n\n"
"建议:\n"
" 1. 使用 Cookie 文件(最可靠):\n"
" 设置 → 高级 → Cookie 文件 → 浏览...\n"
" (浏览器扩展 Get cookies.txt LOCALLY 导出)\n"
" 2. 在设置中关闭 Cookie 功能:\n"
" 设置 → 高级 → 浏览器 Cookie → 选择「不使用」\n"
" (部分视频可能仍可下载)\n\n"
f"详情: {msg[:200]}"
)
return msg[:500]
def fetch_formats(self, url: str) -> List[VideoFormat]:
"""获取视频所有可用格式
提取所有视频和音频格式,去重并排序。
Args:
url: YouTube 视频 URL
Returns:
VideoFormat 列表
"""
all_formats: List[VideoFormat] = []
seen_formats: Dict[str, set] = {
"video": set(),
"audio": set(),
}
try:
info = self._try_extract_info(url)
if info is None or "formats" not in info:
return all_formats
formats = info["formats"]
for fmt in formats:
if fmt is None:
continue
video_format = self._parse_single_format(fmt)
if video_format and self._is_unique_format(
video_format, seen_formats
):
all_formats.append(video_format)
# 按分辨率排序
all_formats.sort(key=lambda f: self._sort_key(f))
logger.info(f"解析到 {len(all_formats)} 个可用格式")
except Exception as e:
logger.error(f"获取格式列表异常: {e}")
return all_formats
def fetch_audio_formats(self, url: str) -> List[VideoFormat]:
"""获取仅音频格式列表
提取纯音频格式,适合只需要音频的用户。
Args:
url: YouTube 视频 URL
Returns:
仅包含音频的 VideoFormat 列表
"""
all_formats = self.fetch_formats(url)
return [f for f in all_formats if f.is_audio_only]
def fetch_video_only_formats(self, url: str) -> List[VideoFormat]:
"""获取仅视频格式列表(不含音频)
提取只有视频流的格式,适合需要手动合并高清视频的场景。
Args:
url: YouTube 视频 URL
Returns:
仅包含视频的 VideoFormat 列表
"""
all_formats = self.fetch_formats(url)
return [f for f in all_formats if f.has_video and not f.has_audio]
def fetch_combined_formats(self, url: str) -> List[VideoFormat]:
"""获取音视频合并格式列表
提取同时包含音频和视频的格式,可直接下载无需合并。
Args:
url: YouTube 视频 URL
Returns:
包含音视频的 VideoFormat 列表
"""
all_formats = self.fetch_formats(url)
return [f for f in all_formats if f.has_video and f.has_audio]
def fetch_subtitles(self, url: str) -> List[SubtitleInfo]:
"""获取可用字幕列表
Args:
url: YouTube 视频 URL
Returns:
SubtitleInfo 列表
"""
subtitles: List[SubtitleInfo] = []
# 语言名称映射
language_names = {
"zh": "中文", "zh-Hans": "中文(简体)", "zh-Hant": "中文(繁体)",
"en": "English", "ja": "日本語", "ko": "한국어",
"fr": "Français", "de": "Deutsch", "es": "Español",
"ru": "Русский", "pt": "Português", "ar": "العربية",
"th": "ไทย", "vi": "Tiếng Việt", "id": "Bahasa Indonesia",
}
try:
info = self._try_extract_info(url)
if info is None:
return subtitles
# 自动字幕
auto_subs = info.get("automatic_captions", {}) or {}
for lang_code, subs in auto_subs.items():
for sub in subs:
if sub.get("ext") in ("vtt", "srt", "ttml"):
subtitles.append(SubtitleInfo(
language=lang_code,
language_name=language_names.get(
lang_code, lang_code
),
is_auto=True,
ext=sub.get("ext", "vtt"),
url=sub.get("url", ""),
))
# 手动字幕 (官方字幕)
manual_subs = info.get("subtitles", {}) or {}
for lang_code, subs in manual_subs.items():
for sub in subs:
if sub.get("ext") in ("vtt", "srt", "ttml"):
subtitles.append(SubtitleInfo(
language=lang_code,
language_name=language_names.get(
lang_code, lang_code
),
is_auto=False,
ext=sub.get("ext", "vtt"),
url=sub.get("url", ""),
))
logger.info(f"解析到 {len(subtitles)} 个字幕")
except Exception as e:
logger.error(f"获取字幕列表异常: {e}")
return subtitles
def _parse_single_format(self, fmt: dict) -> Optional[VideoFormat]:
"""解析单个格式条目
Args:
fmt: yt-dlp 返回的格式字典
Returns:
VideoFormat 对象,无法解析时返回 None
"""
try:
# 基本信息
format_id = fmt.get("format_id", "")
ext = fmt.get("ext", "")
resolution = fmt.get("resolution", "") or ""
fps = fmt.get("fps") or 0
file_size = fmt.get("filesize") or fmt.get("filesize_approx") or 0
# 编码信息
vcodec = fmt.get("vcodec", "") or ""
acodec = fmt.get("acodec", "") or ""
# 判断类型
has_video = vcodec != "none" and vcodec != ""
has_audio = acodec != "none" and acodec != ""
is_audio_only = not has_video and has_audio
# 比特率
tbr = fmt.get("tbr") or 0.0
vbr = fmt.get("vbr") or 0.0
abr = fmt.get("abr") or 0.0
# 格式备注
note = fmt.get("format_note", "") or ""
# 如果不是视频也不是音频,跳过
if not has_video and not has_audio:
return None
return VideoFormat(
format_id=format_id,
ext=ext,
resolution=resolution,
video_codec=vcodec.split(".")[0] if vcodec else "",
audio_codec=acodec.split(".")[0] if acodec else "",
fps=int(fps) if fps else 0,
file_size=int(file_size) if file_size else 0,
tbr=float(tbr),
vbr=float(vbr),
abr=float(abr),
has_video=has_video,
has_audio=has_audio,
is_audio_only=is_audio_only,
note=note,
)
except Exception as e:
logger.debug(f"解析格式条目失败: {e}")
return None
def _is_unique_format(self, fmt: VideoFormat, seen: Dict[str, set]) -> bool:
"""检查格式是否唯一(去重)
按分辨率和扩展名组合去重,避免显示重复格式。
Args:
fmt: 待检查的格式
seen: 已见过的格式记录
Returns:
是否为新的唯一格式
"""
if fmt.is_audio_only:
key = f"audio_{fmt.ext}_{fmt.audio_codec}"
category = "audio"
else:
key = f"video_{fmt.resolution}_{fmt.ext}_{fmt.video_codec}"
category = "video"
if key in seen[category]:
return False
seen[category].add(key)
return True
def _sort_key(self, fmt: VideoFormat) -> tuple:
"""生成格式排序键值
音频格式排在视频格式之后,视频按分辨率升序排列。
Args:
fmt: 视频格式
Returns:
排序元组 (是否音频, 分辨率数值)
"""
is_audio = 1 if fmt.is_audio_only else 0
# 解析分辨率
resolution_int = 0
if fmt.resolution:
try:
resolution_int = int(fmt.resolution.lower().replace("p", ""))
except ValueError:
pass
return (is_audio, resolution_int)
def get_best_video_format(self, formats: List[VideoFormat]) -> Optional[VideoFormat]:
"""获取最佳视频格式(带音频的最高分辨率)
Args:
formats: 可用格式列表
Returns:
最佳 VideoFormat,无可用格式时返回 None
"""
combined = self.fetch_combined_formats_from_list(formats)
if combined:
return combined[-1] # 已排序,最后一个为最高分辨率
return None
def get_best_audio_format(self, formats: List[VideoFormat]) -> Optional[VideoFormat]:
"""获取最佳音频格式
Args:
formats: 可用格式列表
Returns:
最佳音频 VideoFormat,无可用格式时返回 None
"""
audio_formats = [f for f in formats if f.is_audio_only]
if not audio_formats:
return None
# 按比特率排序,返回最高质量
audio_formats.sort(key=lambda f: f.abr, reverse=True)
return audio_formats[0]
@staticmethod
def fetch_combined_formats_from_list(
formats: List[VideoFormat]
) -> List[VideoFormat]:
"""从已有格式列表中筛选合并格式(静态方法)
Args:
formats: 格式列表
Returns:
同时包含音视频的格式
"""
return [f for f in formats if f.has_video and f.has_audio]
+222
View File
@@ -0,0 +1,222 @@
"""播放列表下载器模块
负责解析 YouTube 播放列表,获取所有视频条目信息,
支持单个或批量下载。
"""
import logging
from pathlib import Path
from typing import List, Optional, Dict, Any
import yt_dlp
from ..models.task import VideoInfo
from ..utils.user_agents import get_random_ua
from .format_parser import (
FormatParser, _is_cookie_error, _parse_browser_list,
)
logger = logging.getLogger(__name__)
class PlaylistDownloader:
"""播放列表下载器
负责解析 YouTube 播放列表 URL,提取所有视频条目的基本信息。
实际下载通过 VideoDownloader 执行。
Cookie 回退机制:当配置的浏览器 Cookie 读取失败时,
自动尝试列表中的下一个浏览器。
"""
def __init__(self, cookies_from_browser: str = "", cookies_file: str = "") -> None:
"""初始化播放列表下载器
Args:
cookies_from_browser: 浏览器名称(或逗号分隔的多个),如 "chrome,firefox"
cookies_file: Cookie 文件路径(Netscape 格式),优先于浏览器 Cookie
"""
self._cookies_file = cookies_file
self._browsers = _parse_browser_list(cookies_from_browser)
self._base_opts: Dict[str, Any] = {
"quiet": True,
"no_warnings": True,
"extract_flat": "in_playlist",
"skip_download": True,
"ignoreerrors": True,
"socket_timeout": 30,
"http_headers": {"User-Agent": get_random_ua()},
}
self._last_error: str = ""
def _build_ydl_opts(self, browser_idx: int = -1) -> Dict[str, Any]:
"""构建 yt-dlp 选项(包含 Cookie 配置)"""
opts = self._base_opts.copy()
opts["http_headers"] = {"User-Agent": get_random_ua()}
if self._cookies_file and Path(self._cookies_file).exists():
opts["cookiefile"] = self._cookies_file
elif 0 <= browser_idx < len(self._browsers):
browser = self._browsers[browser_idx]
opts["cookiesfrombrowser"] = (browser,)
return opts
def _try_extract_info(self, url: str, extra_opts: Dict[str, Any] | None = None):
"""调用 yt-dlp extract_info,Cookie 失败时自动回退
Args:
url: YouTube URL
extra_opts: 额外的 yt-dlp 选项
Returns:
yt-dlp info dict
Raises:
RuntimeError: 所有方式都失败
"""
errors: List[str] = []
def _call_with_opts(opts, use_base=False):
if use_base:
merged = self._base_opts.copy()
merged["http_headers"] = {"User-Agent": get_random_ua()}
else:
merged = opts.copy()
if extra_opts:
merged.update(extra_opts)
with yt_dlp.YoutubeDL(merged) as ydl:
return ydl.extract_info(url, download=False)
# 第一步:cookies_file
if self._cookies_file and Path(self._cookies_file).exists():
try:
return _call_with_opts(self._build_ydl_opts(browser_idx=-1))
except Exception as e:
msg = str(e)
logger.warning(f"Cookie 文件模式失败: {msg[:100]}")
errors.append(f"cookies_file: {msg[:120]}")
# 非 Cookie 错误也不 raise,继续回退
# 第二步:依次尝试每个浏览器
for idx, browser in enumerate(self._browsers):
try:
info = _call_with_opts(self._build_ydl_opts(browser_idx=idx))
if info is not None:
logger.info(f"播放列表 Cookie 来源: {browser}")
return info
except Exception as e:
msg = str(e)
logger.warning(
f"浏览器 {browser} Cookie 失败: {msg[:100]}"
)
errors.append(f"{browser}: {msg[:120]}")
if not _is_cookie_error(msg) or idx + 1 >= len(self._browsers):
break # 非 Cookie 错误或已是最后一个浏览器
# 第三步(最终回退):不使用任何 Cookie 直接请求
logger.warning(
"Cookie 来源均失败或有错误,尝试无 Cookie 模式"
)
try:
return _call_with_opts(None, use_base=True)
except Exception as e:
msg = str(e)
errors.append(f"no_cookies: {msg[:120]}")
error_summary = "; ".join(errors)
raise RuntimeError(f"所有方式均失败: {error_summary}")
def fetch_playlist_info(
self, url: str, max_videos: int = 0
) -> List[VideoInfo]:
"""获取播放列表中的所有视频信息
Args:
url: 播放列表 URL
max_videos: 最大获取视频数,0 表示不限制
Returns:
VideoInfo 列表,包含播放列表中每个视频的基本信息
"""
videos: List[VideoInfo] = []
try:
extra = {}
if max_videos > 0:
extra["playlist_items"] = f"1-{max_videos}"
info = self._try_extract_info(url, extra_opts=extra)
if info is None:
logger.error(f"无法获取播放列表信息: {url}")
return videos
# 获取播放列表标题
playlist_title = info.get("title", "未命名播放列表")
logger.info(f"播放列表: {playlist_title}")
# 提取所有视频条目
entries = info.get("entries") or []
# 如果 flat 提取为空,尝试完整提取
if not entries:
logger.info("flat 提取为空,尝试完整提取...")
extra["extract_flat"] = False
info2 = self._try_extract_info(url, extra_opts=extra)
if info2:
entries = info2.get("entries") or []
for entry in entries:
if entry is None:
continue
video_url = entry.get("webpage_url") or entry.get("url") or ""
if not video_url and entry.get("id"):
video_url = f"https://www.youtube.com/watch?v={entry['id']}"
video_info = VideoInfo(
title=entry.get("title", "未知标题"),
author=entry.get("uploader", entry.get("channel", "未知作者")),
duration=entry.get("duration", 0) or 0,
upload_date=str(entry.get("upload_date", "")) if entry.get("upload_date") else "",
thumbnail_url=entry.get("thumbnail") or entry.get("thumbnails", [{}])[0].get("url", ""),
description=entry.get("description", "") or "",
webpage_url=video_url,
)
videos.append(video_info)
logger.info(f"播放列表解析完成,共 {len(videos)} 个视频")
return videos
except yt_dlp.utils.DownloadError as e:
msg = str(e)
logger.error(f"yt-dlp 播放列表解析错误: {msg}")
self._last_error = FormatParser._format_error(msg)
return videos
except Exception as e:
msg = str(e)
logger.error(f"播放列表解析异常: {msg}")
self._last_error = FormatParser._format_error(msg)
return videos
@property
def last_error(self) -> str:
"""获取最后一次错误的详细信息"""
return self._last_error
def fetch_playlist_title(self, url: str) -> str:
"""仅获取播放列表标题
Args:
url: 播放列表 URL
Returns:
播放列表标题,失败返回空字符串
"""
try:
info = self._try_extract_info(url)
return info.get("title", "未命名播放列表") if info else ""
except Exception as e:
logger.error(f"获取播放列表标题失败: {e}")
return ""
+173
View File
@@ -0,0 +1,173 @@
"""字幕下载器模块
支持从 YouTube 下载自动生成字幕和官方手动字幕,
支持多种语言和格式(SRT、VTT)。
"""
import logging
import os
from pathlib import Path
from typing import Optional, List
import yt_dlp
import requests
from ..models.task import SubtitleInfo
from ..utils.user_agents import get_headers
logger = logging.getLogger(__name__)
class SubtitleDownloader:
"""字幕下载器
负责下载 YouTube 视频的字幕文件,
支持自动字幕、官方字幕、多种语言和格式。
"""
# 支持的语言列表
SUPPORTED_LANGUAGES = {
"zh": "中文",
"en": "英文",
"ja": "日文",
"ko": "韩文",
"auto": "自动检测",
}
# 支持的格式
SUPPORTED_FORMATS = ["srt", "vtt"]
def __init__(self) -> None:
"""初始化字幕下载器"""
self._ydl_opts = {
"quiet": True,
"no_warnings": True,
"skip_download": True,
"ignoreerrors": True,
}
def download_subtitle(
self,
url: str,
output_dir: str,
language: str = "zh",
subtitle_format: str = "srt",
auto_subtitle: bool = True,
output_filename: str = "",
) -> Optional[str]:
"""下载字幕文件
Args:
url: YouTube 视频 URL
output_dir: 输出目录
language: 字幕语言代码
subtitle_format: 字幕格式 (srt 或 vtt)
auto_subtitle: 是否使用自动字幕
output_filename: 输出文件名(不含扩展名),为空则使用视频标题
Returns:
下载成功的字幕文件路径,失败返回 None
"""
try:
Path(output_dir).mkdir(parents=True, exist_ok=True)
# 获取视频信息以确定文件名
with yt_dlp.YoutubeDL(self._ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
if info is None:
logger.error("无法获取视频信息用于字幕下载")
return None
video_title = info.get("title", "video")
safe_title = self._sanitize_filename(video_title)
# 构建输出文件路径
if not output_filename:
output_filename = safe_title
output_path = Path(output_dir) / f"{output_filename}.{subtitle_format}"
# 构建字幕下载专用的 yt-dlp 选项
ydl_opts = {
"quiet": True,
"no_warnings": True,
"skip_download": True,
"writesubtitles": True,
"writeautomaticsub": auto_subtitle,
"subtitleslangs": [language],
"subtitlesformat": subtitle_format,
"outtmpl": str(Path(output_dir) / f"{output_filename}.%(ext)s"),
"ignoreerrors": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# 检查字幕文件是否生成
if output_path.exists():
logger.info(f"字幕下载成功: {output_path}")
return str(output_path)
# 尝试查找其他匹配的字幕文件
for f in Path(output_dir).glob(f"{output_filename}*.{subtitle_format}"):
logger.info(f"找到字幕文件: {f}")
return str(f)
logger.warning(f"未找到字幕文件: {output_path}")
return None
except Exception as e:
logger.error(f"字幕下载失败: {e}")
return None
def download_subtitle_from_url(
self,
subtitle_url: str,
output_path: str,
) -> bool:
"""直接从字幕 URL 下载
用于已获取字幕 URL 的情况。
Args:
subtitle_url: 字幕文件 URL
output_path: 输出文件完整路径
Returns:
下载成功返回 True
"""
try:
response = requests.get(
subtitle_url, timeout=30, headers=get_headers()
)
response.raise_for_status()
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "wb") as f:
f.write(response.content)
logger.info(f"字幕下载成功 (直链): {output_path}")
return True
except requests.RequestException as e:
logger.error(f"直链字幕下载失败: {e}")
return False
@staticmethod
def _sanitize_filename(filename: str) -> str:
"""清理文件名
Args:
filename: 原始文件名
Returns:
清理后的文件名
"""
illegal_chars = r'[<>:"/\\|?*\x00-\x1f]'
import re
sanitized = re.sub(illegal_chars, "_", filename)
sanitized = sanitized.strip(". ")
if len(sanitized) > 100:
sanitized = sanitized[:100]
return sanitized or "video"
+170
View File
@@ -0,0 +1,170 @@
"""封面下载器模块
负责下载 YouTube 视频封面/缩略图,
支持 JPG 和 PNG 格式,可自定义输出路径。
"""
import logging
import os
from pathlib import Path
from typing import Optional
import requests
import yt_dlp
from ..utils.user_agents import get_headers
logger = logging.getLogger(__name__)
class ThumbnailDownloader:
"""封面图下载器
负责下载 YouTube 视频的缩略图/封面图,
支持从 yt-dlp 提取或直接 HTTP 下载。
"""
# YouTube 缩略图质量后缀
QUALITY_SUFFIXES = [
"maxresdefault", # 最高分辨率 (1280x720)
"sddefault", # 标准分辨率 (640x480)
"hqdefault", # 高质量 (480x360)
"mqdefault", # 中等质量 (320x180)
"default", # 默认 (120x90)
]
def __init__(self) -> None:
"""初始化封面下载器"""
self._ydl_opts = {
"quiet": True,
"no_warnings": True,
"skip_download": True,
}
def download_thumbnail(
self,
url: str,
output_dir: str,
output_format: str = "jpg",
output_filename: str = "",
) -> Optional[str]:
"""下载视频封面图
Args:
url: YouTube 视频 URL
output_dir: 输出目录
output_format: 图片格式 (jpg 或 png)
output_filename: 输出文件名(不含扩展名),为空则使用视频标题
Returns:
下载成功的图片文件路径,失败返回 None
"""
try:
# 获取视频信息
with yt_dlp.YoutubeDL(self._ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
if info is None:
logger.error("无法获取视频信息用于封面下载")
return None
thumbnail_url = info.get("thumbnail", "")
video_title = info.get("title", "video")
if not thumbnail_url:
logger.error(f"未找到封面图 URL: {url}")
return None
# 确保输出目录存在
Path(output_dir).mkdir(parents=True, exist_ok=True)
# 确定输出文件名
if not output_filename:
safe_title = self._sanitize_filename(video_title)
output_filename = f"{safe_title}_cover"
output_path = Path(output_dir) / f"{output_filename}.{output_format}"
# 下载封面图
return self._download_image(thumbnail_url, str(output_path))
except Exception as e:
logger.error(f"封面下载失败: {e}")
return None
def download_thumbnail_hq(
self,
video_id: str,
output_path: str,
) -> Optional[str]:
"""下载高质量封面(使用 video_id)
直接通过 YouTube 图片服务器下载最高质量封面。
Args:
video_id: YouTube 视频 ID (11 位字符)
output_path: 输出文件完整路径
Returns:
下载成功的文件路径,失败返回 None
"""
# 尝试从高到低质量下载
for quality in self.QUALITY_SUFFIXES:
thumbnail_url = (
f"https://img.youtube.com/vi/{video_id}/{quality}.jpg"
)
result = self._download_image(thumbnail_url, output_path)
if result:
return result
logger.error(f"所有质量的封面图均下载失败: {video_id}")
return None
def _download_image(self, image_url: str, output_path: str) -> Optional[str]:
"""通过 HTTP 下载图片
Args:
image_url: 图片 URL
output_path: 输出文件完整路径
Returns:
下载成功的文件路径,失败返回 None
"""
try:
response = requests.get(
image_url, timeout=30, headers=get_headers()
)
response.raise_for_status()
# 确保输出目录存在
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "wb") as f:
f.write(response.content)
file_size = os.path.getsize(output_path)
if file_size < 1000: # 文件太小,可能不是有效图片
logger.warning(f"封面图文件可能无效 (大小: {file_size} bytes)")
os.remove(output_path)
return None
logger.info(f"封面下载成功: {output_path} ({file_size} bytes)")
return output_path
except requests.RequestException as e:
logger.debug(f"封面下载 HTTP 错误 {image_url}: {e}")
return None
except Exception as e:
logger.error(f"封面下载异常: {e}")
return None
@staticmethod
def _sanitize_filename(filename: str) -> str:
"""清理文件名"""
import re
illegal_chars = r'[<>:"/\\|?*\x00-\x1f]'
sanitized = re.sub(illegal_chars, "_", filename)
sanitized = sanitized.strip(". ")
if len(sanitized) > 100:
sanitized = sanitized[:100]
return sanitized or "video"
+385
View File
@@ -0,0 +1,385 @@
"""视频下载器模块
使用 yt-dlp 实现 YouTube 视频下载,支持:
- 视频/音频下载
- 下载进度回调
- 多线程安全
- 错误重试机制
"""
import logging
import os
import threading
from pathlib import Path
from typing import Optional, Callable, Dict, Any
import yt_dlp
from ..models.task import DownloadTask, DownloadProgress, TaskStatus
from ..utils.user_agents import get_random_ua
from .ffmpeg_merger import FFmpegMerger
logger = logging.getLogger(__name__)
class VideoDownloader:
"""视频下载器
封装 yt-dlp 下载逻辑,提供进度回调和状态管理。
每个下载任务在独立线程中执行。
"""
def __init__(self, ffmpeg_path: str = "") -> None:
"""初始化视频下载器
Args:
ffmpeg_path: FFmpeg 路径,用于合并音视频
"""
self._ffmpeg_path = ffmpeg_path
self._merger = FFmpegMerger(ffmpeg_path)
self._progress_hook: Optional[Callable] = None
# 下载状态(线程安全)
self._cancel_flag = threading.Event()
self._pause_flag = threading.Event()
self._current_task: Optional[DownloadTask] = None
@property
def is_ffmpeg_available(self) -> bool:
"""FFmpeg 是否可用"""
return self._merger.is_available
def set_progress_hook(
self, hook: Optional[Callable[[DownloadProgress, str], None]]
) -> None:
"""设置进度回调
Args:
hook: 回调函数,接收 DownloadProgress 和状态消息
"""
self._progress_hook = hook
def cancel(self) -> None:
"""取消当前下载"""
self._cancel_flag.set()
logger.info("下载取消请求已发送")
def pause(self) -> None:
"""暂停当前下载"""
self._pause_flag.set()
logger.info("下载暂停请求已发送")
def resume(self) -> None:
"""恢复当前下载"""
self._pause_flag.clear()
logger.info("下载恢复")
def download_video(self, task: DownloadTask) -> bool:
"""下载视频
根据任务配置下载视频/音频,对于需要合并的高清视频自动处理。
Args:
task: 下载任务对象
Returns:
下载成功返回 True,失败返回 False
"""
self._cancel_flag.clear()
self._pause_flag.clear()
self._current_task = task
task.status = TaskStatus.DOWNLOADING
task.started_at = task.created_at # 使用创建时间作为开始时间
try:
# 构建 yt-dlp 选项
ydl_opts = self._build_ydl_options(task)
# 执行下载
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([task.url])
# 检查是否被取消
if self._cancel_flag.is_set():
task.status = TaskStatus.CANCELLED
self._cleanup_partial_download(task)
return False
# 设置完成状态
task.status = TaskStatus.COMPLETED
task.progress.percent = 100.0
logger.info(f"下载完成: {task.video_info.title if task.video_info else task.url}")
return True
except yt_dlp.utils.DownloadError as e:
logger.error(f"yt-dlp 下载错误: {e}")
if self._cancel_flag.is_set():
task.status = TaskStatus.CANCELLED
else:
task.status = TaskStatus.FAILED
task.error_message = str(e)[:500]
self._cleanup_partial_download(task)
return False
except Exception as e:
logger.error(f"下载异常: {e}")
task.status = TaskStatus.FAILED
task.error_message = str(e)[:500]
self._cleanup_partial_download(task)
return False
def download_audio(self, task: DownloadTask) -> bool:
"""仅下载音频
Args:
task: 下载任务对象
Returns:
下载成功返回 True,失败返回 False
"""
# 设置为仅音频模式
task.download_audio = True
task.download_video = False
return self.download_video(task)
def _build_ydl_options(self, task: DownloadTask) -> Dict[str, Any]:
"""构建 yt-dlp 下载选项
Args:
task: 下载任务对象
Returns:
yt-dlp 选项字典
"""
# 确定输出目录
output_dir = task.output_dir or str(
Path.home() / "Downloads" / "YouTube"
)
# 确定输出文件名模板
filename_template = "%(title)s.%(ext)s"
# 构建输出路径模板
outtmpl = str(Path(output_dir) / filename_template)
options: Dict[str, Any] = {
"outtmpl": outtmpl,
"quiet": True,
"no_warnings": True,
"ignoreerrors": False,
"socket_timeout": 30,
"retries": 5,
"fragment_retries": 5,
"http_headers": {"User-Agent": get_random_ua()},
"progress_hooks": [self._on_progress],
"postprocessor_hooks": [self._on_postprocessing],
# 启用 Node.js 作为 JS 运行时 + 远程挑战求解器
"js_runtimes": {"node": {}, "deno": {}},
"remote_components": ["ejs:github"],
}
# 格式选择逻辑
if task.download_audio and not task.download_video:
# 仅下载音频
audio_format = task.selected_format
if audio_format and audio_format.format_id:
options["format"] = audio_format.format_id
else:
options["format"] = "bestaudio/best"
if self._merger.is_available:
# FFmpeg 可用:转码为 MP3
options["postprocessors"] = [{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
}]
else:
# FFmpeg 不可用:直接下载原始音频格式(不转码)
logger.warning(
"FFmpeg 不可用,音频将保存为原始格式(不转码为 MP3)"
)
elif task.selected_format and task.selected_format.format_id:
# 用户选择的特定格式
fmt = task.selected_format
if fmt.has_video and not fmt.has_audio:
# 仅视频流,需要下载音频流并合并
if self._merger.is_available:
options["format"] = (
f"{fmt.format_id}+bestaudio/best"
)
options["merge_output_format"] = "mp4"
# yt-dlp 设置 merge_output_format 后会自动使用 FFmpegMergerPP 合并
else:
# FFmpeg 不可用:完全避免 + 合并语法,仅使用预合并格式
# best[height<=X] 选择该分辨率下已包含音频的格式
fmt_selector = (
f"best[height<={fmt.resolution_int}][ext=mp4]/"
f"best[height<={fmt.resolution_int}]/"
f"best[ext=mp4]/best"
)
options["format"] = fmt_selector
logger.warning(
"FFmpeg 不可用,回退到预合并格式 (≤%sp): %s",
fmt.resolution_int, fmt_selector,
)
else:
# 完整格式(含音频)或纯音频
options["format"] = fmt.format_id
else:
# 默认最佳格式
if self._merger.is_available:
# FFmpeg 可用:下载最佳视频+音频流并合并
options["format"] = (
"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"
)
options["merge_output_format"] = "mp4"
else:
# FFmpeg 不可用:直接下载已合并的最佳格式(通常上限 720p)
options["format"] = (
"best[ext=mp4]/best[ext=webm]/best"
)
logger.warning("FFmpeg 不可用,仅下载内置合并格式(最高 720p)")
# 字幕下载选项
if task.download_subtitle:
options["writesubtitles"] = True
options["writeautomaticsub"] = task.subtitle_auto
options["subtitleslangs"] = [task.subtitle_language]
options["subtitlesformat"] = task.subtitle_format
# 封面下载
if task.download_thumbnail:
options["writethumbnail"] = True
# FFmpeg 路径(如果不为空)
if self._ffmpeg_path and os.path.exists(self._ffmpeg_path):
options["ffmpeg_location"] = self._ffmpeg_path
# Cookie 设置(Cookie 文件优先于浏览器 Cookie)
if task.extra_data.get("cookies_file"):
options["cookiefile"] = task.extra_data["cookies_file"]
elif task.extra_data.get("cookies_from_browser"):
options["cookiesfrombrowser"] = (
task.extra_data["cookies_from_browser"],
)
# 代理设置
if task.extra_data.get("proxy"):
options["proxy"] = task.extra_data["proxy"]
# 限速设置
if task.extra_data.get("rate_limit"):
options["ratelimit"] = task.extra_data["rate_limit"]
return options
def _on_progress(self, d: Dict[str, Any]) -> None:
"""yt-dlp 下载进度回调
Args:
d: yt-dlp 进度数据字典
"""
if self._cancel_flag.is_set():
raise yt_dlp.utils.DownloadCancelled("用户取消下载")
# 等待暂停恢复
while self._pause_flag.is_set():
import time
time.sleep(0.1)
if self._cancel_flag.is_set():
raise yt_dlp.utils.DownloadCancelled("用户取消下载")
if self._current_task is None or self._progress_hook is None:
return
status = d.get("status", "")
if status == "downloading":
downloaded = d.get("downloaded_bytes", 0) or 0
total = (
d.get("total_bytes")
or d.get("total_bytes_estimate")
or 0
)
speed = d.get("speed") or 0.0
eta = d.get("eta") or 0
# 计算百分比
percent = (downloaded / total * 100) if total > 0 else 0.0
progress = DownloadProgress(
downloaded_bytes=downloaded,
total_bytes=total,
speed=speed,
percent=percent,
eta=eta,
status="下载中",
)
self._current_task.progress = progress
self._progress_hook(progress, "下载中")
elif status == "finished":
# 下载完成,可能还需后处理
if self._current_task:
self._current_task.progress.percent = 99.0
self._progress_hook(
self._current_task.progress, "处理中..."
)
elif status == "error":
logger.error("下载出错")
def _on_postprocessing(self, d: Dict[str, Any]) -> None:
"""yt-dlp 后处理回调(如合并、转码)
Args:
d: 后处理数据字典
"""
if self._current_task is None or self._progress_hook is None:
return
postprocessor = d.get("postprocessor", "")
status = d.get("status", "")
if postprocessor == "MoveFiles" and status == "started":
pass
elif postprocessor == "MoveFiles" and status == "finished":
self._current_task.progress.percent = 100.0
elif postprocessor in ("FFmpegMergerPP", "FFmpegMergeVideo") and status == "started":
self._current_task.status = TaskStatus.MERGING
self._progress_hook(
self._current_task.progress, "正在合并音视频..."
)
elif postprocessor in ("FFmpegMergerPP", "FFmpegMergeVideo") and status == "finished":
self._progress_hook(
self._current_task.progress, "合并完成"
)
def _cleanup_partial_download(self, task: DownloadTask) -> None:
"""清理不完整的下载文件
Args:
task: 下载任务
"""
# yt-dlp 通常使用 .part 或 .ytdl 扩展名标记未完成下载
output_dir = task.output_dir or str(
Path.home() / "Downloads" / "YouTube"
)
try:
if task.video_info:
# 尝试清理匹配标题的临时文件
clean_title = task.video_info.title.replace("/", "_")[:50]
for ext in [".part", ".ytdl", ".tmp"]:
for f in Path(output_dir).glob(f"*{clean_title}*{ext}"):
try:
f.unlink()
except OSError:
pass
except Exception:
pass