如何读取Id3v2标签 [英] How to read Id3v2 tag

查看:110
本文介绍了如何读取Id3v2标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main(string[] args)
{
    FileStream fs = File.Open(@"C:\Skrillex - Rock n' Roll (Will Take You to the Mountain).mp3", FileMode.Open);
    BinaryReader br = new BinaryReader(fs);

    byte[] tag = new byte[3];
    byte[] version = new byte[2];
    byte[] flags = new byte[1];
    byte[] size = new byte[4];
    byte[] frameId = new byte[4];
    byte[] frameSize = new byte[4];
    byte[] frameFlags = new byte[2];
        
    br.Read(tag, 0, tag.Length);
    br.Read(version, 0, version.Length);
    br.Read(flags, 0, flags.Length);
    br.Read(size, 0, size.Length);
    br.Read(frameId, 0, frameId.Length);
    br.Read(frameSize, 0, frameSize.Length);
    br.Read(frameFlags, 0, frameFlags.Length);

    ulong iSize = (ulong)frameSize[0] << 21 | (ulong)frameSize[1] << 14 | (ulong)frameSize[2] << 7 | (ulong)frameSize[3];
    Console.WriteLine("Frame Data Size : " + iSize.ToString());

    byte[] body = new byte[iSize];
    br.Read(body, 0, body.Length);
    Console.WriteLine(BitConverter.ToString(body));
    Console.WriteLine(ConvertHexToString(BitConverter.ToString(body)));

    br.Close();
}

public string ConvertHexToString(string HexValue)
{
    string StrValue = "";
    HexValue = HexValue.Replace("-", "");
    while (HexValue.Length > 0)
    {
        StrValue += Convert.ToChar(Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
        HexValue = HexValue.Substring(2, HexValue.Length - 2);
    }
    return StrValue;
}

我正在编写无需外部库或Shell32即可读取ID3v2.3标签的代码.

I am writing the code for reading ID3v2.3 tags without external library or Shell32.

上面的代码就是该代码,但似乎无法正常工作.

The above code is that code, but it seems not to work properly.

以下是我运行代码时的结果:

The following is the result when I run the code:

帧数据大小:91

Frame Data Size : 91

01-FF-FE-52-00-6F-00-63-00-6B-00-20-00-6E-00-27-00-20-00-52-00-6F-00-6C -00-6C-00-20-00-28-> 00-57-00-69-00-6C-00-6C-00-20-00-54-00-61-00-6B-00-65-00-20-00-59-00-6F-00- 75-00-20-00-74-> 00-6F-00-20-00-74-00-68-00-65-00-20-00-4D-00-6F-00-75-00-6E-00-74-00-61-00- 69-00-6E-00-29-00

01-FF-FE-52-00-6F-00-63-00-6B-00-20-00-6E-00-27-00-20-00-52-00-6F-00-6C-00-6C-00-20-00-28-> 00-57-00-69-00-6C-00-6C-00-20-00-54-00-61-00-6B-00-65-00-20-00-59-00-6F-00-75-00-20-00-74-> 00-6F-00-20-00-74-00-68-00-65-00-20-00-4D-00-6F-00-75-00-6E-00-74-00-61-00-69-00-6E-00-29-00

ÿþR

它不会返回歌曲标题"Rock n'Roll(将您带到山上)".记录在标签中.

It is not returning the song title "Rock n' Roll (Will Take You to the Mountain)" that was recorded in the tag.

出什么问题了?

推荐答案

开头的01表示它被编码为UTF-16(每个字符2个字节).接下来的两个字节FF FE是字节顺序标记,因此您可以知道是将字节对解释为最高有效优先还是最低有效优先.之后,您便拥有了实际的文本数据.

The 01 at the start indicates that it is encoded as UTF-16 (2 bytes per character). The next two bytes, FF FE, are the byte order mark so you can tell whether to interpret the byte pairs as most significant first or least significant first. After that you have the actual text data.

0052 - R
006F - o
0063 - c
006B - k

这篇关于如何读取Id3v2标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆