Question

I need to delete all of the id3 tags from an mp3 file. I have tried this:

byte[] a = System.IO.File.ReadAllBytes("X.mp3");
int x = 0;
int b=3;
for (int i = 6; i <= 9; i++)
{
   x += (a[i] << (b * 7));
   b--;
}
byte[] r = new byte[a.Length-x];
for (int i = x; i < a.Length; i++)
{
   r[i-x] = a[i];
}
System.IO.File.WriteAllBytes("X.mp3", r);

But it does not delete all of the id3 tags. I think there is a problem in calculating the size of tag but I do not know what is wrong ?

Was it helpful?

Solution

Your code only addresses ID3v2 tags, at the beginning of the file. If your MP3 file has both types of ID3 - ID3v1 and ID3v2 - you'll need to remove both.

ID3v1 tags are placed at the end of the file and are fixed-length. They'd actually be easier to strip - they are always the last 128 bytes of the file, and start with the text "TAG".

You might be better served by using a pre-existing library to work with the MP3 files; one such is the open-source library, taglib-sharp. It can manipulate both types of ID3 tags, as well as perform a variety of other tasks.

OTHER TIPS

I had to do this recently and didn't want to use a library. Here's a snippet:

var mp3 = File.ReadAllBytes("x.mp3");

int skip = 0;
if (Encoding.ASCII.GetString(mp3, 0, 3) == "ID3")
    skip = 7 + BitConverter.ToInt32(mp3.Skip(6).Take(4).Reverse().ToArray(), 0);

int take = mp3.Length - skip;
if (Encoding.ASCII.GetString(mp3, mp3.Length - 128, 3) == "TAG")
    take -= 128;

File.WriteAllBytes("stripped.mp3", mp3.Skip(skip).Take(take).ToArray());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top