1

我今年开始使用 python,我正在做一个音乐播放器,可以播放 youtube 视频,而不需要下载它们。我将 tkinter 用于 gui,vlc 生成一个新窗口,并从 youtube 获取视频。由于 youtube-dl 的移除政策,我已经下载了 youtube-dlc 的 31.10 版本,但是我的程序说 pafy 不再存在......有没有办法在没有 pafy 的情况下做同样的事情?

def set_media(self):
        url = "youtube url"
        # creating pafy object of the video 
        video = pafy.new(url) 

        # getting best stream 
        best = video.getbest() 

        # creating vlc media player object 
        self.media = vlc.MediaPlayer(best.url)
self.get_media.play() 

我试过这个:

import vlc
import time


url = "https://www.youtube.com/watch?v=s5TIulzXoXo"
instance = vlc.Instance()
media = instance.media_new(url)
player = instance.media_player_new()
player.set_media(media)
player.play()

time.sleep(1.5)
duration = player.get_length() / 1000
time.sleep(duration)

...但返回 0 并且它不播放。播放器似乎无法获取媒体。你怎么看?谢谢!

4

1 回答 1

0

您需要解析媒体并播放其第一个子项。

尝试

url = "https://www.youtube.com/watch?v=s5TIulzXoXo"
instance = vlc.Instance()
media = instance.media_new(url)
media.parse()
subitems = media.subitems()

player = instance.media_player_new()
player.set_media(subitems.first()) // select first subitems
player.play()
于 2020-11-04T06:19:49.723 回答