Skip to content

Instantly share code, notes, and snippets.

@lmacken
Created May 25, 2012 19:46
Show Gist options
  • Save lmacken/2790132 to your computer and use it in GitHub Desktop.
Save lmacken/2790132 to your computer and use it in GitHub Desktop.
Add tempo metadata to your music collection. This script has been rewritten and turned into a new project: http://github.com/lmacken/tempi
# NOTE: I re-wrote this code and turned it into a real project:
# http://github.com/lmacken/tempi
# tempofinder - Add tempo metadata to your music
# ==============================================
# Sign up for an EchoNest API key: https://developer.echonest.com/docs
# sudo yum -y install python-virtualenv python-eyed3
# virtualenv env
# source env/bin/activate
# pip install pyechonest
# export ECHO_NEST_API_KEY=
# export ECHO_NEST_CONSUMER_KEY=
# export ECHO_NEST_SHARED_SECRET=
# python tempofinder.py <directory of music>
import os
import sys
import eyeD3
from pyechonest import song
EXTENSIONS = ('mp3', 'flac', 'ogg')
def get_tempo(artist, title):
results = song.search(artist=artist, title=title, results=1,
buckets=['audio_summary'])
if len(results) > 0:
return results[0].audio_summary['tempo']
else:
return None
def process_music(library):
for root, dirs, files in os.walk(library):
for filename in files:
if filename.split('.')[-1] in EXTENSIONS:
full_path = os.path.join(root, filename)
tag = eyeD3.Tag()
try:
tag.link(full_path)
bpm = tag.getBPM()
except Exception, e:
print("Error: %s (%s)" % (str(e), full_path))
continue
if bpm:
continue
artist = tag.getArtist()
title = tag.getTitle()
if artist and title:
tempo = get_tempo(artist, title)
if tempo:
print("%s - %s BPM = %d" % (artist, title, tempo))
tag.setBPM(tempo)
tag.update()
if __name__ == '__main__':
if len(sys.argv) <> 2:
print "Usage: python %s <directory of music>" % sys.argv[0]
sys.exit(-1)
process_music(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment