Mutagen (Mutagenwrapper):从MP3文件中提取多张图片

0 投票
1 回答
946 浏览
提问于 2025-04-30 22:52

我的脚本在提取FLAC文件中的图片时没有问题,但当处理MP3文件时,它只会抓取第一张图片,其他的都抓不到。

我尝试过的办法:

  • 把ID3v2.3格式转换成ID3v2.4格式
  • 把ID3v2.3格式转换成APEv2格式
  • 打印出track['pictures']的完整数组,但只显示了第一个元素。

这个脚本需要两个命令行参数。第一个是挂载名称,也就是图片将要写入的子目录。第二个是文件的路径。

举个例子:python extract_art.py "classic" "hello.mp3"

# -*- coding: UTF-8 -*-

# This script gets called from metadata.liq
# Run this script using Python 2

import sys
import random
import os
import codecs
from mutagenwrapper import read_tags

# Name of the stream (Exaples: anything-goes, anime)
mount_name = sys.argv[1]

# Name of the file to be opened
filename = sys.argv[2]

# Points to the path where images will be written to
full_path = "/mnt/album_art/" + mount_name + "/"

track = read_tags(filename)

try:
    # Check if there is album art
    album_art = track['pictures'][0]
except:
    # If album art cannot be read, return a null string to flag deletion on album art that pervious song wrote.
    album_art = ""

if album_art == "":
    try:
        # If no album art was found, attempt to delete album art that may have been previously written by another song
        os.remove(full_path + 'albumart.jpg')
        print("Deleted album art")
    except:
        # If there is no album art to delete
        print("No album art to delete")
else:
    #If album art was found in the current track, write the new album art and create the proper paths if necessary
    if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))
    with open(full_path + 'albumart.jpg', 'wb') as f:
            f.write(album_art)
            print("Wrote new album art")
            f.close()   

# Same as writing album art, except with background image

background_image = ""

try:
    background_image = track['pictures'][1]
except:
    if background_image == "":
        background_image = album_art
        print("No background image found, using album art as background iamge.")
    else:
        background_image = ""

if background_image == "":
    try:
        os.remove(full_path + 'backgroundimage.jpg')
        print("Deleted background image")
    except:
        print("No background image to delete")
else:
    if not os.path.exists(os.path.dirname(full_path)):
            os.makedirs(os.path.dirname(full_path))
    with open(full_path + 'backgroundimage.jpg', 'wb') as f:
            f.write(background_image)
            print("Wrote new background image")
            f.close()
暂无标签

1 个回答

1

根据文档的说明:

请注意,这个模块仍处于早期开发阶段。许多功能还不支持……

接下来,它提到了在处理专辑封面和多值标签等方面的限制,所以它在一个格式中处理多个专辑封面标签时出现问题也就不足为奇了。毕竟,这只是版本0.0.5……

如果你想要更深入的解释,可能需要查看源代码,或者直接问作者。(可能通过提交一个bug。)但我敢打赌,唯一的解决办法要么是“自己修复”,要么就是“等到其他人愿意去实现它”。

撰写回答