使用python从mp3文件获取图像

2024-06-28 20:42:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现这个使用eyed3的命令在我的mp3文件上可以很好地工作,将找到的图像写入目录DIR

eyeD3 --write-images=DIR  file.mp3

但是我想在python程序中使用它,有人能给我举个例子吗 关于如何做到这一点

这个片段很好用

audiofile = eyed3.load("file.mp3")
        print(audiofile.tag.album)
        print(audiofile.tag.artist) 
        print(audiofile.tag.title)  
        print(audiofile.tag.track_num)  

Tags: 文件图像命令程序目录tagdirmp3
1条回答
网友
1楼 · 发布于 2024-06-28 20:42:33

这是应该做的。 我还添加了图像类型,因为一个MP3文件中可以有多个图像,而您没有指定需要哪一个。对于大多数文件,这将是专辑封面

import eyed3

audio_file = eyed3.load("test.mp3")
album_name = audio_file.tag.album
artist_name = audio_file.tag.artist
for image in audio_file.tag.images:
    image_file = open("{0} - {1}({2}).jpg".format(artist_name, album_name, image.picture_type), "wb")
    print("Writing image file: {0} - {1}({2}).jpg".format(artist_name, album_name, image.picture_type))
    image_file.write(image.image_data)
    image_file.close()

相关问题 更多 >