简体   繁体   English

如何获得 mp3 文件的封面?

[英]how can I get the cover of an mp3 file?

我有一个 mp3 文件,当我用 windows 媒体播放器阅读它时,它有专辑的封面,所以我想知道是否有办法在 javascript 或 jQuery 中获得该封面

Read more at this URL: http://www.richardfarrar.com/embedding-album-art-in-mp3-files/在此 URL 阅读更多信息: http : //www.richardfarrar.com/embedding-album-art-in-mp3-files/

What you want is to use the ID3 header, where the arists data and more is stored.您想要的是使用 ID3 标头,其中存储了 arists 数据等。 Also images can be stored in these headers.图像也可以存储在这些标题中。 Probably this is also done in the mp3 files you have.可能这也在您拥有的 mp3 文件中完成。

A library like https://github.com/aadsm/JavaScript-ID3-Reader can read this data out of the MP3 with Javascript.https://github.com/aadsm/JavaScript-ID3-Reader这样的库可以使用 Javascript 从 MP3 中读取这些数据。

Borrowed from the example (Note: you need the library above before this code will work):从示例中借用(注意:在此代码工作之前,您需要上面的库):

 function showTags(url) {
      var tags = ID3.getAllTags(url);
      console.log(tags);
      document.getElementById('title').textContent = tags.title || "";
      document.getElementById('artist').textContent = tags.artist || "";
      document.getElementById('album').textContent = tags.album || "";

      var image = tags.picture;
      if (image) {
        var base64String = "";
        for (var i = 0; i < image.data.length; i++) {
            base64String += String.fromCharCode(image.data[i]);
        }
        var base64 = "data:" + image.format + ";base64," +
                window.btoa(base64String);
        document.getElementById('picture').setAttribute('src',base64);
      } else {
        document.getElementById('picture').style.display = "none";
      }
    }

ID3 is no longer being maintained. ID3 不再维护。 Check here.检查这里。

var jsmediatags = require("jsmediatags");

jsmediatags.read("./song.mp3", {
  onSuccess: function(tag) {
    console.log(tag);
    var image = tag.tags.picture;
    document.getElementById('title').innerHTML = tag.tags.title;
    document.getElementById('artist').innerHTML= tag.tags.artist;
    document.getElementById('album').innerHTML = tag.tags.album;
    document.getElementById('picture').title = tag.tags.title;
      if (image) {
        var base64String = "";
        for (var i = 0; i < image.data.length; i++) {
            base64String += String.fromCharCode(image.data[i]);
        }
        var base64 = "data:" + image.format + ";base64," +
                window.btoa(base64String);
        document.getElementById('picture').setAttribute('src',base64);
      } else {
        document.getElementById('picture').style.display = "none";
        document.getElementById('picture').title = "none";
      }
  },
  onError: function(error) {
    console.log(':(', error.type, error.info);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM