要開始在Python中提取ID3標籤,有一個模塊。
from ID3 import ID3
mp3_filepath = r'/music/song.mp3'
id3_data = ID3(mp3_filepath)
print 'Artist:', id3_data['ARTIST']
print 'Title:', id3_data['TITLE']
More info on ID3 module.
如果你想遞歸搜索MP3文件的目錄,內置os
模塊可以這樣做:
import os
def mp3_files(root):
# this is a generator that will return mp3 file paths within given dir
for f in os.listdir(root):
fullpath = os.path.join(root,f)
if os.path.isdir(fullpath) and not os.path.islink(fullpath):
for x in mp3_files(fullpath): # recurse into subdir
yield x
else:
if fullpath[len(fullpath)-3:] == 'mp3':
yield fullpath
for p in mp3_files(root_dir):
id3_data = ID3(p)
print 'Artist:', id3_data['ARTIST']
print 'Title:', id3_data['TITLE']
Reference.
在創造方面數據庫,你不需要重新發明輪子(存儲音樂數據是一個常見的數據庫問題) - 谷歌earch會幫助你。 Here's one example。
我從來沒有想過谷歌的數據庫設計,謝謝,關於遞歸mp3搜索的任何想法,但? – Specto 2009-04-21 20:55:24