Read raw SHOUTcast Stream






2.68/5 (7 votes)
Jul 26, 2005

54830

367
How to read raw SHOUTcast stream.
Introduction
This is only a raw solution to grab a SHOUTcast stream from a server.
Using the code
'Imports System.Net
'Imports System.Text
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
SHOUTcast_Save("http://192.168.168.97:80/stream/2001", "c:\song_raw.mp3")
End Sub
Public Function SHOUTcast_Save(ByVal sURL As String, _
ByVal sFileName As String) As Boolean
Dim webreq As System.Net.HttpWebRequest = _
CType(System.Net.WebRequest.Create(sURL), _
System.Net.HttpWebRequest)
webreq.Headers.Clear()
webreq.Headers.Add("GET", "/stream/2001 HTTP/1.0")
webreq.UserAgent = "WinampMPEG/5.09"
webreq.Headers.Add("Icy-MetaData", "1")
Dim webres As System.Net.WebResponse = webreq.GetResponse()
Dim oReader As System.IO.Stream = webres.GetResponseStream()
Dim oFile As System.IO.Stream = New System.IO.FileStream(sFileName, _
System.IO.FileMode.OpenOrCreate, _
System.IO.FileAccess.ReadWrite, System.IO.FileShare.None)
For Each oHeader As System.Net.WebHeaderCollection In webres.Headers
'(0) "icy-notice1" String
'(1) "icy-notice2" String
'(2) "icy-name" String
'(3) "icy-genre" String
'(4) "icy-url" String
'(5) "icy-pub" String
'(6) "icy-metaint" String 'how often the metadata
' is sent in the stream
'(7) "icy-br" String
'(8) "icy-irc" String
'(9) "icy-icq" String
'(10) "icy-aim" String
'see http://ample.sourceforge.net/developers.shtml
'icy-notice1 - An informational message.
'icy-notice2 - Another informational message,
' probably "icy-notice3", "icy-notice4" etc.
' can also be used.
'icy-name - The name of the stream that the server is sending,
' this is usually displayed along with
' the current song title in clients.
'icy-genre - The genre of the music served.
'icy-url - An URL associated with the stream,
' usually the homepage of the "webradio" or similar.
'icy-pub - Not sure, believe it indicates
' if the stream is public or private
'icy-br - BitRate, seems mostly informational as most
' clients encountered seem to support VBR (Variable BitRate).
Next
Dim b As Integer
While True
b = oReader.ReadByte()
If b = -1 Then Exit While
oFile.WriteByte(Convert.ToByte(b))
'ToDo: Get the metadata (Thanks to SmackFU
'http://www.smackfu.com/stuff/programming/)
'Read the data stream as you normally would,
'keeping a byte count as you go.
'When the number of bytes equals the metadata
'interval, you will get a metadata block.
'The first part of the block is a length specifier,
'which is the next byte in the stream.
'This byte will equal the metadata length / 16.
'Multiply by 16 to get the actual metadata length.
'(Max byte size = 255 so metadata max length = 4080.)
'Now read that many bytes and you will
'have a string containing the metadata.
'Restart your byte count, and repeat. Yay!
'Note that the metadata length is set to 0 most
'of the time, meaning there is no metadata.
'The metadata is normally sent at two particular
'places: immediately after connecting and when
'the song changes. It seems as if some servers
'aren't real diligent about the former,
'so it might take a while to get your first block.
'Also, be sure you don't count the bytes in the metadata
'length field or the metadata when you're figuring
'out whether you've hit the interval. Only the MP3
'data counts. So you can't be lazy and just keep
'a total byte count and mod it.
'ToDo: Interpret the metadata
'Part of the metadata string should look like this:
'StreamTitle='title of the song';
End While
oReader.Close()
oFile.Close()
End Function
//
Points of Interest
You see, that the metadata is not parsed. (This means you must not write all bytes.) Good Luck... :)