Files
BiliNote/backend/app/utils/video_helper.py
黄建武 04dad3b72a feat(downloaders): 添加抖音视频识别功能
- 新增 abogus.py 文件,实现 a_bogus 参数的生成逻辑
- 代码源自 JoeanAmier/TikTokDownloader 项目,并进行了适配和优化
- 功能包括生成用户代理字符串、加密 URL 参数和生成最终的 a_bogus值
- 提供了详细的注释和函数说明,便于理解和维护
2025-05-02 14:00:29 +08:00

70 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import shutil
from pathlib import Path
from dotenv import load_dotenv
import subprocess
import os
import uuid
load_dotenv()
api_path = os.getenv("API_BASE_URL", "http://localhost")
BACKEND_PORT= os.getenv("BACKEND_PORT", 8000)
BACKEND_BASE_URL = f"{api_path}:{BACKEND_PORT}"
from typing import Optional
def generate_screenshot(video_path: str, output_dir: str, timestamp: int, index: int) -> str:
"""
使用 ffmpeg 生成截图,返回生成图片路径
"""
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
filename = f"screenshot_{index:03}_{uuid.uuid4()}.jpg"
output_path = output_dir / filename
command = [
"ffmpeg",
"-ss", str(timestamp),
"-i", str(video_path),
"-frames:v", "1",
"-q:v", "2",
str(output_path),
"-y"
]
print("Running command:", command)
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode != 0:
print("ffmpeg failed:", result.stderr)
return str(output_path)
def save_cover_to_static(local_cover_path: str, subfolder: Optional[str] = "cover") -> str:
"""
将封面图片保存到 static 目录下,并返回前端可访问的路径
:param local_cover_path: 本地原封面路径比如提取出来的jpg
:param subfolder: 子目录,默认是 cover可以自定义
:return: 前端访问路径,例如 /static/cover/xxx.jpg
"""
# 项目根目录
project_root = os.getcwd()
# static目录
static_dir = os.path.join(project_root, "static")
# 确定目标子目录
target_dir = os.path.join(static_dir, subfolder or "cover")
os.makedirs(target_dir, exist_ok=True)
# 拷贝文件
file_name = os.path.basename(local_cover_path)
target_path = os.path.join(target_dir, file_name)
shutil.copy2(local_cover_path, target_path) # 保留原时间戳、权限
image_relative_path = f"/static/{subfolder}/{file_name}".replace("\\", "/")
url_path = f"{BACKEND_BASE_URL.rstrip('/')}/{image_relative_path.lstrip('/')}"
# 返回前端可访问的路径
return url_path