mutagen:如何在mp3,flac和mp4中检测和嵌入专辑封面 [英] mutagen: how to detect and embed album art in mp3, flac and mp4

查看:651
本文介绍了mutagen:如何在mp3,flac和mp4中检测和嵌入专辑封面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检测音频文件是否嵌入了专辑封面,如果没有,请向该文件添加专辑封面.我正在使用诱变剂

I'd like to be able to detect whether an audio file has embedded album art and, if not, add album art to that file. I'm using mutagen

1)检测专辑封面.是否有比此伪代码更简单的方法:

1) Detecting album art. Is there a simpler method than this pseudo code:

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文件中: 如何使用Python?

2) I found this for embedding album art into an mp3 file: How do you embed album art into an MP3 using Python?

如何将专辑封面嵌入其他格式?

How do I embed album art into other formats?

嵌入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()   

推荐答案

嵌入flac:

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

这篇关于mutagen:如何在mp3,flac和mp4中检测和嵌入专辑封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆