获取媒体标签(.mp3, m4a)

本文关键字:m4a mp3 媒体 标签 获取 | 更新日期: 2023-09-27 18:04:03

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

我想检索所有这些信息,而不使用任何第三方库。

我找到了这段代码。

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);

获取歌曲标题。

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

任何帮助,谢谢:)

获取媒体标签(.mp3, m4a)

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

EDIT -再次覆盖字符串,您需要这样做:

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);

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