Repository navigation
Expand file tree
/
Copy pathdirect_audio_info.py
More file actions
104 lines (87 loc) · 3.1 KB
/
Copy pathdirect_audio_info.py
File metadata and controls
104 lines (87 loc) · 3.1 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
直接访问文件系统,生成音频文件报告
"""
import os
import sys
import time
def main():
# 强制打印信息到控制台
print("=" * 50)
print("音频文件信息生成器")
print("=" * 50)
# 设置输入和输出文件
input_file = "test_audio.wav"
if len(sys.argv) > 1:
input_file = sys.argv[1]
output_file = "direct_audio_info.txt"
if len(sys.argv) > 2:
output_file = sys.argv[2]
print(f"输入文件: {input_file}")
print(f"输出文件: {output_file}")
sys.stdout.flush() # 强制刷新输出缓冲区
# 创建输出目录(如果需要)
output_dir = os.path.dirname(output_file)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"创建目录: {output_dir}")
# 检查文件是否存在
if not os.path.exists(input_file):
print(f"错误: 文件不存在: {input_file}")
return 1
# 获取文件基本信息
file_size = os.path.getsize(input_file)
mod_time = os.path.getmtime(input_file)
# 尝试获取音频特定信息
audio_info = "无法获取详细音频信息"
try:
# 尝试使用wave模块
import wave
with wave.open(input_file, 'rb') as wav:
channels = wav.getnchannels()
samplewidth = wav.getsampwidth()
framerate = wav.getframerate()
nframes = wav.getnframes()
duration = nframes / framerate
audio_info = f"""
频道数: {channels}
采样宽度: {samplewidth * 8} 位
采样率: {framerate} Hz
总帧数: {nframes}
时长: {duration:.2f} 秒
"""
except Exception as e:
print(f"无法读取音频详情: {str(e)}")
audio_info = f"读取错误: {str(e)}"
# 直接写入文件
try:
print("正在写入报告...")
with open(output_file, 'w', encoding='utf-8') 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"修改时间: {time.ctime(mod_time)}\n\n")
f.write("音频信息:\n")
f.write(audio_info)
f.write("\n备注: 这是一个简单的替代报告,不包含实际的语音转文字功能。\n")
print(f"报告已生成: {output_file}")
print(f"文件大小: {os.path.getsize(output_file)} 字节")
# 显示文件内容
print("\n文件内容:")
print("-" * 40)
with open(output_file, 'r', encoding='utf-8') as f:
print(f.read())
return 0
except Exception as e:
print(f"写入报告出错: {str(e)}")
return 1
if __name__ == "__main__":
try:
exit_code = main()
print(f"程序执行完成,退出代码: {exit_code}")
sys.exit(exit_code)
except Exception as e:
print(f"程序执行失败: {str(e)}")
sys.exit(1)