Search code examples
pythonpython-vlc

Why python-vlc mediaplayer doesn't work with url from list?


I tried to build a small webradio with pyhton-vlc. When I directly write the url of the radiostream in the code everything works fine. But I would like to read the urls from the "radiostationen.txt". But when I replace the hard coded url with the first entry of the urls list I get the following error from the vlc-media-player: "VLC is unable to open the MRL 'https://stream.liferadio.tirol/live/mp3-192".

The python code:

import vlc
import keyboard

volume = 10

with open('radiostationen.txt') as f:
    urls = f.readlines()

url = urls[0]

instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
player=instance.media_player_new()
media=instance.media_new(url)
media.get_mrl()
player.audio_set_volume(volume)
player.set_media(media)
player.play()

The radiostationen.txt:

https://stream.liferadio.tirol/live/mp3-192
https://orf-live.ors-shoutcast.at/tir-q2a
https://orf-live.ors-shoutcast.at/fm4-q2a

If I use url = "https://stream.liferadio.tirol/live/mp3-192" instead of url = urls[0] it works.

Also the link in the error works.


Solution

  • The readline and readlines methods retain the newline character (\n) at the end of the lines they read.

    You need to strip off this character before passing the URL to VLC, e.g. url = urls[0][:-1] or url = urls[0].rstrip() should work.