如何使用 javascript 从 mp3 文件中提取专辑封面/图像?

问题描述 投票:0回答:1

我想知道如何从 mp3 音频文件或其他音频文件格式中提取专辑图像/封面。

javascript node.js audio frontend mp3
1个回答
0
投票

您可以使用 JS MediaTags 包。它允许您从媒体文件(包括 MP3)读取元数据。

通过 npm 安装 jsmediatags:

npm install jsmediatags

使用

jsmediatags.read
方法读取mp3文件标签,并得到
picture
。在
Blob
对象中使用它并在
img
标签中设置。或者您可以使用
Blob
将其保存到磁盘等。

jsmediatags.read("test.mp3", {
  onSuccess: function(tag) {
    // Get the album art from the tags
    var image = tag.tags.picture;
    if (image) {
      // If an album cover is found, create a Blob from the image data
      var blob = new Blob(
        [new Uint8Array(image.data)],
        { type: image.format }
      );

      // Create a URL for the Blob so we can display the album cover in an <img> element
      var url = URL.createObjectURL(blob);

      document.getElementById('picture').setAttribute('src', url);
    } else {
      // If no album cover is found, hide the <img> element with ID 'picture'
      document.getElementById('picture').style.display = "none";
    }
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.