Skip to content

Instantly share code, notes, and snippets.

Last active Aug 22, 2019
What would you like to do?
FMA dataset conversion script, genre classification, to individual subdirs
#! /usr/env/python3
# FMA conversion script, genre classification
# Author: Scott Hawley
# License: Do as you like
# For FMA dataset
# to be used with 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,
AUDIO_DIR = 'fma_small/'
tracks = utils.load('tracks.csv')
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):
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)
## 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment