Created
December 4, 2019 17:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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