You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Mutagen为MP3嵌入APIC封面图片后无法在第三方应用中显示的问题求助

使用Mutagen为MP3嵌入APIC封面图片后无法在第三方应用中显示的问题求助

我现在正在用Python批量更新我的音乐库,数据是从CSV文件里获取的。我选了Mutagen库来做这件事,它用起来简单,大部分功能都能搞定,唯独嵌入图片这块不管用 😅

我写了一段代码尝试创建APIC帧来嵌入封面,但奇怪的是,嵌入后的封面在资源管理器、MP3Tag、Rekordbox这些第三方应用里都完全看不到。日志里明明显示已经成功清除旧的APIC帧、添加了新的帧并保存了更改,图片数据也是有效的,MIME类型检测也没问题,可就是不显示,有没有大佬能帮我排查下问题?

我的代码如下:

from mutagen.id3 import ID3, APIC
from PIL import Image
from io import BytesIO

def embed_artwork(file_path, image_data):
    try:
        # Open the audio file
        audio = ID3(file_path)

        if not image_data:
            print(f"[ERROR] No image data provided for {file_path}.")
            return

        # Detect MIME type from image data
        with Image.open(BytesIO(image_data)) as img:
            detected_format = img.format.lower()
            if detected_format == "jpeg":
                mime_type = "image/jpeg"
            elif detected_format == "png":
                mime_type = "image/png"
            else:
                print(f"[ERROR] Unsupported image format: {detected_format}. Only JPEG and PNG are supported.")
                return

        # Clear existing APIC frames
        audio.delall("APIC")
        print("[INFO] Cleared existing APIC frames.")

        # Create and add the APIC frame
        apic_frame = APIC(
            encoding=3,  # UTF-8 encoding
            mime=mime_type,  # MIME type
            type=3,  # Type 3 indicates front cover artwork
            desc="Cover",  # Description
            data=image_data  # Image data
        )
        audio.add(apic_frame)
        print("[INFO] Added APIC frame with cover image.")

        # Save the changes
        audio.save(v2_version=3)
        print("[INFO] Saved MP3 file with embedded cover image.")

    except Exception as e:
        print(f"[ERROR] Failed to embed cover image in {file_path}: {e}")

备注:内容来源于stack exchange,提问作者Philip Smeets

火山引擎 最新活动