Created
March 10, 2016 10:06
-
-
Save cosven/8a67ff3abe6d5c2c4f19 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# -*- coding:utf8 -*- | |
import chardet | |
import sys, os | |
import shutil | |
from mutagen.mp3 import MP3 | |
import mutagen.id3 | |
from mutagen.id3 import ID3 | |
from mutagen.easyid3 import EasyID3 | |
dictdata = { | |
'album': '', | |
'genre': '', | |
'length': '', | |
'artist': '', | |
'title': '', | |
} | |
def numberAccess(num): | |
num= str(num) | |
if len(num) == 1: | |
num = '00' + num | |
elif len(num) == 2: | |
num = '0' + num | |
else: | |
pass | |
return num | |
def isAvailable(music): | |
# music = mutagen.File(filename, easy=True) | |
if music.has_key('title') \ | |
and music.has_key('artist') and music.has_key('album'): | |
return True | |
else: | |
return False | |
def isHasImage(music): | |
tags = music.tags | |
if tags.has_key('APIC:'): # has image or not | |
return True | |
else: | |
return False | |
def getMusicInfo(filename, num): | |
print filename, ' is under accessing' | |
num = numberAccess(num) | |
music_easy = mutagen.File(filename, easy=True) | |
music = mutagen.File(filename) | |
if isAvailable(music_easy) and isHasImage(music): | |
print filename, " is available" | |
dictdata = {} | |
dictdata['title'] = checkCharset(music_easy['title'][0]) | |
dictdata['artist'] = checkCharset(music_easy['artist'][0]) | |
dictdata['album'] = checkCharset(music_easy['album'][0]) | |
try: | |
dictdata['genre'] = checkCharset(music_easy['genre'][0]) | |
except: | |
dictdata['genre'] = 'UnKnown' | |
dictdata['length'] = str(music_easy.info.length) | |
txtFilename = num + '.txt' | |
f = open(txtFilename, 'w') | |
writeDictToFileWithFormat(dictdata, f) | |
f.close() | |
imageFormat = music.tags['APIC:'].mime | |
if imageFormat == 'image/jpeg': | |
imageFilename = num + '.jpg' | |
image = music.tags['APIC:'].data | |
imageFile = open(imageFilename, 'wb') | |
imageFile.write(image) | |
imageFile.close() | |
musicname = num + '.mp3' | |
shutil.copyfile(filename, musicname) | |
return True | |
else: | |
print filename, " is not available" | |
return False | |
def writeDictToFileWithFormat(dictdata, f): | |
f.write("{") | |
for key in dictdata.keys(): | |
f.write('\n') | |
f.write('\t') | |
f.write(key) | |
f.write(':') | |
try: | |
f.write(dictdata[key]) | |
except Exception, e: | |
f.write('UnKnown') | |
f.write('\n') | |
f.write('}') | |
def checkCharset(text): | |
try: | |
chardet.detect(text) | |
except UnicodeDecodeError: | |
text = text.encode('latin-1').decode('gbk').encode('utf-8') | |
print text, ' has changed the charset to utf8' | |
return text | |
def isMp3File(filename): | |
string= filename[-3:] | |
if string == 'mp3': | |
return True | |
else: | |
return False | |
def accessAllFile(musicPath): | |
number = 1 | |
filelist = os.listdir(musicPath) | |
for eachfile in filelist: | |
filename = musicPath + eachfile | |
if isMp3File(filename): | |
if getMusicInfo(filename, number): | |
number = number + 1 | |
print "accessing " , number, ' file' | |
if __name__=="__main__": | |
currentPath = sys.path[0] | |
os.chdir(currentPath) | |
musicpath = '/home/ysw/tmpmusic/' | |
accessAllFile(musicpath) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment