D
D
Dimazsever2015-06-08 18:14:29
Programming
Dimazsever, 2015-06-08 18:14:29

How to get id3 length in mp3 file?

The length of id3 data is stored in 6-9 bytes. The peculiarity of specifying the length of ID3v2 data is that in each byte the 7th bit is not used and is always set to 0.
Manually using a calculator, I can calculate this length, but how can I calculate it programmatically? How to ignore this 7th bit?
In decimal format, bytes 6-9 contain the following numbers: 0 5 45 56. How to get the length in bytes using this data. The length in this case is 87746 bytes.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Chvalov, 2015-06-08
@Chvalov

PHP -> php.net/manual/ru/ref.id3.php
C/C# -> https://taglib.github.io/
Delphi -> www.delphi.int.ru/articles/1
About MP3 itself like everyone else arranged in it -> habr.ru/p/103635

A
Artyom Egorov, 2016-04-13
@egoroof

JS using File Api and Typed Arrays

Something like this:
function uint7ArrayToUint28(uint7Array) {
    var uint28 = 0;
    for (var i = 0, pow = 21; pow >= 0; pow -= 7, i++) {
        uint28 += uint7Array[i] << pow;
    }
    return uint28;
}


var firstTenBytes = new Uint8Array(arrayBuffer, 0, 10);

var id3TagSize = uint7ArrayToUint28([
    firstTenBytes[6], firstTenBytes[7],
    firstTenBytes[8], firstTenBytes[9]
]) + 10;

The size of the ID3 tag is stored minus the header (10 bytes) - according to the specification. So you need to add 10 more. I somehow overlooked this in the specification, then a bug surfaced - the music was not played.
Essentially it all boils down to this:
var size = bytes[9];
size += bytes[8] << 7;
size += bytes[7] << 14;
size += bytes[6] << 21;
size += 10; // заголовок

Consider an example with your data:
var size = 56;
size += 45 << 7; // 5760 + 56 = 5816
size += 5 << 14; // 81920 + 5816 = 87736
size += 0 << 21; // 0 + 87736 = 87736 - размер ID3 тега без заголовка
size += 10; // 87736 + 10 = 87746 - размер ID3 тега

To understand what is happening here, I recommend reading about JS bitwise operators .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question