1

我正在尝试设置收音机和 youtube 播放列表播放器

我的代码适用于广播和单个 youtube 链接,但播放列表显示链接无法识别,somoene 可以帮忙吗?

import os
from os import path
from random import randrange
from tkinter.ttk import Combobox
import pafy
import requests
import vlc
import time
from tkinter import *
from tkinter import messagebox,PhotoImage

from bs4 import BeautifulSoup

root = Tk()

global phplay,phpause,phstop
global pausevar
pausevar = ""
phplay = PhotoImage(file=r'img/play.png')
phpause = PhotoImage(file=r'img/pause.png')
phstop = PhotoImage(file=r'img/stop.png')


frmradio = LabelFrame(root, text="Radio Player", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
frmradio.grid(row=0, column=0, sticky=NW, pady=2)



def getradiolist():
    var1 = open("Confs/lstradios.txt", "r").readlines()
    data = []

    for line in var1:
        if len(line) > 1:
            estacao, url, = line.split('<=>')
            data.append(estacao)
    return data

valradio = StringVar()
imp_radio = Combobox(frmradio, textvariable=valradio, height=10, width=47)
imp_radio['values'] = getradiolist()
imp_radio['state'] = 'readonly'
imp_radio.grid(row=1, column=0,columnspan=5, pady=2, sticky="ew")

# define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
# Define VLC player
player = instance.media_player_new()
instance.log_unset()


def startplayer(pausevar):
    if pausevar == "sim":
        pausevar=""
        player.pause()


    player.stop()
    esta = imp_radio.get()
    if len(esta)>1:
        var1 = open("Confs/lstradios.txt", "r").readlines()
        for line in var1:
            if len(line) > 1:
                if esta in line:
                    estacao, url, = line.split('<=>')
                    break

        if "youtube" in estacao:

            playlist = pafy.get_playlist(url)
            items = playlist["items"]# getting playlist items
            def loop_play():
                item = items[randrange(len(items))]
                i_pafy = item['pafy']
                y_url = i_pafy.watchv_url
                video = pafy.new(y_url)
                best = video.getbest()
                media = instance.media_new(best.y_url.strip())  # por testar ainda
                player.set_media(media)
                frmradio.config(text=str(playlist["title"]))
                player.play()

            loop_play()
        else:
            media = instance.media_new(url.strip())
            player.set_media(media)
            player.play()


        frmradio.config(text=str("Radio Player : Playing: " + estacao))


def stopplauyer():
    player.stop()
    frmradio.config(text=str("Radio Player"))

def pauseplauyer():
    global pausevar
    pausevar = "sim"
    player.pause()
    frmradio.config(text=str("Radio Player : Pause!"))



Button(frmradio, width="150",height="28", text="Play", image=phplay, command=lambda pausevar=pausevar: startplayer(pausevar)).grid(row=0, column=0, sticky=N + S + E + W)
Button(frmradio, width="100",height="28", text="Pause", image=phpause, command=lambda: pauseplauyer()).grid(row=0, column=1, sticky=N + S + E + W)
Button(frmradio, width="100",height="28", text="Stop", image=phstop,command=lambda: stopplauyer()).grid(row=0, column=2, sticky=N + S + E + W)





if __name__ == '__main__':
    root.mainloop()

收到此错误:

Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/init .py”,第 1892 行,调用 返回 self.func ( *args) 文件“/Users/ricardosimoes/PycharmProjects/OCPP/yplayer.py”,第 105 行,在 Button(frmradio, width="150",height="28", text="Play", image=phplay, command =lambda pausevar=pausevar: startplayer(pausevar)).grid(row=0, column=0, sticky=N + S + E + W)
文件“/Users/ricardosimoes/PycharmProjects/OCPP/yplayer.py”,第 70 行,在 startplayer 播放列表 = pafy.get_playlist2(url) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ site-packages/pafy/playlist.py”,第 316 行,在 get_playlist2 中返回 Playlist.from_url(playlist_url, basic, gdata, size, callback) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3. 9/site-packages/pafy/playlist.py”,第 175 行,在 from_url t = cls(url, basic, gdata, size, callback) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3 .9/site-packages/pafy/playlist.py",第 146 行,在初始化中 引发 ValueError(err % playlist_url) ValueError:无法识别的播放列表网址: https ://www.youtube.com/playlist?list=PLr5JVJSLVg79UpgS6gdrcINWt86npzXkz

Listradios.txt 是

youtube <=> https://www.youtube.com/playlist?list=PLr5JVJSLVg79UpgS6gdrcINWt86npzXkz

超级点击 <=> http://19553.live.streamtheworld.com:80/MEGA_HITS_SC

4

0 回答 0