feat: add download_url field to generated video APIs (#20)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
视频处理器模块
|
||||
视频处理核心类
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -13,7 +13,7 @@ import ffmpeg
|
||||
|
||||
@dataclass
|
||||
class VideoResult:
|
||||
"""视频结果"""
|
||||
"""视频生成结果"""
|
||||
|
||||
output_path: str
|
||||
thumbnail_path: str
|
||||
@@ -29,10 +29,10 @@ class VideoProcessor:
|
||||
|
||||
def __init__(self, temp_dir: str = None):
|
||||
"""
|
||||
初始化处理器
|
||||
初始化视频处理器
|
||||
|
||||
Args:
|
||||
temp_dir: 临时目录路径,默认为系统临时目录
|
||||
temp_dir: 临时文件目录,默认使用系统临时目录
|
||||
"""
|
||||
self.temp_dir = temp_dir or tempfile.gettempdir()
|
||||
|
||||
@@ -44,41 +44,44 @@ class VideoProcessor:
|
||||
fps: int = 25,
|
||||
) -> VideoResult:
|
||||
"""
|
||||
合并多个视频
|
||||
拼接多个视频
|
||||
|
||||
Args:
|
||||
input_paths: 输入视频路径列表
|
||||
output_path: 输出视频路径
|
||||
resolution: 分辨率 (width, height)
|
||||
fps: 帧率
|
||||
resolution: 输出分辨率 (width, height)
|
||||
fps: 输出帧率
|
||||
|
||||
Returns:
|
||||
VideoResult: 视频结果
|
||||
VideoResult: 生成结果
|
||||
"""
|
||||
if not input_paths:
|
||||
raise ValueError("input_paths cannot be empty")
|
||||
|
||||
# 创建输出目录
|
||||
# 确保输出目录存在
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
# P2-6 Fix: 使用 try-finally 确保临时文件清理
|
||||
concat_file = None
|
||||
try:
|
||||
# P2-6: Use NamedTemporaryFile for automatic cleanup
|
||||
concat_file_fd = tempfile.NamedTemporaryFile(
|
||||
mode='w',
|
||||
suffix='.txt',
|
||||
# 使用 NamedTemporaryFile 确保临时文件正确清理
|
||||
concat_file = tempfile.NamedTemporaryFile(
|
||||
mode="w",
|
||||
suffix=".txt",
|
||||
prefix="ffmpeg_concat_",
|
||||
dir=self.temp_dir,
|
||||
delete=True,
|
||||
dir=self.temp_dir
|
||||
)
|
||||
with concat_file_fd as concat_file:
|
||||
for path in input_paths:
|
||||
# FFmpeg concat demuxer 要求绝对路径
|
||||
concat_file.write(f"file '{os.path.abspath(path)}'\n")
|
||||
# Flush is automatic with context manager
|
||||
for path in input_paths:
|
||||
# FFmpeg concat demuxer 格式
|
||||
concat_file.write(f"file '{os.path.abspath(path)}'\n")
|
||||
concat_file.flush()
|
||||
concat_file_path = concat_file.name
|
||||
|
||||
# 使用 FFmpeg 合并
|
||||
# 使用 FFmpeg 拼接视频
|
||||
width, height = resolution
|
||||
(
|
||||
ffmpeg.input(concat_file.name, format="concat", safe=0)
|
||||
ffmpeg.input(concat_file_path, format="concat", safe=0)
|
||||
.output(
|
||||
output_path,
|
||||
vcodec="libx264",
|
||||
@@ -92,16 +95,16 @@ class VideoProcessor:
|
||||
.run(capture_stdout=True, capture_stderr=True)
|
||||
)
|
||||
|
||||
# 获取视频信息
|
||||
# 获取视频元数据
|
||||
probe = ffmpeg.probe(output_path)
|
||||
video_stream = next(s for s in probe["streams"] if s["codec_type"] == "video")
|
||||
video_info = next(s for s in probe["streams"] if s["codec_type"] == "video")
|
||||
|
||||
duration = float(probe["format"]["duration"])
|
||||
width = int(video_stream["width"])
|
||||
height = int(video_stream["height"])
|
||||
width = int(video_info["width"])
|
||||
height = int(video_info["height"])
|
||||
|
||||
# 生成帧率
|
||||
fps_str = video_stream.get("r_frame_rate", "25/1")
|
||||
# 计算帧率
|
||||
fps_str = video_info.get("r_frame_rate", "25/1")
|
||||
fps_parts = fps_str.split("/")
|
||||
fps_value = float(fps_parts[0]) / float(fps_parts[1]) if len(fps_parts) == 2 else float(fps_parts[0])
|
||||
|
||||
@@ -123,6 +126,13 @@ class VideoProcessor:
|
||||
except ffmpeg.Error as e:
|
||||
stderr = e.stderr.decode() if e.stderr else ""
|
||||
raise RuntimeError(f"FFmpeg error: {stderr}") from e
|
||||
finally:
|
||||
# P2-6 Fix: 确保临时文件在所有情况下都被清理
|
||||
if concat_file is not None:
|
||||
try:
|
||||
concat_file.close()
|
||||
except Exception:
|
||||
pass # 忽略关闭时的错误
|
||||
|
||||
def generate_thumbnail(
|
||||
self,
|
||||
@@ -134,15 +144,15 @@ class VideoProcessor:
|
||||
生成视频缩略图
|
||||
|
||||
Args:
|
||||
video_path: 视频路径
|
||||
timestamp: 截图时间点
|
||||
output_path: 输出路径,默认在视频同目录下生成 thumbnail.jpg
|
||||
video_path: 视频文件路径
|
||||
timestamp: 截图时间点(秒)
|
||||
output_path: 输出路径,默认为视频路径 + .jpg
|
||||
|
||||
Returns:
|
||||
缩略图路径
|
||||
"""
|
||||
if output_path is None:
|
||||
output_path = f"{os.path.splitext(video_path)[0]}_thumbnail.jpg"
|
||||
output_path = f"{os.path.splitext(video_path)[0]}_thumb.jpg"
|
||||
|
||||
try:
|
||||
(
|
||||
@@ -163,10 +173,10 @@ class VideoProcessor:
|
||||
获取视频信息
|
||||
|
||||
Args:
|
||||
video_path: 视频路径
|
||||
video_path: 视频文件路径
|
||||
|
||||
Returns:
|
||||
视频信息字典
|
||||
视频元数据字典
|
||||
"""
|
||||
try:
|
||||
probe = ffmpeg.probe(video_path)
|
||||
|
||||
Reference in New Issue
Block a user