10

I am using Python 3.5.3 on Windows 8.1 x64 and i need play audio from here

I have tried pyaudio, but it gives me only white noise and error occurred after a few runs of pyaudio (pyaudio module 'pyaudio' has no attribute 'PyAudio').

Please, advise me how better play the streaming audio from url, using Python...

P.S. I already got the song title and artist name with this code:

import requests
import time
import datetime
print(datetime.datetime.now())
import re


url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'
encoding = 'latin1'
info = ''

radio_session = requests.Session()

while True:

    radio = radio_session.get(url, headers={'Icy-MetaData': '1'}, stream=True)

    metaint = int(radio.headers['icy-metaint'])

    stream = radio.raw

    audio_data = stream.read(metaint)
    meta_byte = stream.read(1)

    if (meta_byte):
        meta_length = ord(meta_byte) * 16

        meta_data = stream.read(meta_length).rstrip(b'\0')

        stream_title = re.search(br"StreamTitle='([^']*)';", meta_data)


        if stream_title:

            stream_title = stream_title.group(1).decode(encoding, errors='replace')

            if info != stream_title:
                print('Now playing: ', stream_title)
                info = stream_title
            else:
                pass

        else:
            print('No StreamTitle!')

    time.sleep(1)
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • Hi, I need exactly something like that you share : "http://prem1.rockradio.com/bluesrock". I need to create a private radio with python. Do you know how can I create a web app like that ? – mevaka Apr 23 '20 at 05:17

1 Answers1

16

If you are open for external libraries, you can install vlc binding for python using pip install python-vlc

And use player method to play audio file directly from URL as below.

import vlc
import time

url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'

#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()

Advantage of vlc player is that you can play most media types directly from URL (not just mp3) and also perform player like options such as

>>> player.pause()  #pause play back
>>> player.play() #resume play back
>>> player.stop() #stop play back
MikeL
  • 5,385
  • 42
  • 41
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • Hello! Just tried your code. He started and immediately stopped working. Music did not play. Can you tell me what the problem is? P.S. Launched on Python 3.5.2 on ZentOS, vlc media player is installed – Борис Борисыч Oct 17 '17 at 12:10
  • I found! I need to specify the playing time! Thank you very much! I owe you! – Борис Борисыч Oct 17 '17 at 13:06
  • Glad it worked. It is customary on SO to accept the answer if it solves the problem and up-vote if you like it. This also closes the loop and question does not linger as unanswered. – Anil_M Oct 17 '17 at 13:07
  • @Anil_M it worked for me till today. Could you test it again just to be sure its still working? – Thingamabobs May 31 '20 at 13:53
  • It didnt work for me either as above. But the examples at their site works after upgrading vlc package. https://github.com/oaubert/python-vlc/tree/master/examples . I suspect implementation may have changed. I'll take a look and update answer when I have more time. In the meantime, you can look at examples for sample implementation – Anil_M May 31 '20 at 23:50
  • Also URL http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d is no longer freely available. You may want to try something else. – Anil_M May 31 '20 at 23:51
  • @Anil_M I found that [Exampel][1] here, but yours was the only for streaming. I didnt find a documentation or something either. I'm relativly new to coding, maybe you can give me a good source to find some answers to this modul. [1]: http://git.videolan.org/?p=vlc/bindings/python.git;a=blob;f=examples/tkvlc.py;h=55314cab09948fc2b7c84f14a76c6d1a7cbba127;hb=HEAD – Thingamabobs Jun 01 '20 at 16:11