简体   繁体   English

使用 javascript 从 mp3 读取 id3 标签

[英]read id3 tags from mp3 using javascript

I know this has been asked here before but my conditions are a little different.我知道这个问题以前在这里被问过,但我的情况有点不同。 I am making a chrome app so i have access to all the latest JavaScript file apis it supports without worrying about compatibility.我正在制作一个 chrome 应用程序,因此我可以访问它支持的所有最新 JavaScript 文件 api,而不必担心兼容性。 More over i would really like to do this my self.. ie without any library.更重要的是,我真的很想自己做这个..即没有任何图书馆。 A tutorial or a guide will do.教程或指南就可以了。 After all how difficult can it really be?毕竟这到底有多难?

Basically i have mp3's that user adds and i want to be able to read most basic information like artist and Album (actually, just these two but others wont do any harm).基本上我有用户添加的mp3,我希望能够阅读艺术家和专辑等最基本的信息(实际上,只有这两个,但其他人不会造成任何伤害)。

I believe i have the idea of what id3 tag is and how can the info be read.我相信我知道 id3 标签是什么以及如何读取信息。 I just have to see it in action just once.我只需要看一次它的实际效果。 Thanks谢谢

There's no need to use binaryajax.js or id3 parser lib anymore.不再需要使用 binaryajax.js 或 id3 解析器库。 In Chrome at, you can use FileReader and DataView to read and extract the ID3v1 info.在 Chrome 中,您可以使用FileReaderDataView来读取和提取 ID3v1 信息。 It's just a few lines:这只是几行:

http://ericbidelman.tumblr.com/post/8343485440/reading-mp3-id3-tags-in-javascript http://ericbidelman.tumblr.com/post/8343485440/reading-mp3-id3-tags-in-javascript

This library has good docs.这个库有很好的文档。 I love GitHub我爱 GitHub

https://github.com/leetreveil/node-musicmetadata https://github.com/leetreveil/node-musicmetadata


API API

var fs = require('fs');
var mm = require('musicmetadata');

//create a new parser from a node ReadStream
var parser = new mm(fs.createReadStream('sample.mp3'));

//listen for the metadata event
parser.on('metadata', function (result) {
  console.log(result);
});

This will output the standard music metadata:这将 output 标准音乐元数据:

{ artist : ['Spor'],
  album : 'Nightlife, Vol 5.',
  albumartist : [ 'Andy C', 'Spor' ],
  title : 'Stronger',
  year : '2010',
  track : { no : 1, of : 44 },
  disk : { no : 1, of : 2 },
  picture : [ { format : 'jpg', data : <Buffer> } ]
}

As @joekarl has pointed out, there are libraries to do this for you.正如@joekarl 所指出的,有一些图书馆可以为您做到这一点。 I saw your request for info so you can do it yourself, but here's a gem from the 500+ or so lines from the library on nihilogic.dk :我看到了你的信息请求,所以你可以自己做,但这里有一个来自nihilogic.dk库中 500 多行左右的宝石:

var iLong = bBigEndian ? 
            (((((iByte1 << 8) + iByte2) << 8) + iByte3) << 8) + iByte4
            : (((((iByte4 << 8) + iByte3) << 8) + iByte2) << 8) + iByte1;
        if (iLong < 0) iLong += 4294967296;
        return iLong;

Not to mention a significant amount of pure Javascript AJAX work.更不用说大量的纯 Javascript AJAX 工作了。

There's no reason to reinvent this wheel.没有理由重新发明这个轮子。 However, if you want to look at the code and rewrite it for whatever reason, here are the two libraries files:但是,如果您想查看代码并出于任何原因重写它,这里有两个库文件:

binary ajax library二进制 ajax 库
id3 parser id3 解析器

If you really want to cut out any of the AJAX and just start with reading a file you already have (somehow, without AJAX), the second link has a function called, coincidentally, readTagsFromData .如果你真的想删除 AJAX 中的任何一个,然后从读取你已经拥有的文件开始(不知何故,没有 AJAX),第二个链接有一个 function,巧合的是, readTagsFromData I suggest you start there for your goals.我建议你从那里开始实现你的目标。

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

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