Skip to content

Instantly share code, notes, and snippets.

@ovuruska
Created December 22, 2022 17:15
Show Gist options
  • Save ovuruska/6290eb077085932cd5109adc8f00c759 to your computer and use it in GitHub Desktop.
Save ovuruska/6290eb077085932cd5109adc8f00c759 to your computer and use it in GitHub Desktop.
Convert from any audio file to .wav
from pathlib import Path
from pydub import AudioSegment
def convert_to_wav(audio_path,target_sr=32000):
P = Path(audio_path)
extension = P.suffix
extension = extension[1:]
if extension == "wav":
return audio_path
sound = AudioSegment.from_file(audio_path, format=extension)
sound = sound.set_frame_rate(target_sr)
new_path = P.with_suffix(".wav")
sound.export(new_path, format="wav")
return new_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment