Last active
August 22, 2019 06:23
-
-
Save drscotthawley/eb4ffb1ec4de29632403c1db396e419a to your computer and use it in GitHub Desktop.
FMA dataset conversion script, genre classification, to individual subdirs
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
#! /usr/env/python3 | |
# | |
# FMA conversion script, genre classification | |
# Author: Scott Hawley | |
# License: Do as you like | |
# | |
# For FMA dataset https://github.com/mdeff/fma | |
# to be used with panotti https://github.com/drscotthawley/panotti | |
# | |
# This will create a directory called Samples/ | |
# inside which will be directories for each genre, e.g. Rock/ Instrumental/ etc. | |
# within these directories will be the audio files converted to .wav format | |
# | |
# Requires: fma | |
# librosa | |
# ffmpeg | |
# ...hour(s) to run. it's slow, runs in serial | |
import os | |
import librosa | |
import utils # note my fix to fma utils, https://github.com/mdeff/fma/issues/34 | |
AUDIO_DIR = 'fma_small/' | |
tracks = utils.load('tracks.csv') | |
os.mkdir('Samples') | |
small = tracks['set', 'subset'] <= 'small' | |
y_small = tracks.loc[small, ('track', 'genre_top')] | |
sr = 44100 | |
for track_id, genre in y_small.iteritems(): | |
if not os.path.exists('Samples/'+genre): | |
os.mkdir('Samples/'+genre) | |
mp3_filename = utils.get_audio_path(AUDIO_DIR, track_id) | |
out_wav_filename = 'Samples/'+genre+'/'+str(track_id)+'.wav' | |
in_wav_filename = out_wav_filename | |
cmd = 'ffmpeg -hide_banner -loglevel panic -i ' + mp3_filename + ' ' + in_wav_filename | |
print("excuting conversion: "+cmd) | |
os.system(cmd) | |
## We could just have ffmpeg do the full conversion, but we'll let librosa | |
## apply its own defaults by reading & writing | |
print("reading ",in_wav_filename) | |
data, sr = librosa.load(in_wav_filename, sr=sr, mono=True) | |
print("writing ",out_wav_filename) | |
librosa.output.write_wav(out_wav_filename,data,sr=sr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment