This commit is contained in:
2025-09-24 10:50:34 +08:00
parent c055a68592
commit ec1705769c
18 changed files with 348 additions and 330 deletions

View File

@@ -1,4 +1,5 @@
"""测试辅助工具"""
import json
import tempfile
import subprocess
@@ -7,7 +8,7 @@ from pathlib import Path
from entity.render_task import RenderTask
from entity.effects.base import EffectProcessor
from config.settings import Config
from config.settings import FFmpegConfig
class MockRenderTask:
@@ -20,7 +21,7 @@ class MockRenderTask:
effects: List[str] = None,
ext_data: Dict[str, Any] = None,
frame_rate: int = 25,
output_path: str = "test_output.mp4"
output_path: str = "test_output.mp4",
):
self.task_id = task_id
self.template_id = template_id
@@ -47,11 +48,11 @@ class FFmpegValidator:
return False
# 检查是否包含基本的滤镜结构
if '[' in filter_str and ']' in filter_str:
if "[" in filter_str and "]" in filter_str:
return True
# 检查常见的滤镜格式
common_filters = ['zoompan', 'setpts', 'trim', 'scale', 'crop']
common_filters = ["zoompan", "setpts", "trim", "scale", "crop"]
return any(f in filter_str for f in common_filters)
except Exception:
@@ -62,7 +63,7 @@ class FFmpegValidator:
"""验证流标识符格式"""
if not stream_id:
return False
return stream_id.startswith('[') and stream_id.endswith(']')
return stream_id.startswith("[") and stream_id.endswith("]")
@staticmethod
def validate_ffmpeg_command(command: List[str]) -> Dict[str, Any]:
@@ -72,7 +73,7 @@ class FFmpegValidator:
"has_input": False,
"has_output": False,
"has_filter": False,
"errors": []
"errors": [],
}
if not command or command[0] != "ffmpeg":
@@ -96,9 +97,7 @@ class FFmpegValidator:
result["has_filter"] = True
result["valid"] = (
result["has_input"] and
result["has_output"] and
len(result["errors"]) == 0
result["has_input"] and result["has_output"] and len(result["errors"]) == 0
)
return result
@@ -108,12 +107,16 @@ class EffectTestHelper:
"""特效测试辅助类"""
@staticmethod
def create_test_effect(effect_class, params: str = "", ext_data: Dict[str, Any] = None):
def create_test_effect(
effect_class, params: str = "", ext_data: Dict[str, Any] = None
):
"""创建测试用特效实例"""
return effect_class(params, ext_data)
@staticmethod
def test_effect_params_validation(effect: EffectProcessor, test_cases: List[Dict[str, Any]]):
def test_effect_params_validation(
effect: EffectProcessor, test_cases: List[Dict[str, Any]]
):
"""批量测试特效参数验证"""
results = []
for case in test_cases:
@@ -123,29 +126,41 @@ class EffectTestHelper:
is_valid = effect.validate_params()
expected = case.get("expected", True)
results.append({
"params": effect.params,
"expected": expected,
"actual": is_valid,
"passed": is_valid == expected,
"description": case.get("description", "")
})
results.append(
{
"params": effect.params,
"expected": expected,
"actual": is_valid,
"passed": is_valid == expected,
"description": case.get("description", ""),
}
)
return results
@staticmethod
def test_filter_generation(effect: EffectProcessor, video_input: str = "[0:v]", effect_index: int = 1):
def test_filter_generation(
effect: EffectProcessor, video_input: str = "[0:v]", effect_index: int = 1
):
"""测试滤镜生成"""
try:
filters, output_stream = effect.generate_filter_args(video_input, effect_index)
filters, output_stream = effect.generate_filter_args(
video_input, effect_index
)
result = {
"success": True,
"filters": filters,
"output_stream": output_stream,
"filter_count": len(filters),
"valid_syntax": all(FFmpegValidator.validate_filter_syntax(f) for f in filters),
"valid_output": FFmpegValidator.validate_stream_identifier(output_stream) if output_stream != video_input else True
"valid_syntax": all(
FFmpegValidator.validate_filter_syntax(f) for f in filters
),
"valid_output": (
FFmpegValidator.validate_stream_identifier(output_stream)
if output_stream != video_input
else True
),
}
except Exception as e:
result = {
@@ -155,23 +170,31 @@ class EffectTestHelper:
"output_stream": "",
"filter_count": 0,
"valid_syntax": False,
"valid_output": False
"valid_output": False,
}
return result
def create_test_video_file(output_path: str, duration: int = 5, resolution: str = "640x480") -> bool:
def create_test_video_file(
output_path: str, duration: int = 5, resolution: str = "640x480"
) -> bool:
"""创建测试用视频文件"""
try:
cmd = [
"ffmpeg", "-y", # 覆盖输出文件
"-f", "lavfi", # 使用libavfilter输入
"-i", f"testsrc=duration={duration}:size={resolution}:rate=25",
"-c:v", "libx264",
"-preset", "ultrafast",
"-crf", "23",
output_path
"ffmpeg",
"-y", # 覆盖输出文件
"-f",
"lavfi", # 使用libavfilter输入
"-i",
f"testsrc=duration={duration}:size={resolution}:rate=25",
"-c:v",
"libx264",
"-preset",
"ultrafast",
"-crf",
"23",
output_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
@@ -191,12 +214,8 @@ def create_sample_template_data() -> Dict[str, Any]:
"id": "part1",
"type": "video",
"duration": 10.0,
"effects": ["zoom:0,2.0,3.0", "ospeed:1.5"]
"effects": ["zoom:0,2.0,3.0", "ospeed:1.5"],
}
],
"settings": {
"width": 1920,
"height": 1080,
"frameRate": 25
}
}
"settings": {"width": 1920, "height": 1080, "frameRate": 25},
}