Skip to content

Instantly share code, notes, and snippets.

@dansuh17
Created December 4, 2019 17:47
Show Gist options
  • Save dansuh17/fa04843cbb4140d42c47ef44e15964e9 to your computer and use it in GitHub Desktop.
Save dansuh17/fa04843cbb4140d42c47ef44e15964e9 to your computer and use it in GitHub Desktop.
reformatting audio files in certain directory as wav file having certain sample rate
import librosa
import os
from scipy.io import wavfile
ROOT = '.'
DIR = 'src' # source directory
TO_DIR = 'dst' # destination directory
SAMPLE_RATE = 16000
for i, fname in enumerate(os.listdir(os.path.join(ROOT, DIR))):
base, ext = os.path.splitext(fname)
fpath = os.path.join(ROOT, DIR, fname)
audio_data, _ = librosa.load(fpath, sr=SAMPLE_RATE) # resample while loading
# naming format: {dst_dir}_{index}.wav
dst_name = f'{TO_DIR}_{i}.wav'
dst_path = os.path.join(ROOT, TO_DIR, dst_name)
wavfile.write(dst_path, rate=SAMPLE_RATE, data=audio_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment