How do I capture an mp3 stream with python

What's the best way of capturing an mp3 stream coming off of http and saving it to disk with python?

Thus far I've tried

target = open(target_path, "w")
conn = urllib.urlopen(stream_url)
while True:
    target.write(conn.read(buf_size))

This gives me data but its garbled or wont play in mp3 players.


Asked by: Patrick977 | Posted: 01-10-2021






Answer 1

If you're on Windows, you might accidentally be doing CRLF conversions, corrupting the binary data. Try opening target in binary mode:

target = open(target_path, "wb")

Answered by: Brianna190 | Posted: 02-11-2021



Answer 2

The best way for this is:

urllib.urlretrieve(stream_url, target_path);

Answered by: Grace849 | Posted: 02-11-2021



Answer 3

Perhaps the syntax changed from the previous urllib answer (that got me to the correct answer btw), but this syntax works for python3:

import urllib.request

urllib.request.urlretrieve(stream_url, target_path)

Answered by: Vanessa539 | Posted: 02-11-2021



Similar questions

Python Webcam Http streaming and Image capture

Using Python, I need to be able to create a http streaming video from the webcam and capture a still image from that http source. I thought I could use VLC player to stream the video and just capture a frame using python, but If the webcam is being used by VLC, Python cannot use it to grab a still image. This lead to the thinking that I could use python to stream the video and I could use the same script to take a still im...


python - OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?

Goal and problem I'd like to set up an opencv system to process either HLS streams or RMTP streams, however, I am running into a strange issue regarding a reduced frame-rate and an accumulating lag. It's as if the video gets further and further behind from where it is supposed to be in the stream. I'm looking for a way to keep up to date with a live source even if it means dropping frames. ...


streaming - Playing MMS streams within Python

I'm writing a XM desktop application (I plan on releasing the source on github when I'm finished if anyone is interested) Anyway, the one part I know very little about is how to play media within Python (I'm using PyQt for the frontend). Basically, I have a mms:// url that I need to play. I was wondering if there is a library that could accomplish this or something, really I just need someone to point me in the right direc...


python - Streaming the result of a command back to the browser using Twisted and Comet

I'm writing an application that streams the output (by this I mean both sys.stdout and sys.stderr) of a python script excited on the server, in real time to the browser. The users on the site will be allowed to select the script to run, excite and kill their chosen script, and change some parameters, so I will need a different thread per user on the site (user A can start, stop and change a script, whilst user B c...


Python: Creating a streaming gzip'd file-like?

I'm trying to figure out the best way to compress a stream with Python's zlib. I've got a file-like input stream (input, below) and an output function which accepts a file-like (output_function, below): with open("file") as input: output_function(input) And I'd like to gzip-compress input chunks before sending them to ou...


Python: HTTP Post a large file with streaming

I'm uploading potentially large files to a web server. Currently I'm doing this: import urllib2 f = open('somelargefile.zip','rb') request = urllib2.Request(url,f.read()) request.add_header("Content-Type", "application/zip") response = urllib2.urlopen(request) However, this reads the entire file's contents into memory before posting it. How can I have it stream the file to the server?...


streaming - Python access webcam and audio input

Can a python script on my server access the webcam and audio input of a user as easily and as well as a Flash plugin can?


Streaming 1GB File in Python

How long should it take to stream a 1GB file in python on say a 2Ghz Intel Core 2 Duo machine? fp = open('publisher_feed_8663.xml') for line in fp: a = line.split('<') I suppose I wasn't specific enough. This process takes 20+ minutes which is abnormally long. Based on empirical data, what is a reasonable time?


sockets - python streaming TCP server with RPC

I have written a little streaming mp3 server in python. So far all it does is accept a ServerSocket connection, and begin streaming all mp3 data in its queue to the request using socket.send(). I have implemented this to chunk in stream icy metadata, so the name of the playing song shows up in the client. I would like to add playlist management to the server, so that I can manipulate the playlist of the running s...


Twitter Streaming API with oAuth with Python

I've been trying to search for a good Module to in order to use twitter live streaming API and Python. I have found "tweepy" but it seems like it is using the "Basic Authentication" which is now deprecated. Is there any new module out there to use for that purpose that use oAuth? Thanks, Joel


stream - 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.


python - How do I use the Twitter streaming API?

stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41],track=["twitpic"]) This works. However, it's not "AND". It's "OR". This line gets the location OR keyword. How do I make it "AND"? Here's the code to the library I'm using: def filter(self, follow=None, track=None, async=False, locations=None): self.parameters = {} self.headers['Content-type'...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top