|
|
@@ -92,6 +92,7 @@ def any_to_amr(any_path, amr_path):
|
|
|
audio = AudioSegment.from_file(any_path)
|
|
|
audio = audio.set_frame_rate(8000) # only support 8000
|
|
|
audio.export(amr_path, format="amr")
|
|
|
+ return audio.duration_seconds * 1000
|
|
|
|
|
|
|
|
|
def sil_to_wav(silk_path, wav_path, rate: int = 24000):
|
|
|
@@ -101,3 +102,26 @@ def sil_to_wav(silk_path, wav_path, rate: int = 24000):
|
|
|
wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
|
|
|
with open(wav_path, "wb") as f:
|
|
|
f.write(wav_data)
|
|
|
+
|
|
|
+
|
|
|
+def split_audio(file_path, max_segment_length_ms=60000):
|
|
|
+ """
|
|
|
+ 分割音频文件
|
|
|
+ """
|
|
|
+ audio = AudioSegment.from_file(file_path)
|
|
|
+ audio_length_ms = len(audio)
|
|
|
+ if audio_length_ms <= max_segment_length_ms:
|
|
|
+ return [file_path]
|
|
|
+ segments = []
|
|
|
+ for start_ms in range(0, audio_length_ms, max_segment_length_ms):
|
|
|
+ end_ms = min(audio_length_ms, start_ms + max_segment_length_ms)
|
|
|
+ segment = audio[start_ms:end_ms]
|
|
|
+ segments.append(segment)
|
|
|
+ file_prefix = file_path[: file_path.rindex(".")]
|
|
|
+ format = file_path[file_path.rindex(".") + 1 :]
|
|
|
+ files = []
|
|
|
+ for i, segment in enumerate(segments):
|
|
|
+ path = f"{file_prefix}_{i+1}" + f".{format}"
|
|
|
+ segment.export(path, format=format)
|
|
|
+ files.append(path)
|
|
|
+ return files
|