Skip to content

Instantly share code, notes, and snippets.

@owengc
Created January 18, 2014 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owengc/8486332 to your computer and use it in GitHub Desktop.
Save owengc/8486332 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"############################################################\n",
"## Based on code from Python online documention for os.walk \n",
"## and the Essentia 'Python tutorial' \n",
"############################################################\n",
"\n",
"import os\n",
"import essentia\n",
"import essentia.standard\n",
"import essentia.streaming\n",
"\n",
"from os.path import join, getsize\n",
"\n",
"extensions = dict()\n",
"durations = []\n",
"with open('durations.txt', 'wb') as f:\n",
" for root, dirs, files in os.walk('/Users/ogc/Music/iTunes'):\n",
" if 'CVS' in dirs:\n",
" dirs.remove('CVS') # don't visit CVS directories\n",
" #print sum(getsize(join(root, name)) for name in files),\n",
" print 'album: {0}\\n'.format(root)\n",
" for name in files:\n",
" #used this code to get a complete list of different audio formats in my music library\n",
" #extension = os.path.splitext(name)[1]\n",
" #if extension not in extensions:\n",
" # extensions[extension] = 1\n",
" #else:\n",
" # extensions[extension] += 1\n",
" if name.endswith(('.wav', '.aif', 'aiff', '.flac', '.mp3', '.m4a')):\n",
" #print root\n",
" # and then we actually perform the loading:\n",
" try:\n",
" loader = essentia.standard.MonoLoader(filename = join(root, name))\n",
" #print loader.paramValue('sr') # How can I check for the sampling rate of the file?\n",
" audio = loader()\n",
" #durations.append(len(audio)/44100.) # Using hard-coded 44100 sr for now\n",
" duration_in_seconds = len(audio)/44100.\n",
" f.write('{0}, '.format(duration_in_seconds))\n",
" print '{0},'.format(duration_in_seconds),\n",
" del loader\n",
" del audio\n",
" gc.collect()\n",
" except (RuntimeError, TypeError, NameError):\n",
" #print 'could not parse file $s\\n' % name\n",
" continue\n",
" print\n",
" \n",
"#print extensions.keys()\n",
"#print durations\n"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment