C # automatically change the name based on MP3 Information

Source: Internet
Author: User

Using system;
Using system. IO;
Using system. text;

Public partial class MP3: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
//
}
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
}

/// <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;
}

/// <Summary>
/// Obtain information about mp3 songs, segment the byte array returned above, and save it to the mp3info structure.
/// </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 [I];
Position ++;
}
Currentindex = position;
Mp3info. Identify = STR;

// Obtain the song name
STR = NULL;
Int J = 0;
Byte [] byttitle = new byte [30]; // read the song name to a separate array.
J = 0;
For (I = currentindex; I <currentindex + 30; I ++)
{
Byttitle [J] = info [I];
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 [I];
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 [I];
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 [I];
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 [I];
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;
}

/// <Summary>
/// Convert the byte array into a string. the method used by the above program.
/// </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;
}

/// <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

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 ");
Response. Write ("<SCRIPT> alert ('mp3 renamed! '); </SCRIPT> ");
// String mp3infostr = "<Table border = '1' bordercolor = '#000000' cellpadding = '0' cellspacing = '0' style = 'border-collapse: collapse; '>"
// + "<Tr>"
// + "<TD> identify </TD> <TD> title </TD> <TD> artist </TD> <TD> album </TD> <TD> year </TD>"
// + "<TD> comment </TD> <TD> reserved1 </TD> <TD> reserved2 </TD> <TD> reserved3 </TD>"
// + "</Tr>"
// + "<Tr>"
// + "<TD>" + mp3info. Identify + "</TD>"
// + "<TD>" + mp3info. Title + "</TD>"
// + "<TD>" + mp3info. Artist + "</TD>"
// + "<TD>" + mp3info. album + "</TD>"
// + "<TD>" + mp3info. Year + "</TD>"
// + "<TD>" + mp3info. Comment + "</TD>"
// + "<TD>" + mp3info. reserved1 + "</TD>"
// + "<TD>" + mp3info. reserved2 + "</TD>"
// + "<TD>" + mp3info. reserved3 + "</TD>"
// + "</Tr>"
// + "</Table> ";
// Response. Write (mp3infostr );
Return true;
}
Catch (exception E)
{
Response. Write (E. tostring ());
Return false;
}
}
Else
{
Response. Write ("<SCRIPT> alert ('mp3 file does not exist! '); </SCRIPT> ");
Return false;
}
}
Protected void mp3rename_click (Object sender, eventargs E)
{
Rename (@ "D:/FFMPEG/df.mp3 ");
}
}

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.