cloningapp / utils.py
meraj12's picture
Update utils.py
3441734 verified
raw
history blame
717 Bytes
import torchaudio
import os
def save_uploaded_audio(uploaded_file, save_path="converted_audio.wav"):
try:
# Save to a temp file
temp_path = f"temp_{uploaded_file.name}"
with open(temp_path, "wb") as f:
f.write(uploaded_file.read())
# Load with torchaudio
waveform, sample_rate = torchaudio.load(temp_path)
# Convert to 16kHz mono .wav (Whisper preferred)
waveform = waveform.mean(dim=0).unsqueeze(0)
torchaudio.save(save_path, waveform, 16000)
# Clean up temp
os.remove(temp_path)
return save_path
except Exception as e:
print(f"[Error] Audio conversion failed: {e}")
return None