2

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.

Bharel
  • 23,672
  • 5
  • 40
  • 80
BOSS
  • 23
  • 3
  • I'm not sure but I heard that YouTube removed button `Dislike` from web page - so now `Pafy` may need also changes. So you may have to wait for newer version. – furas Nov 23 '21 at 06:38
  • 1
    Oh, I see. But I hate the decision that youtube took, I mean the like and dislike buttons are an important aspect to judge a video. – BOSS Nov 23 '21 at 07:49
  • Its not about the dislike button, Google/youtube doenst allow you to reproduce his songs in a script. If you look in GeeksForGeeks from you code come from, you will see that Pafy its a great tool to take the information about the video, like views.. authors .. but doesnt work to run the link. you could use url_video = "someYoutubeUrl" webbrowser.open_new(url_video) – Enrique Benito Casado Nov 23 '21 at 15:15
  • Thanks for your info. I'll definitely try it – BOSS Nov 24 '21 at 05:17
  • As @EnriqueBenitoCasado said I tried doing it but, it didn't work either but also gave me an error `>>> url_video = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" msedge.open_new(url_video) File "", line 1 url_video = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" msedge.open_new(url_video) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid syntax. Perhaps you forgot a comma? >>>` – BOSS Nov 25 '21 at 09:47
  • Does this answer your question? [backend\_youtube\_dl.py", line 54, in \_fetch\_basic self.\_dislikes = self.\_ydl\_info\['dislike\_count'\] KeyError: 'dislike\_count'](https://stackoverflow.com/questions/70344739/backend-youtube-dl-py-line-54-in-fetch-basic-self-dislikes-self-ydl-info) – Bharel Dec 14 '21 at 07:14
  • Yep, got it . . . – BOSS Dec 24 '21 at 01:01

2 Answers2

2

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
1

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)
Enrique Benito Casado
  • 1,914
  • 1
  • 20
  • 40