I'm trying to record samples from a stream and save the snippets as an .mp3 file. The following code properly reads the stream and saves the data to the file. The file plays in a media player but it has no headers, etc., so when I try and do something else with the .mp3 file (such as converting it to another format using LAME), it fails every time. Does anyone have any experience with this sort of thing?
Below is a working example to get the .mp3 file. Sorry if you get a commercial; radio sucks.
import urllib2
from datetime import datetime
from datetime import timedelta
# Name of the file stream and how long to record a sample
URL = "http://4893.live.streamtheworld.com:80/KUFXFMAAC_SC"
RECORD_SECONDS = 30
# Open the file stream and write file
filename = 'python.mp3'
f=file(filename, 'wb')
url=urllib2.urlopen(URL)
# Basically a timer
t_start = datetime.now()
t_end = datetime.now()
t_end_old = t_end
# Record in chunks until
print "Recording..."
while t_end-t_start < timedelta(seconds=RECORD_SECONDS):
f.write(url.read(1024))
t_end = datetime.now()
# Or one big read?
#f.write(url.read(1024*517))
f.close()
This is maybe close to what I'm trying to do:? encoding mp3 from a audio stream of PyTTS