pythonpython-3.xyoutubevlcpafy

Error in python script using python-vlc and pafy


Recently I have created a python script to play youtube videos using pafy and python-vlc. The below code is the script:

# importing vlc module
import vlc

# importing pafy module
import pafy

# url of the video
url = "https://www.youtube.com/watchv=il_t1WVLNxk&list=PLqM7alHXFySGqCvcwfqqMrteqWukz9ZoE"

# creating pafy object of the video
video = pafy.new(url)

# getting stream at index 0
best = video.streams[0]

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

# start playing video
media.play()

And after running it I get this error:

Traceback (most recent call last):
File "C:\Users\harsh\Desktop\don't.py", line 11, in <module>
video = pafy.new(url)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site- 
packages\pafy\pafy.py", line 124, in new
return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site- 
packages\pafy\backend_youtube_dl.py", line 31, in __init__
super(YtdlPafy, self).__init__(*args, **kwargs)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site- 
packages\pafy\backend_shared.py", line 97, in __init__
self._fetch_basic()
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site- 
packages\pafy\backend_youtube_dl.py", line 54, in _fetch_basic
self._dislikes = self._ydl_info['dislike_count']
KeyError: 'dislike_count'

Please help me with this error. If you have any questions please ask.


Solution

  • Pafy its very usefull tool if you want to extract information about youtube video like, retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords or Download video or audio at requested resolution. But it doesnt work(at least untill now) to play a video. If you want to play a Yt video inside your code use it:

     import webbrowser
    
     url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
     webbrowser.open(url)