sound_util.py
3.33 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
import os
import uuid
from ffmpy import FFmpeg
from pydub import AudioSegment
from funasr.download.file import download_from_url
# # 音频格式转换
# filelist = ["E:\话术质检\东莞音频\3-17\标准录音 3.mp3"]
# for name in filelist:
def transform_sound_type():
# 加载M4A文件
audio = AudioSegment.from_file("baidu_sound_transform_sdk/test01.aac", format="aac")
# 转换为WAV格式并保存
audio.export("./output.mp3", format="mp3")
# 通过倍率提升
def raise_by_ratio(audio_path: str, output_dir: str, ratio):
ext = os.path.basename(audio_path).strip().split('.')[-1]
if ext not in ['wav', 'mp3', 'MP3']:
raise Exception('format error')
ff = FFmpeg(
inputs={
'{}'.format(audio_path): None},
outputs={
os.path.join(
output_dir, '{}.{}'.format(
uuid.uuid4(), ext)): '-filter:a "volume={}"'.format(ratio)})
print(ff.cmd)
ff.run()
return os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
# 通过分贝数提升
def raise_by_decibel(audio_path: str, output_dir: str, decibel):
ext = os.path.basename(audio_path).strip().split('.')[-1]
if ext not in ['wav', 'mp3', 'MP3']:
raise Exception('format error')
ff = FFmpeg(
inputs={
'{}'.format(audio_path): None}, outputs={
os.path.join(
output_dir, '{}.{}'.format(
uuid.uuid4(), ext)): '-filter:a "volume={}dB"'.format(decibel)})
print(ff.cmd)
ff.run()
return os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
def increase_volume(file_path, gain_db):
# 加载音频文件
audio = AudioSegment.from_file(file_path)
# 增加音量
audio = audio + gain_db
# 保存增强后的音频
temp_file_path = "temp_audio_" + str(os.getpid()) + ".mp3"
audio.export(temp_file_path, format="mp3")
return temp_file_path
def sound_volume(url):
# file_path = download_from_url(url)
file_path = "../audios/test.mp3"
print(file_path)
print(os.path.dirname(file_path))
res = increase_volume(file_path, 20)
print(res)
# out_put_path = raise_by_ratio(file_path, str(os.path.dirname(file_path)), 2)
# print(out_put_path)
# return str(out_put_path)
# if __name__ == "__main__":
sound_volume(
"http://glsk-oss.oss-cn-shenzhen.aliyuncs.com/quality/ba6f9c32-1e2d-4ea1-84bc-6a9c7b45a42d.mp3")
# # 音频按大小强制分隔
# filename = "陕西店2-2.28-9.42.MP3"
# audios = AudioSegment.from_file(filename, format="MP3")
# # 获取音频的总时长(秒)
# duration_seconds = audios.duration_seconds
# # 打印时长
# print(f"音频时长(秒): {duration_seconds}")
# # 计算中间位置
# middle_time = int(len(audios)*(80*60+28)/(99*60+14))
# # 创建第一个分割(从开始到中间)
# first_segment = audios[:middle_time]
# first_segment.export("first_half.mp3", format="mp3")
# # 创建第二个分割(从中间到结束)
# second_segment = audios[middle_time:]
# second_segment.export("second_half.mp3", format="mp3")
# 音频合并
# filename = "陕西店2-2.28-9.42.MP3"
# audios = AudioSegment.from_file(filename, format="MP3")
# filename1 = "陕西店2-2.28-9.42.MP3"
# audio1 = AudioSegment.from_file(filename1, format="MP3")
# second_segment = audios + audio1
# second_segment.export("new.mp3", format="mp3")