【问题标题】:Getting media tag (.mp3, m4a)获取媒体标签(.mp3、m4a)
【发布时间】:2014-07-08 03:18:40
【问题描述】:

我正在编写一个程序,该程序从选定的媒体文件中收集信息。我想获得以下信息:艺术家、专辑、流派、时间、年份。所有基本的东西。现在我搜索了 Stack 以找到另一个表单帖子,就像我正在创建的帖子一样,没有人有,所以这里什么都没有。

我想在不使用任何第三方库的情况下检索所有这些信息。

不过,我找到了这个 sn-p 代码。

byte[] b = new byte[128];
string sTitle;
string sSinger;
string sAlbum;
string sYear;
string sComm;

FileStream fs = new FileStream(@"D:\Music\Led Zeppelin - Discography\01. Studio albums\04. Led Zeppelin IV (1971)\01. Black Dog.mp3", FileMode.Open);
fs.Seek(-128, SeekOrigin.End);
fs.Read(b, 0, 128);
bool isSet = false;
String sFlag = System.Text.Encoding.Default.GetString(b, 0, 3);
if (sFlag.CompareTo("TAG") == 0)
 {
  System.Console.WriteLine("Tag   is   setted! ");
  isSet = true;
 }

if (isSet)
 {
//get   title   of   song; 
 sTitle = System.Text.Encoding.Default.GetString(b, 3, 30);
 System.Console.WriteLine("Title: " + sTitle);
//get   singer; 
 sSinger = System.Text.Encoding.Default.GetString(b, 33, 30);
System.Console.WriteLine("Singer: " + sSinger);
//get   album; 
 sAlbum = System.Text.Encoding.Default.GetString(b, 63, 30);
 System.Console.WriteLine("Album: " + sAlbum);
//get   Year   of   publish; 
 sYear = System.Text.Encoding.Default.GetString(b, 93, 4);
 System.Console.WriteLine("Year: " + sYear);
//get   Comment; 
 sComm = System.Text.Encoding.Default.GetString(b, 97, 30);
 System.Console.WriteLine("Comment: " + sComm);
}
 System.Console.Read();

我不明白它是如何获取它所获得的信息的。

sTitle = System.Text.Encoding.Default.GetString(b, 3, 30);

获取歌曲名称。

另外,当我得到信息时,我想自定义它,然后再次设置它。

任何帮助,谢谢:)

【问题讨论】:

标签: c# metadata media id3


【解决方案1】:

这里描述了 MP3 ID 标签结构:http://en.wikipedia.org/wiki/ID3

Field   Length      Description
header  3       "TAG"
title   30      30 characters of the title
artist  30      30 characters of the artist name
album   30      30 characters of the album name
year    4       A four-digit year
comment     28 or 30    The comment.
zero-byte   1       If a track number is stored, this byte contains a binary 0.
track   1       The number of the track on the album, or 0. Invalid, if previous byte is not a binary 0.
genre   1       Index in a list of genres, or 255

编辑 - 要再次覆盖字符串,您需要执行以下操作:

FileStream fs = new FileStream(@"D:\Music\Led Zeppelin - Discography\01. Studio albums\04. Led Zeppelin IV (1971)\01. Black Dog.mp3", FileMode.Open);
fs.Seek(-128 + 3, SeekOrigin.End);  //-128 to ID tag, +3 to title
byte[] title = System.Text.Encoding.Default.GetBytes("Black Dog");
fs.Write(title, 0, title.Length);

【讨论】:

  • 感谢您提供的资源。但是b3 是什么意思... sTitle = System.Text.Encoding.Default.GetString(b, 3, 30); 我找到了答案,它们是偏移量。 stackoverflow.com/questions/1645803/…
  • yes GetString(b, 3, 30) 表示读取缓冲区 (b),从第 (3) 个字节开始获取 (30) 个字节并将其格式化为字符串
【解决方案2】:

b 是我创建的字节,它是元数据存储在 mp3 中的位置。它存储在文件的最后 128 个字节中。 b 后面的数字是偏移量。基本上是某些信息存储在 .MP3 文件中的位置。

Field      Length    Offsets
Tag        3           0-2
Songname   30          3-32
Artist     30         33-62
Album      30         63-92
Year       4          93-96
Comment    30         97-126
Genre      1           127

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多