LibVLC can't read stream from network

This forum is about all development around libVLC.
GuitarExtended
Blank Cone
Blank Cone
Posts: 11
Joined: 25 Mar 2013 22:27

LibVLC can't read stream from network

Postby GuitarExtended » 25 Mar 2013 22:34

Hi,
This is my first post here. I'm new to libVLC. I want to use it with Python, using the bindings with vlc.py.
I followed this example (the one with the wrapper classes) :
http://git.videolan.org/?p=vlc/bindings ... b;f=README

It all works fine when I try to play a local file, but it doesn't work with a stream from the web (a web radio). This very same stream works fine in VLC (with the GUI).
I have tested this in Win XP and in Debian (on a Raspberry Pi). In XP "p.play()" returns 0, but I cannot hear any sound and 'p.is_playing()' returns 0. In Debian, I get a "cannot peek" error in the terminal, but i don't get any audio either.

I have no idea what's wrong. I checked my firewall in XP and both VLC and python have exceptions.

Any help is most welcome, this is a bit frustrating...

Cheers!

sherington
Cone that earned his stripes
Cone that earned his stripes
Posts: 491
Joined: 10 Sep 2008 11:57
VLC version: master
Operating System: Linux

Re: LibVLC can't read stream from network

Postby sherington » 26 Mar 2013 08:13

You don't give an example of the web radio URL that is not working for you so it's hard to say... but maybe the URL you have is not the stream itself and is instead a playlist like this:

http://dir.xiph.org/listen/561/listen.m3u

If this is the case, then when you play that URL vlc will actually parse what is found at that URL and create one or more sub-items and attach them to the current media. You can than iterate those sub-items and play them, or play the first one or whatever.

Just about every streaming web radio directory that I've seen supplies URLs like this.

GuitarExtended
Blank Cone
Blank Cone
Posts: 11
Joined: 25 Mar 2013 22:27

Re: LibVLC can't read stream from network

Postby GuitarExtended » 26 Mar 2013 09:04

Hi, thank you first of all for your reply.
I have tried with these 2 urls :
http://tvradio.ert.gr/radio/liveradio/asx/trito.asx
http://www.tv-radio.com/station/fip_mp3 ... 3-128k.m3u

If i simply use ".set_media(url)' on the MediaPlayer, it won't play.
What do I need to do ? Create a MediaListPlayer instead of a MediaPlayer ? And use the ".set_media_list()" method instead of ".set_media()" ?

Cheers.

sherington
Cone that earned his stripes
Cone that earned his stripes
Posts: 491
Joined: 10 Sep 2008 11:57
VLC version: master
Operating System: Linux

Re: LibVLC can't read stream from network

Postby sherington » 26 Mar 2013 13:19

I don't know about the python bindings specifically, but that m3u is indeed a playlist and will work in the way I described in my earlier post. So... you basically need to "play" the playlist URL, wait for the "finished" event from the media player, then iterate the new media sub-items and play the one you want (probably the first).

GuitarExtended
Blank Cone
Blank Cone
Posts: 11
Joined: 25 Mar 2013 22:27

Re: LibVLC can't read stream from network

Postby GuitarExtended » 26 Mar 2013 18:24

Hi, i'm sorry, i don't think i understand. I try a few things to no avail.
Do I need to use media_list_player, or would media_player be enough ? Do I need to put the url in a media_list, or simply in a media ?
The "finished" event doesn't seem to exist (not with this name at least).
When i put the url in a list and count the number of items, i get 55. How would I know which one is the good one ? I don't know how to iterate through the sub-items.

sherington
Cone that earned his stripes
Cone that earned his stripes
Posts: 491
Joined: 10 Sep 2008 11:57
VLC version: master
Operating System: Linux

Re: LibVLC can't read stream from network

Postby sherington » 26 Mar 2013 18:50

I told you in principle how to do it using libvlc. I don't know python.

If I "play" your m3u URL, I get this single sub-item created on the media instance:

Code: Select all

http://mp3.live.tv-radio.com/fip/all/fiphautdebit.mp3
I then play that and it works.

If I "play" your other asx URL, I get this single sub-item created:

Code: Select all

mms://a1318.l3271931317.c32719.g.lm.akamaistream.net/D/1318/32719/v0001/reflector:31317
That must be possible somehow in the python bindings.

GuitarExtended
Blank Cone
Blank Cone
Posts: 11
Joined: 25 Mar 2013 22:27

Re: LibVLC can't read stream from network

Postby GuitarExtended » 27 Mar 2013 11:24

Hurrah, it's working !

Thank you very much for your help. I paste my code below for future readers because I haven't seen many examples for Python bindings on the web.

Code: Select all

import vlc import time url="http://tvradio.ert.gr/radio/liveradio/asx/net.asx" global flag flag = 0 def end_reached(self): global flag flag = 1 print("End reached!") i=vlc.Instance() #Create VLC instance p=i.media_player_new() # Create new media player event_manager = p.event_manager() # Attach event to player (next 3 lines) event=vlc.EventType() event_manager.event_attach(event.MediaPlayerEndReached, end_reached) m=i.media_new(url) # Create new media p.set_media(m) # Set URL as the player's media m.release() p.play() # play it while flag == 0: # Wait until the end of the first media has been reached... time.sleep(0.5) print('Loading playlist...') sub_list = m.subitems() # .. and get the sub itmes in the playlist sub_list.lock() sub = sub_list.item_at_index(0) # Get the first sub item sub_list.unlock() sub_list.release() p.set_media(sub) # Set it as the new media in the player p.play() # and play it sub.release() a = 0 while p.is_playing()==0: # This is just a counter that runs until the stream is actually being played time.sleep(0.5) a += 1 print(a)

yang0312
New Cone
New Cone
Posts: 3
Joined: 20 Jan 2017 13:15

Re: LibVLC can't read stream from network

Postby yang0312 » 20 Jan 2017 13:42

HI, nice posts.
I have problem with playing .m3u file(for music playlist)
I used exactly same code with yours, but changed url into .m3u file.
seems like code is working, but music don't make sounds....

I know .m3u file is usable. I tried

Code: Select all

vlc playlist.m3u
and it's perfectly fine.....

I hope someone could teach me how I use .m3u file in LibVLC

I used python 2.7.9

Code: Select all

# -*- coding: utf-8 -*- import os from threading import Timer import vlc import time global flag flag = 0 def end_reached(self): global flag flag = 1 print("End reached!") dir = "/home/pi/Smart-Mirror-master/music" # This is the directory where the audio files are, you can make it something like # C:\Users\M4Shooter\Desktop\Music\file.mp3 too, just to play a specific file print "Playing ", dir playlist = open('playlist.m3u', 'w') # Create a temp playlist (if not present) playlist.truncate() # Truncate / Erase the file data playlist.write(dir + '\n') # Write's the directory path in 'dir' to the file playlist.close() i=vlc.Instance() #Create VLC instance p=i.media_player_new() # Create new media player event_manager = p.event_manager() # Attach event to player (next 3 lines) event=vlc.EventType() event_manager.event_attach(event.MediaPlayerEndReached, end_reached) m=i.media_new("playlist.m3u") # Create new media p.set_media(m) # Set URL as the player's media m.release() p.play() # play it while flag == 0: # Wait until the end of the first media has been reached... time.sleep(0.5) print('Loading playlist...') sub_list = m.subitems() # .. and get the sub itmes in the playlist sub_list.lock() sub = sub_list.item_at_index(0) # Get the first sub item sub_list.unlock() sub_list.release() p.set_media(sub) # Set it as the new media in the player p.play() # and play it sub.release() a = 0 while p.is_playing()==0: # This is just a counter that runs until the stream is actually being played time.sleep(0.5) a += 1 print(a) remove = lambda : os.remove('playlist.m3u') # Function to remove the file removeFile = Timer(20.0, remove) # We need to wait for some seconds before removing the file, removeFile.start()


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 13 guests