Extract song information from MP3 (C #)

Source: Internet
Author: User
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.

 

/// <Summary>

 

/// Obtain information about mp3 songs

 

/// </Summary>

 

/// <Param name = "info"> binary information captured from an MP3 file </param>

 

/// <Returns> Returns a mp3info structure </returns>

 

Private mp3info getmp3info (byte [] info)

 

{

 

Mp3info mp3info = new mp3info ();

 

 

 

String STR = NULL;

 

Int I;

 

Int position = 0; // The starting value of the loop

 

Int currentindex = 0; // The current index value of info

 

// Obtain the tag ID

 

For (I = currentindex; I <currentindex + 3; I ++)

 

{

 

STR = STR + (char) Info;

 

 

Position ++;

 

}

 

Currentindex = position;

 

Mp3info. Identify = STR;

 

 

 

// Obtain the song name

 

STR = NULL;

 

Byte [] byttitle = new byte [30]; // read the song name to a separate array.

 

Int J = 0;

 

For (I = currentindex; I <currentindex + 30; I ++)

 

{

 

Byttitle [J] = Info;

 

Position ++;

 

J ++;

 

}

 

Currentindex = position;

 

Mp3info. Title = This. bytetostring (byttitle );

 

 

 

// Obtain the artist name

 

STR = NULL;

 

J = 0;

 

Byte [] bytartist = new byte [30]; // read the singer's name in a separate array.

 

For (I = currentindex; I <currentindex + 30; I ++)

 

{

 

Bytartist [J] = Info;

 

Position ++;

 

J ++;

 

}

 

Currentindex = position;

 

Mp3info. Artist = This. bytetostring (bytartist );

 

 

 

// Obtain the recording name

 

STR = NULL;

 

J = 0;

 

Byte [] bytalbum = new byte [30]; // read the recording name to a separate array.

 

For (I = currentindex; I <currentindex + 30; I ++)

 

{

 

Bytalbum [J] = Info;

 

Position ++;

 

J ++;

 

}

 

Currentindex = position;

 

Mp3info. album = This. bytetostring (bytalbum );

 

 

 

// Obtain the year

 

STR = NULL;

 

J = 0;

 

Byte [] bytyear = new byte [4]; // read the year to a separate array.

 

For (I = currentindex; I <currentindex + 4; I ++)

 

{

 

Bytyear [J] = Info;

 

Position ++;

 

J ++;

 

}

 

Currentindex = position;

 

Mp3info. Year = This. bytetostring (bytyear );

 

 

// Obtain comments

 

STR = NULL;

 

J = 0;

 

Byte [] bytcomment = new byte [28]; // read the comments to a separate array.

 

For (I = currentindex; I <currentindex + 25; I ++)

 

{

 

Bytcomment [J] = Info;

 

Position ++;

 

J ++;

 

}

 

Currentindex = position;

 

Mp3info. Comment = This. bytetostring (bytcomment );

 

 

 

// Obtain the reserved bits

 

Mp3info. reserved1 = (char) info [++ position];

 

Mp3info. reserved2 = (char) info [++ position];

 

Mp3info. reserved3 = (char) info [++ position];

 

 

 

Return mp3info;

 

}

 

The above program uses the following method:

 

/// <Summary>

 

/// Convert the byte array into a string

 

/// </Summary>

 

/// <Param name = "B"> byte array </param>

 

/// <Returns> returns the converted string </returns>

 

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 should I do if I change my name? We change the name of the song in the format of the song name. The procedure is as follows:

 

/// <Summary>

 

/// Change the file name

 

/// </Summary>

 

/// <Param name = "filepath"> file name </param>

 

/// <Returns> </returns>

 

Private bool Rename (string filepath)

 

{

 

If (file. exists (filepath ))

 

{

 

Mp3info mp3info = new mp3info ();

 

Mp3info = This. getmp3info (this. getlast128 (filepath); // read the file information

 

 

 

Mp3info. Artist = This. deletenotvalue (mp3info. Artist );

 

Mp3info. Title = This. deletenotvalue (mp3info. Title );

 

 

 

If (mp3info. Artist. Trim (). Length = 0)

 

{

 

Mp3info. Artist = "not named ";

 

}

 

 

 

If (mp3info. Title. Trim (). Length = 0)

 

{

 

Mp3info. Title = "Unknown name 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;

 

}

 

}

 

 

 

Oh, the idea is like this, if there is a problem or need source code please mail to: lifenote@21cn.com request.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.