我有这个代码,它从shoutcast执行记录,但它将其分为不同文件中的每个歌曲标题。我想要的是将所有记录放在一个文件中,并希望精确计算期限
public class SHOUTcastRipper
{
private SHOUTcastRipper()
{
// No objects of this class allowed
}
[STAThread]
static void Main()
{
// http://relay.pandora.radioabf.net:9000
String server = "http://radio.mosaiquefm.net:8000/mosalive";
String serverPath = "/";
String destPath = "A:\\"; // destination path for saved songs
HttpWebRequest request = null; // web request
HttpWebResponse response = null; // web response
int metaInt = 0; // blocksize of mp3 data
int count = 0; // byte counter
int metadataLength = 0; // length of metadata header
string metadataHeader = ""; // metadata header that contains the actual songtitle
string oldMetadataHeader = null; // previous metadata header, to compare with new header and find next song
byte[] buffer = new byte[512]; // receive buffer
Stream socketStream = null; // input stream on the web request
Stream byteOut = null; // output stream on the destination file
// create web request
request = (HttpWebRequest) WebRequest.Create(server);
// clear old request header and build own header to receive ICY-metadata
request.Headers.Clear();
request.Headers.Add("GET", serverPath + " HTTP/1.0");
request.Headers.Add("Icy-MetaData", "1"); // needed to receive metadata informations
request.UserAgent = "WinampMPEG/5.09";
// execute request
try
{
response = (HttpWebResponse) request.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
// read blocksize to find metadata header
metaInt = Convert.ToInt32(response.GetResponseHeader("icy-metaint"));
try
{
// open stream on response
socketStream = response.GetResponseStream();
// rip stream in an endless loop
while (true)
{
// read byteblock
int bufLen = socketStream.Read(buffer, 0, buffer.Length);
if (bufLen < 0)
return;
for (int i=0; i<bufLen ; i++)
{
// if there is a header, the 'headerLength' would be set to a value != 0. Then we save the header to a string
if (metadataLength != 0)
{
metadataHeader += Convert.ToChar(buffer[i]);
metadataLength--;
if (metadataLength == 0) // all metadata informations were written to the 'metadataHeader' string
{
string fileName = "";
// if songtitle changes, create a new file
if (!metadataHeader.Equals(oldMetadataHeader))
{
// flush and close old byteOut stream
if (byteOut != null)
{
byteOut.Flush();
byteOut.Close();
}
// extract songtitle from metadata header. Trim was needed, because some stations don't trim the songtitle
fileName = Regex.Match(metadataHeader, "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();
// write new songtitle to console for information
Console.WriteLine(fileName);
// create new file with the songtitle from header and set a stream on this file
byteOut = createNewFile(destPath, fileName);
// save new header to 'oldMetadataHeader' string, to compare if there's a new song starting
oldMetadataHeader = metadataHeader;
}
metadataHeader = "";
}
}
else // write mp3 data to file or extract metadata headerlength
{
if (count++ < metaInt) // write bytes to filestream
{
if (byteOut != null) // as long as we don't have a songtitle, we don't open a new file and don't write any bytes
{
byteOut.Write(buffer, i, 1);
if (count%100 == 0)
byteOut.Flush();
}
}
else // get headerlength from lengthbyte and multiply by 16 to get correct headerlength
{
metadataLength = Convert.ToInt32(buffer[i])*16;
count = 0;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (byteOut != null)
byteOut.Close();
if (socketStream != null)
socketStream.Close();
}
}
/// <summary>
/// Create new file without overwritin existing files with the same filename.
/// </summary>
/// <param name="destPath">destination path of the new file</param>
/// <param name="filename">filename of the file to be created</param>
/// <returns>an output stream on the file</returns>
private static Stream createNewFile(String destPath, String filename)
{
// replace characters, that are not allowed in filenames. (quick and dirrrrrty ;) )
filename = filename.Replace(":", "");
filename = filename.Replace("/", "");
filename = filename.Replace("\\", "");
filename = filename.Replace("<", "");
filename = filename.Replace(">", "");
filename = filename.Replace("|", "");
filename = filename.Replace("?", "");
filename = filename.Replace("*", "");
filename = filename.Replace("\"", "");
try
{
// create directory, if it doesn't exist
if (!Directory.Exists(destPath))
Directory.CreateDirectory(destPath);
// create new file
if (!File.Exists(destPath + filename + ".mp3"))
{
return File.Create(destPath + filename + ".mp3");
}
else // if file already exists, don't overwrite it. Instead, create a new file named <filename>(i).mp3
{
for (int i=1;; i++)
{
if (!File.Exists(destPath + filename + "(" + i + ").mp3"))
{
return File.Create(destPath + filename + "(" + i + ").mp3");
}
}
}
}
catch (IOException)
{
return null;
}
}
}
如何修改代码来完成这项工作?
答案 0 :(得分:2)
我得到了解决方案,这是代码,如果任何身体需要它
public void record()
{
// http://relay.pandora.radioabf.net:9000
String server = "http://radio.mosaiquefm.net:8000/mosalive";
String serverPath = "/";
String destPath = "A:\\"; // destination path for saved songs
String fname="test";
HttpWebRequest request = null; // web request
HttpWebResponse response = null; // web response
int metaInt = 0; // blocksize of mp3 data
int count = 0; // byte counter
int metadataLength = 0; // length of metadata header
byte[] buffer = new byte [ 512 ]; // receive buffer
Stream socketStream = null; // input stream on the web request
Stream byteOut = null; // output stream on the destination file
// create web request
request = ( HttpWebRequest ) WebRequest . Create ( server );
// clear old request header and build own header to receive ICY-metadata
request . Headers . Clear ( );
request . Headers . Add ( "GET" , serverPath + " HTTP/1.0" );
request . Headers . Add ( "Icy-MetaData" , "1" ); // needed to receive metadata informations
request . UserAgent = "WinampMPEG/5.09";
// execute request
try
{
response = ( HttpWebResponse ) request . GetResponse ( );
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message );
return;
}
// read blocksize to find metadata header
metaInt = Convert . ToInt32 ( response . GetResponseHeader ( "icy-metaint" ) );
try
{
// open stream on response
socketStream = response . GetResponseStream ( );
byteOut = createNewFile ( destPath , fname );
// rip stream in an endless loop
while ( byteOut . Length <1024000) // 23650000 ~ 30 min
{
// read byteblock
int bufLen = socketStream . Read ( buffer , 0 , buffer . Length );
if ( bufLen < 0 )
return;
for ( int i=0 ; i < bufLen ; i++ )
{
if ( count++ < metaInt ) // write bytes to filestream
{
if ( byteOut != null ) // as long as we don't have a songtitle, we don't open a new file and don't write any bytes
{
byteOut . Write ( buffer , i , 1 );
if ( count % 100 == 0 )
byteOut . Flush ( );
}
}
else // get headerlength from lengthbyte and multiply by 16 to get correct headerlength
{
metadataLength = Convert . ToInt32 ( buffer [ i ] ) * 16;
count = 0;
}
}
}
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message );
}
finally
{
if ( byteOut != null )
byteOut . Close ( );
if ( socketStream != null )
socketStream . Close ( );
}
}