27 lines
918 B
Python
27 lines
918 B
Python
# import os
|
|
import pydub
|
|
from pathlib import Path
|
|
|
|
|
|
class FileHandler:
|
|
def __init__(self, directory: str):
|
|
self.dir_path = Path(directory)
|
|
|
|
def combine_waves(self, output_file: str, add_silence: float = 0.5):
|
|
wav_files = sorted(
|
|
[f for f in self.dir_path.glob("*.wav")], key=lambda f: f.stat().st_mtime
|
|
)
|
|
combined = pydub.AudioSegment.empty()
|
|
for f in wav_files:
|
|
audio = pydub.AudioSegment.from_wav(str(f))
|
|
combined += audio
|
|
if add_silence > 0:
|
|
combined += pydub.AudioSegment.silent(duration=int(add_silence * 1000))
|
|
combined.export(output_file, format="wav")
|
|
|
|
def wavs_to_mp3(self, wav_file: str, output_file: str):
|
|
mp3_file = pydub.AudioSegment.empty()
|
|
audio = pydub.AudioSegment.from_wav(wav_file)
|
|
mp3_file += audio
|
|
mp3_file.export(output_file, format="mp3")
|