Skip to content

Instantly share code, notes, and snippets.

@drscotthawley
Last active Aug 22, 2019
Embed
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 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