VLC Python doesn't play YouTube video

喜欢而已 提交于 2020-01-04 14:17:10

问题


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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/11436864/vlc-python-doesnt-play-youtube-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!