A MP3 song except for music information, there is also information such as song name, singer, when we listen to music with WinAmp software, the playlist will automatically read this information. Most people like to download music from the Internet, but the downloaded MP3 file name is automatically named file upload system, and the song itself does not match, so it has brought a lot of trouble. However, lazy people have lazy practices, why don't we write a program yourself, automatically read the song information and automatically renamed MP3 files?
Below I will use C # as a tool, write the development process.
An additional information of a MP3 is stored in the last side of the file, accounting for 128 bytes, including the following content (we define a structural description):
Public struct mp3info
{
Public String Identify; // tag, three bytes
Public String Title; // Song name, 30 bytes
Public String Artist; // Singer Name, 30 bytes
Public String Album; // Self-belled, 30 bytes
Public String Year; // year, 4 characters
Public String Comment; // Note, 28 bytes
Public char reserved1; // Reserved, one byte
Public char reserved2; // Reserved, one byte
Public char reserved3; // Reserved, one byte
}
So, we can read the last 128 bytes of the MP3 file and save it to this structure. The function is defined as follows:
///
/// Get the last 128 bytes of MP3 files
/// summary>
/// file name param>
///
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;
}
Then remove the byte arrays returned above and save it to the MP3Info structure.
///
/// Get information about MP3 songs
/// summary>
/// Binary information taken from the MP3 file param>
///
Private MP3Info getmp3info (byte [] info)
{
MP3INFO MP3INFO = New MP3INFO ();
String str = NULL;
INT I;
INT position = 0; // The start value of the loop
INT currentIndex = 0; // INFO current index value
/ / Get the TAG ID
For (i = currentIndex; i Str = str (char) info [i]; POSITION ; } CurrentIndex = position; MP3Info.identify = STR; // Get the song name Str = NULL; Byte [] byttitle = new byte [30]; // read the song name part to a separate array INT j = 0; For (i = currentIndex; i { Byttitle [J] = INFO [I]; POSITION ; J ; } CurrentIndex = position; MP3Info.title = this.byTerstring (Byttitle); // Get the name of the singer Str = NULL; J = 0; Byte [] bytartist = new byte [30]; // read the singer's part part to a separate array For (i = currentIndex; i { Bytartist [J] = INFO [I]; POSITION ; J ; } CurrentIndex = position; Mp3info.artist = this.byTostring (bytartist); // Get the name of the record Str = NULL; J = 0; Byte [] bytalbum = new byte [30]; // read the record name part into a separate array For (i = currentIndex; i { BYTALBUM [J] = INFO [I]; POSITION ; J ; } CurrentIndex = position; MP3info.album = this.byTostring (Bytalbum); // Get the year Str = NULL; J = 0; Byte [] bytyear = new byte [4]; // read the year to a separate array For (i = currentIndex; i { Bytyear [J] = INFO [I]; POSITION ; J ; } CurrentIndex = position; Mp3info.Year = this.byTostring (Bytyear); // get comments Str = NULL; J = 0; Byte [] byTcomment = new byte [28]; // read the comment section to a separate array For (i = currentIndex; i { BYTComment [J] = INFO [I]; POSITION ; J ; } CurrentIndex = position; Mp3info.comment = this.byTostring (ByTcomment); // The following get the reserved bit Mp3info.reserved1 = (char) info [ position]; mp3info.reserved2 = (char) info [ position]; MP3INFO.RESERVED3 = (char) info [ position]; Return MP3Info; } The above method is used below: /// /// convert byte arrays into strings /// summary> /// byte array param> /// Private string bytetostring (byte [] b) { Encoding enc = encoding.getencoding ("GB2312"); String str = enc.getstring (b); Str = str.substring (0, str.indexof ('/ 0')> = 0? str.indexof ('/ 0'): str.length); // Remove useless characters Return Str; } What is the rename? We are renamed songs in the format of (singing) song name, the program is as follows: /// // / Change the file name /// summary> /// file name param> /// Private Bool Rename (String Filepath) { File.exists (FilePath)) { MP3INFO MP3INFO = New MP3INFO (); MP3INFO = this.getMP3Info (this.getlast128 (filepath)); // read file information Mp3info.artist = this.deletenotValue (mp3info.artist); Mp3info.title = this.deletenotValue (MP3Info.title); IF (mp3info.artist.trim (). Length == 0) { Mp3info.artist = "Not Name"; } IF (MP3Info.title.trim (). Length == 0) { Mp3info.title = "Unknown song"; } Try { // Rename File.move (FilePath, Filepath.Substring (0, Filepath.tolower (). LastIndexof ("//"))). Trim () "//" "(" MP3Info.artist.trim () ") " MP3Info.title.trim () " .mp3 "); Return True; } Catch (Exception) { Return False; } } Else { Return False; } }