Repository navigation
Expand file tree
/
Copy pathsimple_audio_info.py
More file actions
99 lines (82 loc) · 3.86 KB
/
Copy pathsimple_audio_info.py
File metadata and controls
99 lines (82 loc) · 3.86 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
极简版音频分析工具 - 仅使用Python标准库
"""
import os
import sys
import wave
import time
import codecs
def analyze_audio(input_file, output_file):
"""分析WAV音频文件的基本信息"""
print(f"分析音频文件: {input_file}")
try:
# 检查文件是否存在
if not os.path.exists(input_file):
raise FileNotFoundError(f"找不到文件: {input_file}")
# 获取文件大小
file_size = os.path.getsize(input_file)
print(f"文件大小: {file_size / 1024:.2f} KB")
# 打开WAV文件
with wave.open(input_file, 'rb') as wav_file:
# 获取基本信息
channels = wav_file.getnchannels()
sample_width = wav_file.getsampwidth()
frame_rate = wav_file.getframerate()
n_frames = wav_file.getnframes()
duration = n_frames / frame_rate
print(f"通道数: {channels}")
print(f"采样宽度: {sample_width * 8} 位")
print(f"采样率: {frame_rate} Hz")
print(f"总帧数: {n_frames}")
print(f"时长: {duration:.2f} 秒")
# 生成报告文件,使用带BOM的UTF-8编码
with codecs.open(output_file, 'w', encoding='utf-8-sig') as f:
f.write("音频文件分析报告\n")
f.write("==============\n\n")
f.write(f"文件名: {os.path.basename(input_file)}\n")
f.write(f"文件大小: {file_size / 1024:.2f} KB\n")
f.write(f"通道数: {channels}\n")
f.write(f"采样宽度: {sample_width * 8} 位\n")
f.write(f"采样率: {frame_rate} Hz\n")
f.write(f"总帧数: {n_frames}\n")
f.write(f"时长: {duration:.2f} 秒\n\n")
f.write("音频质量评估:\n")
if frame_rate >= 44100 and sample_width >= 2:
f.write("- 音频质量评级: 高质量 (CD级别或更高)\n")
elif frame_rate >= 22050 and sample_width >= 2:
f.write("- 音频质量评级: 中等质量 (适合语音)\n")
else:
f.write("- 音频质量评级: 低质量\n")
# 添加备注
f.write("\n备注: 此分析仅提供音频的基本信息,不含实际的语音转文字功能。\n")
f.write("如需使用完整的语音转文字功能,请安装以下依赖:\n")
f.write("1. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openai-whisper\n")
f.write("2. 安装ffmpeg并设置系统环境变量\n")
print(f"分析完成,报告已保存至: {output_file}")
return True
except Exception as e:
print(f"分析音频时出错: {str(e)}")
# 生成错误报告
try:
with codecs.open(output_file, 'w', encoding='utf-8-sig') as f:
f.write(f"分析失败: {str(e)}\n")
f.write("\n此功能需要WAV格式的音频文件。\n")
f.write("如果您的文件是其他格式,请先转换为WAV格式。\n")
except:
print(f"无法创建错误报告文件: {output_file}")
return False
if __name__ == "__main__":
if len(sys.argv) < 2:
print("用法: python simple_audio_info.py 输入音频文件.wav [输出文件.txt]")
sys.exit(1)
input_file = sys.argv[1]
if len(sys.argv) >= 3:
output_file = sys.argv[2]
else:
# 默认输出文件名
base_name = os.path.splitext(input_file)[0]
output_file = f"{base_name}_info.txt"
result = analyze_audio(input_file, output_file)
sys.exit(0 if result else 1)