Created
September 26, 2014 10:30
-
-
Save Psirus/d404ba00bba05c22f855 to your computer and use it in GitHub Desktop.
Music by year histogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import os | |
from mutagen import File | |
from mutagen.flac import FLAC, VCFLACDict | |
from mutagen.mp3 import MP3 | |
from mutagen.apev2 import APEv2 | |
from mutagen.m4a import M4A | |
from mutagen.oggvorbis import OggVorbis | |
import matplotlib.pyplot as plt | |
def get_tracks(start_path = '.'): | |
tracks, albums, flacs, cum_length = 0, 0, 0, 0.0 | |
cum_bitrate = 0.0 | |
scanned_tracks = 0 | |
mp3s = 0 | |
oggs = 0 | |
flacs = 0 | |
m4as = 0 | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
if dirnames == []: | |
albums += 1 | |
for f in filenames: | |
ext = os.path.splitext(f)[1] | |
fullpath = os.path.join(dirpath, f) | |
musicextensions = ['.flac', '.mp3', '.mpc', '.wma', '.m4a', '.ogg'] | |
if any(ext == musicext for musicext in musicextensions): | |
tracks += 1 | |
if ext == '.flac': | |
flacs += 1 | |
track = FLAC(fullpath) | |
if ext == '.mp3': | |
mp3s += 1 | |
track = MP3(fullpath) | |
if ext == '.m4a': | |
m4as += 1 | |
track = M4A(fullpath) | |
if ext == '.ogg': | |
oggs += 1 | |
track = OggVorbis(fullpath) | |
length = track.info.length | |
cum_length += length | |
scanned_tracks += 1 | |
bitrate = os.path.getsize(fullpath) / length | |
cum_bitrate += bitrate | |
print cum_length | |
print scanned_tracks | |
print 8* cum_bitrate / scanned_tracks | |
print mp3s+flacs | |
print oggs | |
return (tracks, albums) | |
def years(start_path = '.'): | |
years = [str(x) for x in range(1960, 2015)] | |
histogram = {year: 0 for year in years} | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
for f in filenames: | |
is_track = False | |
ext = os.path.splitext(f)[1] | |
fullpath = os.path.join(dirpath, f) | |
if ext in ['.flac', '.mp3', '.m4a', '.ogg', '.wma']: | |
is_track = True | |
track = File(fullpath) | |
if is_track: | |
date = '' | |
keys = track.keys() | |
if 'date' in keys: | |
date = track.get('date')[0] | |
if 'year' in keys: | |
date = track.get('year')[0] | |
if 'TDRC' in keys: | |
date = str(track.get('TDRC')[0]) | |
if '\xa9day' in keys: | |
date = track.get('\xa9day')[0] | |
if 'WM/Year' in keys: | |
date = track.get('WM/Year')[0].value | |
for year in years: | |
if date.startswith(year): | |
histogram[year] += 1 | |
return histogram | |
def plot_histogram(hist): | |
ymax = max(hist.values()) | |
plt.figure() | |
x = [int(x) for x in hist.keys()] | |
plt.bar(x,hist.values()) | |
plt.title('Zeitliche Verteilung Musiksammlung') | |
plt.xlabel('Jahr') | |
plt.ylabel('Anzahl Songs') | |
# plt.axis([0, xmax, 0, ymax]) | |
plt.show() | |
if __name__ == '__main__': | |
hist = years('/home/psirus/Musik') | |
plot_histogram(hist) | |
print hist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment