Stream recording and saving internet radio in python

I am looking for a python fragment to read an Internet radio stream (.asx, .pls, etc.) and save it in a file.

The final project is a cron'ed script that will record an hour or two of Internet radio and then transfer it to my phone for playback during my switching. (3g is a spotted view along my commutation)

any snippets or pointers are welcome.

+7
python stream radio audio-streaming
source share
5 answers

So, after training and playing with him, I found Streamripper to work better. This is the command I use

streamripper http://yp.shoutcast.com/sbin/tunein-station.pls?id=1377200 -d ./streams -l 10800 -a tb$FNAME 
+4
source share

The following works for me, using the request library to process the http request.

 import requests stream_url = 'http://your-stream-source.com/stream' r = requests.get(stream_url, stream=True) with open('stream.mp3', 'wb') as f: try: for block in r.iter_content(1024): f.write(block) except KeyboardInterrupt: pass 

This will save the stream in stream.mp3 until you stream.mp3 it with ctrl+C

+3
source share

If you find that your requests or urllib.request call in Python 3 cannot save the stream because you get "ICY 200 OK" instead of the "HTTP / 1.0 200 OK" header, you need to tell the main function of ICY 200 OK in order !

What you can do efficiently is to intercept a procedure that processes the reading of the state after opening the stream immediately before processing the headers.

Simply put, as described above, above the stream open code.

 def NiceToICY(self): class InterceptedHTTPResponse(): pass import io line = self.fp.readline().replace(b"ICY 200 OK\r\n", b"HTTP/1.0 200 OK\r\n") InterceptedSelf = InterceptedHTTPResponse() InterceptedSelf.fp = io.BufferedReader(io.BytesIO(line)) InterceptedSelf.debuglevel = self.debuglevel InterceptedSelf._close_conn = self._close_conn return ORIGINAL_HTTP_CLIENT_READ_STATUS(InterceptedSelf) 

Then place these lines at the beginning of the main procedure before opening the URL.

 ORIGINAL_HTTP_CLIENT_READ_STATUS = urllib.request.http.client.HTTPResponse._read_status urllib.request.http.client.HTTPResponse._read_status = NiceToICY 

They will redefine the standard procedure (only once) and run the NiceToICY function instead of the usual status check when it opened the stream. NiceToICY replaces the unrecognized response with a state, and then copies the corresponding bits of the original response, which are needed by the "real" function _read_status. Finally, the original is called, and the values ​​from this are passed back to the caller, and everything else continues as usual.

I found this to be the easiest way to get around the status message issue causing the error. Hope this is useful for you too.

+3
source share

I know this is the year, but this is still a viable question that I have been addressing recently.

Most Internet radio stations will provide you with a download type option, I select the MP3 version, then I read the information from the raw socket and write it to a file. The trick figures out how fast your download compares to playing the song, so you can create a balance in read / write size. This will be in your def buffer.

Now that you have the file, it’s fine to just leave it on your disk (recording), but most players will delete the already played fragment from the file and clear the file from the disk and disconnect it when streaming is stopped.

I used code snippets from a file archive without a compression application to process many files that process while playing, buffering magic. This is very similar to the process going on. If you write some sudo code (which I highly recommend), you can see the similarities.

+2
source share

I only know how streamcast streaming (which will be mentioned in the .pls file) works:

You are uploading a pls file which is only a playlist. This format is pretty simple, as it is just a text file indicating where the real stream is.

You can connect to this stream, since it is just HTTP, which transmits either MP3 or AAC. For your use, just save every byte that you get to a file and you will get an MP3 or AAC file that you can transfer to your mp3 player.

Shoutcast has one add-on that is optional: metadata. You can find how it works here , but not really needed.

If you want to create an example application, let me know and I'll come up with something later.

+1
source share

All Articles