November 14, 2019
How to play YouTube videos with Python and Pafy

How to play YouTube videos with Python and Pafy

YouTube is a video sharing service that allows users to watch videos posted by other users and upload videos of their own. It is currently the largest video social network, with 1,300,000,000 users in 2019 and 5 billion videos watched every single day. The platform makes a perfect place for us to learn many new things, with an unless number of available videos. We can also listen to music, watch fun animations and movies etc... But say a situation we have to play the video outside of a browser, say a media player, played by keys? No, we are not going to download them. Python, an amazing language, can help us automate this thing with the help of a module - Pafy.

What is the module used to play YouTube videos using Python?

Pafy is a Python library that can play content from YouTube in your chosen media player. Created by np1 this library is super simple to install and use.

What can this module do?

  • Retreive metadata such as viewcount, duration, rating, author, thumbnail, keywords
  • Download video or audio at requested resolution / bitrate / format / filesize
  • Command line tool (ytdl) for downloading directly from the command line
  • Retrieve the URL to stream the video in a player such as vlc or mplayer
  • Works with age-restricted videos and non-embeddable videos
  • Small, standalone, single importable module file (pafy.py)
  • Select highest quality stream for download or streaming
  • Download video only (no audio) in m4v or webm format
  • Download audio only (no video) in ogg or m4a format
  • Retreive playlists and playlist metadata
  • Works with Python 2.6+ and 3.3+
  • Optionally depends on youtube-dl (recommended; more stable)

How to install it?

On linux

Use the terminal and pip.

sudo pip install pafy

On windows

pip.exe install pafy

How to use it

Using your favourite Python editor.
To use pafy we first need to import the library, and then tell it the URL of the YouTube video. In this case an LGR video where he unboxes lots of retro computer stuff.

import pafy
url = "https://www.youtube.com/watch?v=1yNfzVABvCM"

Now lets tell pafy about the URL.

video = pafy.new(url)

Examples of Pafy features

Pafy can also be used to get the details of a video, so lets get the title of the video that we have chosen.

video.title

This will return

LGR - Opening Stuff You Sent Me! October 2018

Or we can see the rating, and see a description of the chosen video by using...

video.rating
print(video.description)

Another cool feature is that we can also check out the best video formats for the media.

best = video.getbest()

And to find out the best URL that we can use to stream with VLC...

best.url

And to play that URL with the Python 3 VLC library...

import vlc, pafy
url = "https://www.youtube.com/watch?v=1yNfzVABvCM"
video = pafy.new(url)
best = video.getbest()
media = vlc.MediaPlayer(best.url)
media.play()

Ok so can I build something quick with it?


Yup, lets make a quick app that will ask for a YouTube URL and then open it in VLC. For this we need to have VLC and VLC Python library installed.
As usual we import the libraries at the start.

import vlc, pafy

Then we create a variable called "url" and in there we store the users URL using the input function.

url = input("Paste your YouTube URL here :>> ")

Then we tell pafy about the video.

video = pafy.new(url)

Get the best video stream.

best = video.getbest()
media = vlc.MediaPlayer(best.url)

Play the media.

media.play()

Run the code and it will ask for the YouTube URL.

If you want to stop the video, in the REPL (Python shell)

media.stop()
Join the discussion

Danny2507 Blog