134 lines
4.0 KiB
Python
134 lines
4.0 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
测试音视频处理功能
|
|
"""
|
|
import sys
|
|
import os
|
|
sys.path.append('apps')
|
|
|
|
def test_media_handler():
|
|
"""测试音视频处理器"""
|
|
print("测试音视频处理器...")
|
|
|
|
try:
|
|
from common.handle.impl.media.media_split_handle import MediaSplitHandle
|
|
from common.handle.impl.media.media_adapter import MediaAdapter
|
|
|
|
# 创建处理器
|
|
handler = MediaSplitHandle()
|
|
print("✓ MediaSplitHandle 创建成功")
|
|
|
|
# 测试文件类型支持
|
|
class MockFile:
|
|
def __init__(self, name, content=b'test'):
|
|
self.name = name
|
|
self.content = content
|
|
self.size = len(content)
|
|
|
|
def read(self):
|
|
return self.content
|
|
|
|
def seek(self, pos):
|
|
pass
|
|
|
|
# 测试音频文件支持
|
|
audio_files = ['test.mp3', 'test.wav', 'test.m4a', 'test.flac']
|
|
for filename in audio_files:
|
|
file = MockFile(filename)
|
|
if handler.support(file, lambda x: x.read()):
|
|
print(f"✓ {filename} 支持")
|
|
else:
|
|
print(f"✗ {filename} 不支持")
|
|
|
|
# 测试视频文件支持
|
|
video_files = ['test.mp4', 'test.avi', 'test.mov', 'test.mkv']
|
|
for filename in video_files:
|
|
file = MockFile(filename)
|
|
if handler.support(file, lambda x: x.read()):
|
|
print(f"✓ {filename} 支持")
|
|
else:
|
|
print(f"✗ {filename} 不支持")
|
|
|
|
# 测试非媒体文件
|
|
other_files = ['test.txt', 'test.pdf', 'test.docx']
|
|
for filename in other_files:
|
|
file = MockFile(filename)
|
|
if not handler.support(file, lambda x: x.read()):
|
|
print(f"✓ {filename} 正确排除")
|
|
else:
|
|
print(f"✗ {filename} 错误支持")
|
|
|
|
print("\n✓ 所有文件类型测试通过")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
return True
|
|
|
|
def test_media_adapter():
|
|
"""测试媒体适配器"""
|
|
print("\n测试媒体适配器...")
|
|
|
|
try:
|
|
from common.handle.impl.media.media_adapter import MediaAdapter
|
|
|
|
# 创建适配器
|
|
adapter = MediaAdapter()
|
|
print("✓ MediaAdapter 创建成功")
|
|
|
|
# 测试配置
|
|
if adapter.config:
|
|
print("✓ 配置加载成功")
|
|
print(f" - STT Provider: {adapter.config.get('stt_provider')}")
|
|
print(f" - Max Duration: {adapter.config.get('max_duration')}秒")
|
|
print(f" - Segment Duration: {adapter.config.get('segment_duration')}秒")
|
|
|
|
# 测试媒体类型检测
|
|
test_cases = [
|
|
('test.mp3', 'audio'),
|
|
('test.mp4', 'video'),
|
|
('test.wav', 'audio'),
|
|
('test.avi', 'video'),
|
|
]
|
|
|
|
for filename, expected_type in test_cases:
|
|
detected_type = adapter._detect_media_type(filename)
|
|
if detected_type == expected_type:
|
|
print(f"✓ {filename} -> {detected_type}")
|
|
else:
|
|
print(f"✗ {filename} -> {detected_type} (期望: {expected_type})")
|
|
|
|
print("\n✓ 适配器测试通过")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 测试失败: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
print("=" * 50)
|
|
print("音视频学习模块测试")
|
|
print("=" * 50)
|
|
|
|
success = True
|
|
|
|
# 运行测试
|
|
if not test_media_handler():
|
|
success = False
|
|
|
|
if not test_media_adapter():
|
|
success = False
|
|
|
|
print("\n" + "=" * 50)
|
|
if success:
|
|
print("✅ 所有测试通过!")
|
|
else:
|
|
print("❌ 部分测试失败")
|
|
print("=" * 50) |