2011-09-01 35 views
10

我希望能夠檢測到音頻文件是否嵌入了專輯封面,如果沒有,則將專輯封面添加到該文件。我正在使用誘變劑誘變劑:如何檢測和嵌入MP3,FLAC和MP4專輯封面

1)檢測專輯封面。是否有一個更簡單的方法比這個僞代碼:

from mutagen import File 
audio = File('music.ext') 
test each of audio.pictures, audio['covr'] and audio['APIC:'] 
    if doesn't raise an exception and isn't None, we found album art 

2)我發現這個嵌入專輯封面成MP3文件: How do you embed album art into an MP3 using Python?

如何嵌入專輯封面成其他格式?

編輯:嵌入MP4

audio = MP4(filename) 
data = open(albumart, 'rb').read() 

covr = [] 
if albumart.endswith('png'): 
    covr.append(MP4Cover(data, MP4Cover.FORMAT_PNG)) 
else: 
    covr.append(MP4Cover(data, MP4Cover.FORMAT_JPEG)) 

audio.tags['covr'] = covr 
audio.save() 

回答

5

嵌入後手:

from mutagen.flac import File, Picture, FLAC 

def add_flac_cover(filename, albumart): 
    audio = File(filename) 

    image = Picture() 
    image.type = 3 
    if albumart.endswith('png'): 
     mime = 'image/png' 
    else: 
     mime = 'image/jpeg' 
    image.desc = 'front cover' 
    with open(albumart, 'rb') as f: # better than open(albumart, 'rb').read() ? 
     image.data = f.read() 

    audio.add_picture(image) 
    audio.save() 

爲了完整性,檢測圖像

def pict_test(audio): 
    try: 
     x = audio.pictures 
     if x: 
      return True 
    except Exception: 
     pass 
    if 'covr' in audio or 'APIC:' in audio: 
     return True 
    return False 
+2

哪裏圖片()函數來自哪裏?你有什麼要導入? – Danny

+4

它是「從mutagen.flac導入FLAC,圖片」你應該包括這個在你的文檔 – Danny

+0

截至目前在'''​​'''''''''''''''''mutagen.flac'''中沒有''''''''''。什麼是'''File''',它來自哪裏? –