{"id":11070,"date":"2023-11-29T00:21:17","date_gmt":"2023-11-28T16:21:17","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11070"},"modified":"2023-11-29T00:21:17","modified_gmt":"2023-11-28T16:21:17","slug":"js-mp3-media-tags-metadata","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/11\/js-mp3-media-tags-metadata\/","title":{"rendered":"\u672c\u5730MP3\u5c01\u9762\u56fe\u3001\u65f6\u957f\u7b49\u4fe1\u606f\u7684JS\u8bfb\u53d6"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11070\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11070<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202311-mp3-read.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/mp3-cover.png\" width=\"422\" height=\"264\" alt=\"\u5c01\u9762\u56fe\uff0c\u5178\u96c5\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u57fa\u672c\u5e38\u8bc6<\/h3>\n<p>\u97f3\u9891\u6587\u4ef6\u4e0d\u4ec5\u4ec5\u6709\u97f3\u9891\u91c7\u6837\u6570\u636e\uff0c\u8fd8\u5305\u542b\u5f88\u591a\u6e90\u4fe1\u606f\uff0c\u8fd8\u662f\u53eb\u5143\u4fe1\u606f\u7684\u3002<\/p>\n<p>\u8fd9\u4e9b\u5143\u4fe1\u606f\u5185\u5bb9\u975e\u5e38\u4e30\u5bcc\uff0c\u6709\u4f5c\u8005\u540d\u5b57\u3001\u4e13\u8f91\u4fe1\u606f\u3001\u5c01\u9762\u3001\u7f16\u7801\u683c\u5f0f\u3001\u97f3\u8f68\u4fe1\u606f\u7b49\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/2023-11-28_234119.png\" width=\"469\" height=\"384\" alt=\"\u5143\u4fe1\u606f\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u800c\uff0c\u6211\u4eec\u5728Web\u4e2d\u4f7f\u7528\u7684\u97f3\u9891\u6587\u4ef6\u5927\u591a\u662f\u6ca1\u6709\u8fd9\u4e9b\u4fe1\u606f\u7684\uff0c\u90fd\u88ab\u5de5\u5177\u79fb\u9664\u4e86\uff0c\u51cf\u5c0f\u6587\u4ef6\u7684\u4f53\u79ef\uff0c\u8282\u7ea6\u4f20\u8f93\u7684\u6d41\u91cf\u3002<\/p>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u591a\u5728\u97f3\u4e50\u7f51\u7ad9\u4e0b\u8f7d\u7684MP3\u97f3\u9891\u4e2d\u3002<\/p>\n<p>OK\uff0c\u4e0b\u9762\u9700\u6c42\u6765\u4e86\uff0c\u7f51\u7ad9\u6709\u4e2a\u672c\u5730\u4e0a\u4f20MP3\u97f3\u9891\u7684\u529f\u80fd\uff0c\u5e0c\u671b\u5982\u679cMP3\u97f3\u9891\u6709\u5c01\u9762\u56fe\u7b49\u4fe1\u606f\uff0c\u4e5f\u4e00\u5e76\u4e0a\u4f20\uff0c\u6ca1\u6709\uff0c\u90a3\u5c31\u7b97\u4e86\uff0c\u8bf7\u95ee\u5982\u4f55\u5b9e\u73b0\uff1f<\/p>\n<h3>\u4e8c\u3001\u5c01\u9762\u56fe\u548c\u65f6\u957f\u7684\u83b7\u53d6<\/h3>\n<p>\u97f3\u9891\u7684\u65f6\u957f\u4e0d\u5c5e\u4e8e\u5143\u4fe1\u606f\uff0c\u56e0\u6b64\uff0c\u5c01\u9762\u56fe\u7684\u83b7\u53d6\u548c\u65f6\u957f\u7684\u83b7\u53d6\u9700\u8981\u5206\u5f00\u5b9e\u73b0\u3002<\/p>\n<h4>1. \u5c01\u9762\u56fe\u7684\u83b7\u53d6<\/h4>\n<p>\u97f3\u4e50\u7684\u5c01\u9762\u56fe\u5185\u5bb9\u5c5e\u4e8e\u5143\u4fe1\u606f\uff0c\u56e0\u6b64\uff0c\u9700\u8981\u4ece\u5143\u4fe1\u606f\u4e2d\u8bfb\u53d6\u3002<\/p>\n<p>\u81f3\u4e8e\u8bfb\u53d6\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u76ee\u524d\u5e02\u9762\u4e0a\u6bd4\u8f83\u6210\u719f\u7684JavaScript\u9879\u76ee\uff0c\u4f8b\u5982\uff1a<a href=\"https:\/\/github.com\/aadsm\/jsmediatags\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/aadsm\/jsmediatags<\/a><\/p>\n<p>\u4f7f\u7528\u5f88\u7b80\u5355\uff0c\u5f15\u7528JS\u6587\u4ef6\uff0c\u8bfb\u53d6MP3\u97f3\u9891\uff0c\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>&lt;script src=\".\/jsmediatags.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\njsmediatags.read(\".\/music-file.mp3\", {\r\n  onSuccess: function(tag) {\r\n    console.log(tag);\r\n  },\r\n  onError: function(error) {\r\n    console.log(':(', error.type, error.info);\r\n  }\r\n});\r\n&lt;\/script&gt;<\/pre>\n<p>\u5176\u4e2d\u7684<code>tag<\/code>\u662f\u4e2a\u5de8\u5927\u7684\u5bf9\u8c61\uff0c\u91cc\u9762\u5305\u542b\u5f88\u591a\u7684\u4fe1\u606f\uff0c\u5f53\u7136\uff0c\u524d\u63d0\u662f\u8fd9\u4e2a\u97f3\u9891\u7684\u5143\u4fe1\u606f\u90fd\u4fdd\u7559\u4e0b\u6765\u4e86\u3002<\/p>\n<p>\u5176\u4e2d\u7684\u5c01\u9762\u56fe\u4fe1\u606f\u5c31\u5b58\u5728 <code>tag.tags.picture.data<\/code> \u8fd9\u4e2a\u5bf9\u8c61\u4e0a\u3002<\/p>\n<p>\u5982\u679c\u662f\u672c\u5730\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u672c\u8eab\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>inputFile.addEventListener(\"change\", function(event) {\r\n  var file = event.target.files[0];\r\n  jsmediatags.read(file, ...);\r\n}, false);<\/pre>\n<p>\u4e0d\u8fc7<code>tag.tags.picture.data<\/code>\u7684\u503c\u662f\u4e2aArrayBuffer\u6570\u636e\uff0c\u4e0d\u80fd\u76f4\u63a5\u4f5c\u4e3a\u56fe\u7247\u663e\u793a\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/2023-11-28_235026.png\" width=\"403\" height=\"304\" alt=\"data\u6570\u636e\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8f6c\u6210\u56fe\u7247\u5730\u5740\u3002<\/p>\n<ol>\n<li>\u8f6cBase64\u5730\u5740\uff0c\u8fd9\u662fjsmediatags\u8fd9\u4e2a\u9879\u76ee\u63d0\u4f9b\u7684\u65b9\u6cd5\uff1a\n<pre>const { data, format } = result.tags.picture;\r\nlet base64String = \"\";\r\nfor (const i = 0; i &lt; data.length; i++) {\r\n  base64String += String.fromCharCode(data[i]);\r\n}\r\nimg.src = `data:${data.format};base64,${window.btoa(base64String)}`;<\/pre>\n<p>\u4e0d\u8fc7\u6b64\u65b9\u6cd5\u6709\u4e9b\u5570\u55e6\u3002\n<\/li>\n<li>\u8f6cBlob URL\u5730\u5740\uff0c\u53c2\u8003\u5b9e\u73b0\u5982\u4e0b\uff1a\n<pre>img.src = URL.createObjectURL(new Blob([new Uint8Array(tag.tags.picture.data).buffer]));<\/pre>\n<p>\u672c\u6587<a href=\"https:\/\/www.zhangxinxu.com\/study\/202311\/js-read-mp3-picture-album-duration-demo.php\" rel=\"noopener\" target=\"_blank\">\u63d0\u4f9b\u7684demo\u9875\u9762<\/a>\u5c31\u4f7f\u7528\u7684\u4e0a\u9762\u7684\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n<h4>2. \u97f3\u9891\u6587\u4ef6\u65f6\u957f\u7684\u83b7\u53d6<\/h4>\n<p>\u8fd9\u4e2a\u901a\u8fc7FileReader\u5bf9\u8c61\u8bfb\u53d6\u9009\u62e9\u7684\u672c\u5730\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7\u89e6\u53d1\u52a0\u8f7d\u7684\u65b9\u5f0f\u5c31\u53ef\u4ee5\u6307\u5b9a\u97f3\u9891\u7684\u65f6\u957f\u4e86\u3002\u53c2\u8003\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>inputFile.addEventListener(\"change\", function(event) {\r\n    var file = event.target.files[0];\r\n    const reader = new FileReader();\r\n    reader.onload = function (event) {\r\n        const url = event.target.result;\r\n        const audio = new Audio(url);\r\n        audio.addEventListener('loadedmetadata', function () {\r\n            console.log('\u97f3\u9891\u65f6\u957f\uff1a' + audio.duration);\r\n        });\r\n    };\r\n    reader.readAsDataURL(file);\r\n}, false);<\/pre>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u8fd4\u56de\u7684\u97f3\u9891\u65f6\u957f\u7684\u5355\u4f4d\u662f\u79d2\uff0c\u5b9e\u9645\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u8fd8\u9700\u8981\u8f6c\u6362\u6210<em>\u5206:\u79d2<\/em>\u7684\u5f62\u5f0f\uff0c\u8fd9\u4e2a\u5c5e\u4e8eJS\u57fa\u7840\u4e86\uff0c\u4e0d\u5c55\u793a\u3002<\/p>\n<h3>\u4e09\u3001\u4e07\u4e8b\u5177\u5907\uff0c\u6f14\u793a\u9875\u9762<\/h3>\n<p>\u597d\uff0c\u9884\u5907\u77e5\u8bc6\u90fdget\u5230\u4e86\uff0c\u63a5\u4e0b\u6765\u5c31\u7b80\u5355\u4e86\uff0c\u5957\u7528\u4ee3\u7801\uff0c\u8bbe\u60f3\u4e2a\u573a\u666f\uff0c\u505a\u4e2ademo\u6f14\u793a\u9875\u9762\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202311\/js-read-mp3-picture-album-duration-demo.php\" rel=\"noopener\" target=\"_blank\">\u4f7f\u7528JS\u8bfb\u53d6\u672c\u5730\u97f3\u9891\u56fe\u7247\u65f6\u957fdemo<\/a><\/p>\n<p>\u4f8b\u5982\uff0c\u4ece\u672c\u5730\u9009\u62e9\u4e2a\u5982\u4e0b\u56fe\u6240\u793a\u7684\u97f3\u4e50\u6587\u4ef6\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/2023-11-29_000209.png\" width=\"510\" height=\"247\" alt=\"MP3\u97f3\u4e50\u6587\u4ef6\u9009\u62e9\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u968f\u540eJS\u6267\u884c\u4e0a\u9762\u4e00\u5c0f\u8282\u63d0\u5230\u7684\u5df4\u62c9\u5df4\u62c9\u5b9e\u73b0\uff0c\u7136\u540e\u628a\u83b7\u53d6\u5230\u7684\u4fe1\u606f\u518d\u4e00\u6e32\u67d3\uff0c\u5f53~\u6548\u679c\u6709\u4e86\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/2023-11-29_000301.png\" width=\"414\" height=\"213\" alt=\"\u56fe\u7247\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u56db\u3001\u77ed\u5c0f\u7cbe\u608d\u7ed3\u675f\u8bed<\/h3>\n<p>\u8c8c\u4f3c\u6700\u8fd1\u7684\u6587\u7ae0\u90fd\u662f\u56f4\u7ed5\u97f3\u89c6\u9891\u5f00\u53d1\uff0c\u8fd8\u6709\u89c6\u89c9\u7279\u6548\u5c55\u5f00\u7684\uff0c\u5176\u5b9e\u90fd\u5c5e\u4e8e\u524d\u7aef\u975e\u5e38\u89c4\u7684\u4e1a\u52a1\u5f00\u53d1\u5206\u652f\uff0c\u5728\u5927\u5382\u90fd\u662f\u6709\u4e13\u95e8\u7684\u5c97\u4f4d\u7684\uff0c\u5206\u522b\u662f\u5bcc\u5a92\u4f53\u65b9\u5411\u548c\u89c6\u89c9\u8868\u73b0\u65b9\u5411\uff0c\u867d\u5c0f\u4f17\uff0c\u4f46\u79ef\u7d2f\u5230\u4e00\u5b9a\u7a0b\u5ea6\uff0c\u8fd8\u662f\u5f88\u6709\u7ade\u4e89\u529b\u7684\uff0c\u662f\u4e2a\u9999\u997d\u997d\u3002<\/p>\n<p>\u4e0b\u4e2a\u6708\u5f00\u53d1\uff0c\u6587\u7ae0\u5e94\u8be5\u4f1a\u56de\u5230CSS\u3001HTML\u548cJavaScript\u4e0a\uff0c\u7ee7\u7eed\u4ecb\u7ecd\u65b0\u7279\u6027\uff0c\u4f8b\u5982CSS\u65b0\u51fa\u7684<code>@scope<\/code>\u89c4\u5219\uff0c\u8fd9\u4e5f\u662f\u597d\u4e1c\u897f\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\uff0c\u5509\uff0c\u7a81\u7136\u60f3\u9493\u9c7c\u4e86\u3002<\/p>\n<p><button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11070\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11070<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202311\/mp3-cover.png\" width=\"160\" height=\"100\" alt=\"\u5c01\u9762\u56fe\uff0c\u5178\u96c5\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u901a\u8fc7\u771f\u5b9e\u7684demo\u6f14\u793a\u6848\u4f8b\u7ed9\u5927\u5bb6\u5c55\u793a\u5982\u4f55\u4f7f\u7528JS\u83b7\u53d6\u672c\u5730\u97f3\u4e50\u6587\u4ef6\u7684\u5c01\u9762\u56fe\u3001\u6587\u4ef6\u540d\u53ca\u65f6\u957f\u7b49\u4fe1\u606f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[1868,489,710,872,873,638,982],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11070"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11070"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11070\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}