How to play mp3 from URL

前端 未结 3 982
不思量自难忘°
不思量自难忘° 2020-12-06 03:11

I\'m trying to write a python script that will play mp3 from Soundcloud URL

This is what I\'ve already done:

from urllib.request import urlopen

url          


        
3条回答
  •  有刺的猬
    2020-12-06 03:34

    Check if you can download file manually using that URL. If its protected site with username/passwd, you may need to take care of that first.

    If not, here is a working code that downloads file from url using urllib2 and then plays it using pydub.

    Its a two step process where first mp3 file is downloaded and saved to file and then played using 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 did mention that you need streaming from web. In that case you may want to look at GStreamer with Python Bindings

    Here is a SO link for that.

提交回复
热议问题