Error in python script using python-vlc and pafy

1.3k Views Asked by At

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.

2

There are 2 best solutions below

0
Enrique Benito Casado On BEST ANSWER

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)
0
Subin Maharjan On

The dislike button on youtube has been made private, so some modification on backend_youtube_dl.py is required to run pafy.

  • Navigate to C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site- packages\pafy
  • Open backend_youtube_dl.py file
  • Comment or remove this code: self._dislikes = self._ydl_info['dislike_count']

Besides the dislike function, everything else works fine. It worked for me and hope the same goes for everyone.