Skip to content

Instantly share code, notes, and snippets.

@drscotthawley
Last active May 3, 2025 17:44
Show Gist options
  • Save drscotthawley/eb4ffb1ec4de29632403c1db396e419a to your computer and use it in GitHub Desktop.
Save drscotthawley/eb4ffb1ec4de29632403c1db396e419a to your computer and use it in GitHub Desktop.
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)
@Melouka22
Copy link

hey, Im working with siamese nn for similarity and i need to work with a dataset of pairs positive for same music different exemples and negative different music i couldnt find any but i thought i could make one by fma small i started by augmented each music and make pos pair with itself and it didnt work well, im looking for an idea for this. could you help? please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment