How to play mp3 from url

I am trying to write a python script that will play mp3 from Soundcloud url

This is what I have already done:

from urllib.request import urlopen url = "soundcloud.com/artist/song.mp3" u = urlopen(url) data = u.read(1024) while data: player.play(data) data = u.read(1024) 

I tried pyaudio with many parameters, such as changing formats, channels, speed. and I just get a weird sound from the speakers, I searched Google for pyaudio , playing mp3 and did not find any information.

I tried pygame by creating a Sound object, transferring bytes from mp3, and then just executing the play function. I am not getting any errors: the script is working, but nothing is reproducing.

I work with Python 3 and Ubuntu.

+5
source share
3 answers

If you have VLC installed (or are ready to install it), then this should work:

 import vlc p = vlc.MediaPlayer("http://your_mp3_url") p.play() 

This has the advantage that it works with everything that works with VLC, not just MP3. It can also be paused if you want.

+7
source

Sorry, I don’t have Python3 to test here, for mp3 stream using pyaudio you need to decode it in PCM data, I know that pymedia can do this, but it is too old and just supports python27 .

To do this correctly, you will need to find out some attributes of your sound, such as samplerate, number of channels, bit resolution, to set it to pyaudio.

I can show how I do it using python27 + pyaudio , first I will show how it is done for .wav stream

 from urllib2 import urlopen #to python3.x #from urllib.request import urlopen import pyaudio pyaud = pyaudio.PyAudio() srate=44100 stream = pyaud.open(format = pyaud.get_format_from_width(1), channels = 1, rate = srate, output = True) url = "http://download.wavetlan.com/SVV/Media/HTTP/WAV/NeroSoundTrax/NeroSoundTrax_test4_PCM_Mono_VBR_8SS_44100Hz.wav" u = urlopen(url) data = u.read(8192) while data: stream.write(data) data = u.read(8192) 

select a large buffer, python is slowly in the while loop, I did it using chunks of size 8192 , note that format , channels and rate are rigth attributes for this wav file, so for .wav we do not need to decode, this is PCM data, now for mp3 we need to decode and put in PCM format in the stream.

Let's try using pymedia

 from urllib2 import urlopen import pyaudio import pymedia.audio.acodec as acodec import pymedia.muxer as muxer dm= muxer.Demuxer( 'mp3' ) pyaud = pyaudio.PyAudio() srate=44100 stream = pyaud.open(format = pyaud.get_format_from_width(2), channels = 1, rate = srate, output = True) url = "http://www.bensound.org/bensound-music/bensound-dubstep.mp3" u = urlopen(url) data = u.read(8192) while data: #Start Decode using pymedia dec= None s= " " sinal=[] while len( s ): s= data if len( s ): frames= dm.parse( s ) for fr in frames: if dec== None: # Open decoder dec= acodec.Decoder( dm.streams[ 0 ] ) r= dec.decode( fr[ 1 ] ) if r and r.data: din = r.data; s="" #decode ended stream.write(din) data = u.read(8192) 

This may be a secret because I have never seen anyone show how this can be done in python, for python3 I don't know anything that can decode .mp3 into pieces like pymedia do.

Here these two codes are erased and work for .wav and .mp3

+2
source

Check if you can upload the file manually using the URL . If its a secure site with the username / passwd, you may need to take care of this first.

If not, here is the working code that loads the file from the URL using urllib2 and then plays it using pydub ,

Its a two-step process in which the first mp3 file is downloaded and saved to a file, and then played using an external player.

 import urllib2 from pydub import AudioSegment from pydub.playback import play mp3file = urllib2.urlopen("http://www.bensound.org/bensound-music/bensound-dubstep.mp3") with open('./test.mp3','wb') as output: output.write(mp3file.read()) song = AudioSegment.from_mp3("./test.mp3") play(song) 

** Update **
You mentioned that you need streaming from the Internet. In this case, you can watch GStreamer with Python. Handcuffs.

Below is the link fooobar.com/questions/959587 / ....

0
source

All Articles