Fast start fapy

Keywords: pip Python Programming github

Fast start fapy

Understanding of fapy package

Today, when I was paddling the water, I picked YouTube. After a fierce operation, I couldn't find the url I wanted. So I googled it and found the fapy library. So I translated it for downloading YouTube content and retrieving metadata. The pay object is related to videos hosted on YouTube. They contain metadata such as title, number of views, author, and video ID.


pip install pafy


  • Search metadata, such as views, duration, rating, author, thumbnail, keywords
  • Download video or audio at the required resolution / bit rate / format / file size
  • Command line tools (ytdl), which can be downloaded directly from the command line
  • Retrieve URL to stream video in players such as vlc or mplayer
  • For age limited and non embeddable videos
  • Small independent single importable module file( )
  • Choose the highest quality stream for download or streaming
  • Download only M4V or WebM format video (no audio)
  • Download audio only in ogg or m4a format (no video)
  • Retrieve playlists and playlist metadata
  • For Python 2.6 + and 3.3+
  • (optional) depends on YouTube DL (recommended; more stable)

Examples of use

This is the way to use the module in your own python code. For a description of the command-line tool (ytdl), see below

>>> import pafy

Create a video instance using the YouTube web address:

>>> url = ""
>>> video =

Get the corresponding property

>>> video.title   #title
'Richard Jones: Introduction to game programming - PyCon 2014'
>>> video.rating    # score
>>> video.viewcount,, video.length    # Number of views, author, length
(1916, 'PyCon 2014', 10394)
>>> video.duration, video.likes, video.dislikes   # Duration, like count, don't like count
('02:53:14', 25, 0)

>>> print(video.description)   #describe
Speaker: Richard Jones

This tutorial will walk the attendees through development of a simple game using PyGame with time left over for some experimentation and exploration of different types of games.

Slides can be found at: and

List available streams for video

>>> streams = video.streams
>>> for s in streams:
...     print(s)

Display all formats, file size and download URL:

>>> for s in streams:
...    print(s.resolution, s.extension, s.get_filesize(), s.url)
1280x720 mp4 2421958510[...]
640x360 webm 547015732[...]
640x360 mp4 470655850[...]
320x240 flv 345455674[...]
320x240 3gp 208603447[...]
176x144 3gp 60905732[...]

Get the best resolution regardless of file format:

>>> best = video.getbest()
>>> best.resolution, best.extension
('1280x720', 'mp4')

Get the best resolution for a specific file format: (mp4, webm, flv or 3gp)

>>> best = video.getbest(preftype="webm")
>>> best.resolution, best.extension
('640x360', 'webm')

Obtain the web address for downloading or streaming in mplayer / vlc, etc.:

>>> best.url

Download video and show progress:

3,734,976 Bytes [0.20%] received. Rate: [ 719 KB/s].  ETA: [3284 secs]

Download the video using a specific directory and / or filename:

>>> filename ="/tmp/")

>>> filename ="/tmp/Game." + best.extension)

Get pure audio stream (m4a and / or ogg vorbis):

>>> audiostreams = video.audiostreams
>>> for a in audiostreams:
...     print(a.bitrate, a.extension, a.get_filesize())
256k m4a 331379079
192k ogg 172524223
128k m4a 166863001
128k ogg 108981120
48k m4a 62700449

Download the second audio stream from the list above:

>>> audiostreams[1].download()

Get the best quality audio stream:

>>> bestaudio = video.getbestaudio()
>>> bestaudio.bitrate

Download the best quality audio file:


Show all media types of video (Video + audio, video only and audio only):

>>> allstreams = video.allstreams
>>> for s in allstreams:
...     print(s.mediatype, s.extension, s.quality)

normal mp4 1280x720
normal webm 640x360
normal mp4 640x360
normal flv 320x240
normal 3gp 320x240
normal 3gp 176x144
video m4v 1280x720
video webm 1280x720
video m4v 854x480
video webm 854x480
video m4v 640x360
video webm 640x360
video m4v 426x240
video webm 426x240
video m4v 256x144
video webm 256x144
audio m4a 256k
audio ogg 192k
audio m4a 128k
audio ogg 128k
audio m4a 48k

Usage of command line tools (ytdl)

usage: ytdl [-h] [-i] [-s]
            [-t {audio,video,normal,all} [{audio,video,normal,all} ...]]
            [-n N] [-b] [-a]

YouTube Download Tool

positional arguments:
  url                   YouTube video URL to download

optional arguments:
  -h, --help            show this help message and exit
  -i                    Display vid info
  -s                    Display available streams
  -t {audio,video,normal,all} [{audio,video,normal,all} ...]
                        Stream types to display
  -n N                  Specify stream to download by stream number (use -s to
                        list available streams)
  -b                    Download the best quality video (ignores -n)
  -a                    Download the best quality audio (ignores -n)

ytdl example:


pip install youtube-dl

Download the best available resolution (- b):

$ ytdl -b ""

Download the best available audio stream (- a) (note; a full URL is not required, just a video ID is sufficient):

$ ytdl -a cyMHZVT91Dw

Get video information (- i):

$ ytdl -i cyMHZVT91Dw

List available download streams:

$ ytdl cyMHZVT91Dw

Stream Type    Format Quality         Size
------ ----    ------ -------         ----
1      normal  webm   [640x360]       33 MB
2      normal  mp4    [640x360]       23 MB
3      normal  flv    [320x240]       14 MB
4      normal  3gp    [320x240]        9 MB
5      normal  3gp    [176x144]        3 MB
6      audio   m4a    [48k]            2 MB
7      audio   m4a    [128k]           5 MB
8      audio   ogg    [128k]           5 MB
9      audio   ogg    [192k]           7 MB
10     audio   m4a    [256k]          10 MB

Download mp4 640x360 (stream 2):

$ ytdl -n2 cyMHZVT91Dw

Download m4a audio stream at 256k bit rate:

$ ytdl -n10 cyMHZVT91Dw

Note: after testing, ytdl needs to use YouTube DL

Posted by frikus on Thu, 18 Jun 2020 21:15:56 -0700