python - 诱变剂:如何在 mp3、flac 和 mp4 中检测和嵌入专辑封面

标签 python metadata albumart mutagen

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

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

最佳答案

嵌入 flac:

from mutagen import File
from mutagen.flac import 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

关于python - 诱变剂:如何在 mp3、flac 和 mp4 中检测和嵌入专辑封面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7275710/

相关文章:

clojure - 如何向通过 Clojure 中的 def 定义的序列添加元/注释?

python - 如何实现这个机制 :

python - 读取文本文件、操作字符串并以特定格式导出 csv

python - Py.test 没有名为 * 的模块

python - 如何在Python中比较列表和实际字符串?

java - 如何以编程方式从 doc 和 docx 文件中删除用户身份信息?

android - 如何从 Android 中的图像文件中检索元数据

ffmpeg - 如何使用 ffmpeg 添加专辑封面?

android - API 29 等已弃用 ALBUM_ART 列,如何获取路径?