PAFY (Python API For YouTube) is a Python library that does what the name says on the tin - it allows you to access YouTube videos programmatically, get information about them, download them, etc.
Full documentation for Pafy is here.
You can install Pafy with the command: pip install pafy
I tried out Pafy a little, and whatever I tried worked the way the docs said it would.
Here is a simple example of using Pafy to get info about a YouTube video and download it to your machine. I used the video 'Concurrency is not Parallelism' - by Rob Pike, co-inventor of the Go language, in the example. The video is also embedded below:
import pafy # Concurrency is not parallelism - video URL = "https://www.youtube.com/watch?v=cN_DpYBzKso" cinp_url = "https://www.youtube.com/watch?v=cN_DpYBzKso" cinp_video = pafy.new(cinp_url) cv = cinp_video print "Info for video 'Concurrency is not Parallelism' by Rob Pike, Commander, Google:" print 'Title:', cv.title print 'Length:', cv.length print 'Duration:', cv.duration print 'Description:', cv.description """ This fragment prints info about the streams available in the video. streams = cv.streams for s in streams: print(s) """ # Get the best stream for the video. best = cv.getbest() # Download the best stream. best.download(quiet=False, filepath=best.title + "." + best.extension)
The Pafy download() method not only downloads the video, it also shows the size downloaded so far as a percentage, the download speed, and the ETA for the download, in real time.
Also check out my earlier post on another YouTube tool in Python:
youtube-dl, a YouTube downloader in Python
- Vasudev Ram - Dancing Bison Enterprises - Python consulting and training