Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record streaming and saving internet radio in python

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

The final project is cron'ed script that will record an hour or two of internet radio and then transfer it to my phone for playback during my commute. (3g is kind of spotty along my commute)

any snippits or pointers are welcome.

like image 632
madmaze Avatar asked Nov 22 '10 15:11

madmaze


People also ask

Can I record streaming radio?

Mobizen is a screen recorder that you can also use for recording internet radio. You capture any radio streaming and edit the same whenever you want. It comes with easy to start-stop recording feature that will let you record audio from radio stations without any hassle.

How can I record streaming audio from the Internet?

A great way to record streaming audio online is to use a free online recording tool like Screen Capture. This web recorder lets you record audio on your device, ranging from short snippets and clips right up to lengthy music sequences.

What is audio stream Python?

Audiostream is a Python extension that provide an easy-to-use API for streaming bytes to the speaker, or read an audio input stream. It use SDL + SDL_Mixer for streaming the audio out, and use platform-api for reading audio input. This extension works on Android and iOS as well.


2 Answers

I am aware this is a year old, but this is still a viable question, which I have recently been fiddling with.

Most internet radio stations will give you an option of type of download, I choose the MP3 version, then read the info from a raw socket and write it to a file. The trick is figuring out how fast your download is compared to playing the song so you can create a balance on the read/write size. This would be in your buffer def.

Now that you have the file, it is fine to simply leave it on your drive (record), but most players will delete from file the already played chunk and clear the file out off the drive and ram when streaming is stopped.

I have used some code snippets from a file archive without compression app to handle a lot of the file file handling, playing, buffering magic. It's very similar in how the process flows. If you write up some sudo-code (which I highly recommend) you can see the similarities.

like image 61
omgimdrunk Avatar answered Nov 03 '22 17:11

omgimdrunk


The following has worked for me using the requests library to handle 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

That will save a stream to the stream.mp3 file until you interrupt it with ctrl+C.

like image 29
Jazzer Avatar answered Nov 03 '22 17:11

Jazzer