5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascriptでmp3ファイルのID3タグを表示

Posted at

mp3ファイルをアップロードした際にID3v1のデータを表示するプログラムを製作しました。
ID3v1はデータ最後の128bitなので、それを切り分けて読み込みしています。

ID3v2版も作ろうと思いましたが、v1とv2が混在していたり、データも可変長で何かと面倒なので次回にでもやろうと思います。

html
<!DOCTYPE html>
<html>

<body>
	<form method="post" enctype="multipart/form-data">
		<input type="file" id="file" accept="audio/mp3">
	</form>
	<p id='trackInfo'>ファイル情報</p>
	<script>

	</script>
</body>

</html>
javascript
window.onload = function() {
	const reader = new FileReader();
	file.addEventListener('change', function(e) {
		reader.readAsArrayBuffer(e.target.files[0]);
		trackInfo.innerHTML = "";
	});
	reader.onload = function(e) {
		var id3v1 = (new Uint8Array(e.target.result)).slice(-128);
		var judge = id3v1[0] + id3v1[1] + id3v1[2];
		if (judge == 220) {
			for (var i = 3; i < 93; i++) {
				if (i == 33 || i == 63) trackInfo.innerHTML += '<br>';
				trackInfo.innerHTML += String.fromCharCode(id3v1[i]);
			}
		}
	}
};
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?