Javascript 使用javascript从mp3中读取id3标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6332384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read id3 tags from mp3 using javascript
提问by Achshar
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. 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?
我知道以前有人问过这个问题,但我的条件有点不同。我正在制作一个 chrome 应用程序,所以我可以访问它支持的所有最新的 JavaScript 文件 API,而不必担心兼容性。更重要的是,我真的很想自己做这件事......即没有任何图书馆。教程或指南就可以了。毕竟,这真的有多困难?
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. I just have to see it in action just once. Thanks
我相信我知道 id3 标签是什么以及如何读取信息。我只需要看一次它的实际效果。谢谢
回答by ebidel
There's no need to use binaryajax.js or id3 parser lib anymore. In Chrome at, you can use FileReader
and DataView
to read and extract the ID3v1 info. It's just a few lines:
不再需要使用 binaryajax.js 或 id3 解析器库。在 Chrome 中,您可以使用FileReader
和DataView
来读取和提取 ID3v1 信息。这只是几行:
http://ericbidelman.tumblr.com/post/8343485440/reading-mp3-id3-tags-in-javascript
http://ericbidelman.tumblr.com/post/8343485440/reading-mp3-id3-tags-in-javascript
回答by Colonel Panic
This library has good docs. I love GitHub
这个库有很好的文档。我爱 GitHub
https://github.com/leetreveil/node-musicmetadata
https://github.com/leetreveil/node-musicmetadata
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:
这将输出标准音乐元数据:
{ 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> } ]
}
回答by rockerest
As @joekarl has pointed out, there are libraries to do this for you. 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:
正如@joekarl 指出的那样,有一些库可以为您执行此操作。我看到了你的信息请求,所以你可以自己做,但这里有一个来自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:
没有理由重新发明这个轮子。但是,如果您想查看代码并出于任何原因重写它,这里有两个库文件:
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
. I suggest you start there for your goals.
如果您真的想删除任何 AJAX 并从读取您已有的文件开始(不知何故,没有 AJAX),那么巧合的是,第二个链接有一个名为readTagsFromData
. 我建议你从那里开始你的目标。