VLC Python doesn't play YouTube video
Asked Answered
R

2

2

When trying to play a YouTube URL with VLC media player via the Python bindings, the video does not actually play.

Here is my code which does the playing:

    def play(self, mrl):

    self.instance = vlc.Instance()

    try:
        self.media = self.instance.media_new_location(mrl)
    except NameError:
        print('NameError: %s (%s vs LibVLC %s)' % (sys.exc_info()[1],
                                                   vlc.__version__,
                                                   vlc.libvlc_get_version()))

    self.player = self.instance.media_player_new()
    self.player.set_media(self.media)

    event_manager = self.player.event_manager()
    event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, self.end_callback)

    self.player.play()

    while self.stillPlaying:
        #do nothing
        pass

And I call it like this:

    play("http://www.youtube.com/watch?v=2hP-UGqA1Ek")

If you do use VLC media player to play this type of link, it will 'find' the video and play it. But all that happens with my code is a standard HTTP GET for the page, and a return of the page content from YouTube, but no video.

Rexferd answered 11/7, 2012 at 16:10 Comment(2)
I think the problem is that the VLC player uses the youtube.lua script to find the true location of the video and then passes this to the player. I will simply need to do that myself somehow.Rexferd
And here is Fixed VLC youtube.lua parser at Github.Turku
G
3

That's because the link you provided is NOT the link to the video. VLC media player can get the actual link to the YouTube video, but libVLC will not do this.

Gonsalve answered 22/10, 2013 at 15:54 Comment(0)
M
1

I had the same problem recently but the youtube.lua file seems to have been long fixed.

Thanks to a tip from Rémi Denis-Courmont, one of the VLC developers, I solved this problem by creating a MediaListPlayer and then adding the media (local file or YouTube URL) to the MediaList then associating that to the player. I am not sure why such a convoluted approach is needed, but at least it works for me.

   #movie is the YouTube or a local URL
   media = instance.media_new(movie)
   media_list = instance.media_list_new([movie]) #A list of one movie

   player = instance.media_player_new()
   player.set_media(media)

   #Create a new MediaListPlayer instance and associate the player and playlist with it

   list_player =  instance.media_list_player_new()
   list_player.set_media_player(player)
   list_player.set_media_list(media_list)
Micahmicawber answered 3/4, 2015 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.