-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo_processor.py
More file actions
115 lines (93 loc) · 3.03 KB
/
video_processor.py
File metadata and controls
115 lines (93 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import tempfile
from moviepy import VideoFileClip
import soundfile as sf
def extract_audio_from_video(video_path, output_audio_path=None):
"""
从视频文件中提取音频
Args:
video_path (str): 视频文件路径
output_audio_path (str, optional): 输出音频文件路径,如果不提供则使用临时文件
Returns:
str: 音频文件路径
"""
if not os.path.exists(video_path):
raise FileNotFoundError(f"视频文件不存在: {video_path}")
if output_audio_path is None:
# 创建临时音频文件
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
output_audio_path = tmp_file.name
try:
# 加载视频文件
video = VideoFileClip(video_path)
# 提取音频
audio = video.audio
# 保存音频文件
audio.write_audiofile(output_audio_path, logger=None)
# 关闭资源
audio.close()
video.close()
return output_audio_path
except Exception as e:
raise Exception(f"提取音频失败: {str(e)}")
def validate_audio_file(audio_path):
"""
验证音频文件是否有效
Args:
audio_path (str): 音频文件路径
Returns:
dict: 音频文件信息
"""
try:
data, samplerate = sf.read(audio_path)
duration = len(data) / samplerate
return {
"valid": True,
"duration": duration,
"sample_rate": samplerate,
"channels": data.shape[1] if len(data.shape) > 1 else 1,
"samples": len(data)
}
except Exception as e:
return {
"valid": False,
"error": str(e)
}
def get_supported_video_formats():
"""
获取支持的视频格式列表
Returns:
list: 支持的视频格式扩展名
"""
return ['.mp4', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.webm', '.m4v']
def validate_video_file(video_path):
"""
验证视频文件是否有效且支持
Args:
video_path (str): 视频文件路径
Returns:
dict: 验证结果
"""
if not os.path.exists(video_path):
return {"valid": False, "error": "文件不存在"}
file_ext = os.path.splitext(video_path)[1].lower()
if file_ext not in get_supported_video_formats():
return {
"valid": False,
"error": f"不支持的视频格式: {file_ext}。支持的格式: {', '.join(get_supported_video_formats())}"
}
try:
video = VideoFileClip(video_path)
duration = video.duration
fps = video.fps
size = video.size
video.close()
return {
"valid": True,
"duration": duration,
"fps": fps,
"size": size,
"format": file_ext
}
except Exception as e:
return {"valid": False, "error": f"无法读取视频文件: {str(e)}"}