-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_accelerator.py
More file actions
139 lines (110 loc) · 3.94 KB
/
audio_accelerator.py
File metadata and controls
139 lines (110 loc) · 3.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
"""
音频加速处理脚本
读取voice_zhcn目录中的音频文件,将其加速,然后保存到output文件夹
"""
import os
import argparse
import sys
from pathlib import Path
try:
import librosa
import soundfile as sf
import numpy as np
except ImportError as e:
print(f"错误:缺少必要的库 {e}")
print("请先安装以下依赖:")
print("pip install librosa soundfile numpy")
sys.exit(1)
def create_output_directory(output_dir):
"""创建输出目录"""
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
return output_path
def accelerate_audio(input_file, output_file, speed_factor=1.5):
"""
加速音频文件
Args:
input_file (str): 输入音频文件路径
output_file (str): 输出音频文件路径
speed_factor (float): 加速倍数,默认1.5倍
"""
try:
# 读取音频文件
print(f"正在处理: {input_file}")
audio_data, sample_rate = librosa.load(input_file, sr=None)
# 使用librosa的time_stretch进行时间拉伸(加速)
# speed_factor > 1 表示加速,< 1 表示减速
accelerated_audio = librosa.effects.time_stretch(audio_data, rate=speed_factor)
# 保存加速后的音频
sf.write(output_file, accelerated_audio, sample_rate)
print(f"已保存: {output_file}")
except Exception as e:
print(f"处理文件 {input_file} 时出错: {e}")
def process_audio_directory(input_dir="Voice_zhCN", output_dir="output", speed_factor=1.5):
"""
处理整个音频目录
Args:
input_dir (str): 输入目录名,默认为Voice_zhCN
output_dir (str): 输出目录名,默认为output
speed_factor (float): 加速倍数,默认1.5倍
"""
input_path = Path(input_dir)
# 检查输入目录是否存在
if not input_path.exists():
print(f"错误:输入目录 '{input_dir}' 不存在")
return
# 创建输出目录
output_path = create_output_directory(output_dir)
# 支持的音频格式
audio_extensions = {'.ogg', '.wav', '.mp3', '.flac', '.m4a', '.aac'}
# 获取所有音频文件
audio_files = [f for f in input_path.iterdir()
if f.is_file() and f.suffix.lower() in audio_extensions]
if not audio_files:
print(f"在目录 '{input_dir}' 中没有找到支持的音频文件")
return
print(f"找到 {len(audio_files)} 个音频文件")
print(f"加速倍数: {speed_factor}x")
print(f"输出目录: {output_dir}")
print("-" * 50)
# 处理每个音频文件
success_count = 0
for audio_file in audio_files:
input_file_path = str(audio_file)
output_file_path = str(output_path / audio_file.name)
try:
accelerate_audio(input_file_path, output_file_path, speed_factor)
success_count += 1
except Exception as e:
print(f"跳过文件 {audio_file.name}: {e}")
print("-" * 50)
print(f"处理完成!成功处理 {success_count}/{len(audio_files)} 个文件")
def main():
"""主函数"""
parser = argparse.ArgumentParser(description="音频加速处理工具")
parser.add_argument(
"--input-dir",
default="Voice_zhCN",
help="输入目录名 (默认: Voice_zhCN)"
)
parser.add_argument(
"--output-dir",
default="output",
help="输出目录名 (默认: output)"
)
parser.add_argument(
"--speed",
type=float,
default=1.5,
help="加速倍数 (默认: 1.5)"
)
args = parser.parse_args()
# 验证加速倍数
if args.speed <= 0:
print("错误:加速倍数必须大于0")
return
# 处理音频文件
process_audio_directory(args.input_dir, args.output_dir, args.speed)
if __name__ == "__main__":
main()