I have scenario, where i am building a podcast web applications that allows to listen and store .mp3 podcast file.
I am trying to implement a basic web interface where someone can add the entire id3 tag from the client side (the file will be stored locally on the client side : this client is not like the everyone client but just the guy who gets the raw podcast file without any id3 tag preferably). He then hosts this one page locally adds the correct id3 tags and then copies these .mp3 do a WebDav folder.
I do understand that edits needs to be done at server, but it would really helpful if it all can be done locally on the browser.
Off course there is no ready library to edit files , so i decided to use the HTML5 filesystem api, i.e drop the file into the virtual file system , edit it there are then copy it back to the local system. (for copying there is a ready library FileSaver.js) .
I have been able to do the following: 1) associate the mp3 file dropped at a drop zone to filesystem api using webkitGetAsEntry
2) copy this file then to the file system api.
part of the code looks like:
function onDrop(e)
{
e.preventDefault();
e.stopPropagation();
var items = e.dataTransfer.items;
var files = e.dataTransfer.files;
for (var i = 0, item; item = items[i]; ++i)
{
// Skip this one if we didn't get a file.
if (item.kind != 'file') {
continue;
}
var entry = item.webkitGetAsEntry();
if (entry.isFile)
{
// Copy the dropped entry into local filesystem.
entry.copyTo(cwd, null, function(copiedEntry) {
//setLoadingTxt({txt: DONE_MSG});
renderMp3Writer(entry);
My confusion is how do i add the entire id3 tag ? . I am lost at this point as i am not sure about:
1) can we add the entire id3 tag into the file from the fileWriter method? 2) If yes would this be a binary edit or how?? .
Any Help would be useful. tried the below but i am guessing i am wrong.
var blob1 = new Blob(['ID3hTIT2ga'], {type: 'audio/mp3'});
fileWriter.write(blob1);
You need to build a ID3 buffer, then create a buffer large enough to hold both the ID3 and MP3 file, insert the ID3 and append the MP3 data.
For this you need the ID3 specification and use typed arrays with DataView to build your array.
The ID3 overall structure is defined like this (see link above):
At this point the buffer length is unknown so you need to do this in steps. There are several ways to do this, you can build up small buffer segments for each field, then sum them up into a single buffer. Or you can make a larger buffer you know can hold all the fields you want to include and copy the sum of field from that buffer to the final one.
The latter tends to be simpler and as we're dealing with very small sizes this could be the best way (considering that each fragment in the first approach has their overheads).
So the first thing you need to do is to define the header. The header is defined this way:
ID3 and version are fixed values (other versions exists of course, but lets follow the current).
You can probably ignore most of the flags, if not all, by setting them to 0. But check the docs for your use-case, for example if you want to use extended headers.
Size is defined:
An example how you can build your buffer. First define a buffer big enough to hold all the data as well as a DataView:
The DataView defaults to big-endian which is perfect, so all we need to do now is to fill in the data where it should be. We can make a few helper methods to help us move position at the same time as we write. Positions for DataView are byte-bound:
etc. you can make helpers to write text unicode strings (see TextEncoder for example) and so forth.
To define the header, we can write in the "magic" word ID3. You could convert a string, or since it's only 3 bytes also just write it straight-forward. ID3 = 0x494433 in hex so:
Since we made a wrapper we don't need to worry about the buffer position.
Then write in version (according to spec v.2.4.0 uses 0x0400 not using major version (2)):
Now you can continue with flags and size (see specs).
When the ID3 header is filled up
pos
will now hold the total length. So make a new buffer for ID3 tag and MP3 buffer:The view8 view will allow us to do a simple copy to destination:
If everything went OK you now have a MP3 with a ID3 tag (remember to check for existing ID3s - you need to scan to to end).
You can now send the ArrayBuffer to server, or convert to Blob for IndexedDB, or to an Object-URL if you want to present a link for download (none shown here as answer is becoming out-of-scope).
This should be enough to get you started - as said, you need to study the specs. If you're not familiar with typed array, check those out as well.
Also see the site for other resources (frames etc.).
Sync-safe values
"MP3" files uses frames which starts with 11 bits, all set to 1. If the size field of the header happen to contain 11 bits set to 1, the decoder could mistakenly interpret it as sound data. To avoid this the concept of sync-safe integers are used making sure that each byte's MSB (most signicant bit, bit 7) always is set to 0. The bit is moved to the left, the next byte is shifted one bit, for ID3 tag 4 times (hence the 4x %01111111).
Here is how to encode and decode sync-safe integers using JavaScript (from Wikipedia C/C++ source):
The sync-safe value would show the bits like:
&b01111111011111110111111101111111
for the example value used in the demo above.