Extract song information from MP3
In addition to music information, an MP3 song also contains information such as the song name and singer. When we use the winamp software to listen to music, the playlist will automatically read the information. Most people like to download music from the Internet, but the names of the downloaded MP3 files are automatically named by the file upload system, which is not consistent with the songs themselves, it brings a lot of trouble to users. However, if a lazy person is a lazy person, why don't we write a program that automatically reads the song information and renames the MP3 file?
Next I will write the development process using C # as a tool.
The additional information of an MP3 file is stored at the end of the file, which occupies 128 bytes, including the following content (we define a structure description ):
Public struct Mp3Info
{
Public string identify; // TAG, three bytes
Public string Title; // song name, 30 bytes
Public string Artist; // Artist name, 30 bytes
Public string Album; // The Album name, 30 bytes.
Public string Year; // Year, 4 characters long
Public string Comment; // Comment, 28 bytes
Public char reserved1; // Reserved Bit, one byte
Public char reserved2; // Reserved Bit, one byte
Public char reserved3; // reserved byte
}
Therefore, we only need to read the last 128 bytes of the MP3 file and save it to this structure. The function is defined as follows:
/// <Summary>
/// Obtain the last 128 bytes of the MP3 file
/// </Summary>
/// <Param name = "FileName"> file name </param>
/// <Returns> returns a byte array </returns>
Private byte [] getLast128 (string FileName)
{
FileStream fs = new FileStream (FileName, FileMode. Open, FileAccess. Read );
Stream stream = fs;
Stream. Seek (-128, SeekOrigin. End );
Const int seekPos = 128;
Int rl = 0;
Byte [] Info = new byte [seekPos];
Rl = stream. Read (Info, 0, seekPos );
Fs. Close ();
Stream. Close ();
Return Info;
}
Segment the byte array returned above and save it to the Mp3Info structure.