Extract song information from MP3 (C #)

Source: Internet
Author: User
Tags file upload reserved trim
Extract song information from MP3



In addition to music information, a MP3 song contains information such as song name, singer and so on, when we listen to music with Winamp software, the playlist automatically reads the information. Most people like downloading music from the Internet, but download the MP3 file name is the file upload system automatically named, and the song itself simply does not match, so, to the user brought a lot of trouble. However, lazy people have the practice of lazy people, why don't we write a program, the song information automatically read out and automatically renamed for MP3 files?



Below I will use C # as a tool to write out the development process.



The extra information for a MP3 is held at the end of the file, representing a total of 128 bytes, including the following (we define a structure 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;//belongs to record, 30 bytes

public string year;//year, 4 characters

public string comment;//Comment, 28 bytes



Public char reserved1;//reserved bit, one byte

Public char reserved2;//reserved bit, one byte

Public char reserved3;//reserved bit, one byte

}



So, we can just read the last 128 bytes of the MP3 file and save it in the structure. The function is defined as follows:

<summary>

Get the last 128 bytes of the MP3 file

</summary>

<param name= "filename" > FileName </param>

<returns> return 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;

}



The byte array returned above is then segmented and saved into the MP3INFO structure for return.

<summary>

Get information on MP3 songs

</summary>

<param name = "Info" > binary information intercepted from MP3 file </param>

<returns> return to a MP3INFO structure </returns>

Private Mp3info Getmp3info (byte[] Info)

{

Mp3info mp3info = new Mp3info ();



string str = NULL;

int i;

int position = start value of 0;//loop

int currentindex = Current index value of 0;//info

Get tag ID

for (i = currentindex;i<currentindex+3;i++)

{

str = str+ (char) info[i];



position++;

}

Currentindex = position;

mp3info.identify = str;



Get the name of the song

str = NULL;

byte[] Byttitle = new byte[30];//reads the song name part into a separate array

int j = 0;

for (i = currentindex;i<currentindex+30;i++)

{

BYTTITLE[J] = Info[i];

position++;

j + +;

}

Currentindex = position;

Mp3info.title = this.bytetostring (Byttitle);



Get the name of the singer

str = NULL;

j = 0;

byte[] Bytartist = new byte[30];//reads the singer name part into a separate array

for (i = currentindex;i<currentindex+30;i++)

{

BYTARTIST[J] = Info[i];

position++;

j + +;

}

Currentindex = position;

Mp3info.artist = this.bytetostring (bytartist);



Get album name

str = NULL;

j = 0;

byte[] Bytalbum = new byte[30];//reads part of the album name into a separate array

for (i = currentindex;i<currentindex+30;i++)

{

BYTALBUM[J] = Info[i];

position++;

j + +;

}

Currentindex = position;

Mp3info.album = this.bytetostring (bytalbum);



Get year

str = NULL;

j = 0;

byte[] Bytyear = new byte[4];//reads the year part into a separate array

for (i = currentindex;i<currentindex+4;i++)

{

BYTYEAR[J] = Info[i];

position++;

j + +;

}

Currentindex = position;

Mp3info.year = this.bytetostring (bytyear);



Get comments

str = NULL;

j = 0;

byte[] Bytcomment = new byte[28];//reads the annotation section into a separate array

for (i = currentindex;i<currentindex+25;i++)

{

BYTCOMMENT[J] = Info[i];

position++;

j + +;

}

Currentindex = position;

Mp3info.comment = this.bytetostring (bytcomment);



The following gets the reserved bit

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

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

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



return mp3info;

}

The above procedure uses the following method:

<summary>

Converts a byte array to 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? Str. IndexOf (' the '): Str. LENGTH);//Remove unwanted characters



return str;

}



What about renaming? We renamed the song according to the name of the song (singer), and the procedure was as follows:

<summary>

Change file name

</summary>

<param name= "FilePath" > FileName </param>

<returns></returns>

private bool ReName (string FilePath)

{

if (file.exists (FilePath))

{

Mp3info mp3info = new Mp3info ();

Mp3info = This.getmp3info (this.getlast128 (FilePath));/read out 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= "not well-known song";

}



Try

{

Renamed

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;

}

}



Ah, the idea is this, if there is a problem or need source code, please send an email to: lifenote@21cn.com request.




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.