Skip to main content
https://img.shields.io/pypi/v/Pafy.svg https://img.shields.io/pypi/dm/Pafy.svg https://img.shields.io/coveralls/np1/pafy/develop.svg Code Health https://travis-ci.org/mps-youtube/pafy.svg?branch=develop Wheel Status

Features

  • 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+

  • Depends on youtube-dl

Documentation

Full documentation is available at http://pythonhosted.org/pafy

Usage Examples

Here is how to use the module in your own python code. For command line tool (ytdl) instructions, see further below

>>> import pafy

create a video instance from a YouTube url:

>>> url = "https://www.youtube.com/watch?v=bMt47wvK6u0"
>>> video = pafy.new(url)

get certain attributes:

>>> video.title
'Richard Jones: Introduction to game programming - PyCon 2014'

>>> video.rating
5.0

>>> video.viewcount, video.author, video.length
(1916, 'PyCon 2014', 10394)

>>> video.duration, video.likes, video.dislikes
('02:53:14', 25, 0)

>>> print(video.description)
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: https://speakerdeck.com/pycon2014 and https://github.com/PyCon/2014-slides

list available streams for a video:

>>> streams = video.streams
>>> for s in streams:
...     print(s)
...
normal:mp4@1280x720
normal:webm@640x360
normal:mp4@640x360
normal:flv@320x240
normal:3gp@320x240
normal:3gp@176x144

show all formats, file-sizes and their download url:

>>> for s in streams:
...    print(s.resolution, s.extension, s.get_filesize(), s.url)
...
1280x720 mp4 2421958510 https://r1---sn-aiglln7e.googlevideo.com/videoplayba[...]
640x360 webm 547015732 https://r1---sn-aiglln7e.googlevideo.com/videoplaybac[...]
640x360 mp4 470655850 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
320x240 flv 345455674 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
320x240 3gp 208603447 https://r1---sn-aiglln7e.googlevideo.com/videoplayback[...]
176x144 3gp 60905732 https://r1---sn-aiglln7e.googlevideo.com/videoplayback?[...]

get best resolution regardless of file format:

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

get best resolution for a particular file format: (mp4, webm, flv or 3gp)

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

get url, for download or streaming in mplayer / vlc etc:

>>> best.url
'http://r12---sn-aig7kner.c.youtube.com/videoplayback?expire=1369...

Download video and show progress:

>>> best.download(quiet=False)
3,734,976 Bytes [0.20%] received. Rate: [ 719 KB/s].  ETA: [3284 secs]

Download video, use specific directory and/or filename:

>>> filename = best.download(filepath="/tmp/")

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

Get audio-only streams (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 2nd audio stream from the above list:

>>> audiostreams[1].download()

Get the best quality audio stream:

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

Download the best quality audio file:

>>> bestaudio.download()

show all media types for a 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

Installation

pafy can be installed using pip:

$ [sudo] pip install pafy

or use a virtualenv if you don’t want to install it system-wide:

$ virtualenv venv
$ source venv/bin/activate
$ pip install pafy

Alternatively you can just grab the pafy.py file and import it in your python code:

wget https://raw.githubusercontent.com/np1/pafy/master/pafy/pafy.py

Command Line Tool (ytdl) Usage

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

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 Examples

Download best available resolution (-b):

$ ytdl -b "http://www.youtube.com/watch?v=cyMHZVT91Dw"

Download best available audio stream (-a) (note; the full url is not required, just the video id will suffice):

$ ytdl -a cyMHZVT91Dw

get video info (-i):

$ ytdl -i cyMHZVT91Dw

list available dowload 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 (ie. stream number 2):

$ ytdl -n2 cyMHZVT91Dw

Download m4a audio stream at 256k bitrate:

$ ytdl -n10 cyMHZVT91Dw

IRC

The mps-youtube irc channel (#mps-youtube on Freenode) can be used for discussion of pafy.

Release files for pafy 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pafy 0.4.1
File Size Uploaded
pafy-0.4.1.tar.gz 17.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pafy 0.4.1
File Interpreter ABI Platform
pafy-0.4.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 37.6 kB

Release files / pafy-0.4.1.tar.gz

Download URL pafy-0.4.1.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
74ce7b9e011072a44ffc635f16bf6844f418a05275c2faf8d79b00c833e7b4fd
BLAKE2b-256 checksum
How to use checksums
5bf3ffedc49c278dc398dfa083265f609ba61f9767ceb0eb28d98f1d6107d51c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pafy-0.4.1-py2.py3-none-any.whl

Download URL pafy-0.4.1-py2.py3-none-any.whl
Size 20.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6dd3ad95d049a46f76a15690a1a06938b543e650c129fade123bc6b7ceb73d9a
BLAKE2b-256 checksum
How to use checksums
c37598bd18632073361e861e01306610fc3507c3952a5a0e6b9a5591e182e8b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.5.5

2 release files

0.5.4

2 release files

0.5.3.1

1 release file

0.5.3

1 release file

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.80

2 release files

0.3.78

2 release files

0.3.68

2 release files

0.3.66

2 release files

0.3.64

2 release files

0.3.62

2 release files

0.3.60

2 release files

0.3.58

2 release files

0.3.56

2 release files

0.3.54

2 release files

0.3.52

2 release files

0.3.50

2 release files

0.3.48

2 release files

0.3.44

2 release files

0.3.42

1 release file

0.3.41

1 release file

0.3.40

1 release file

0.3.39

1 release file

0.3.38

1 release file

0.3.37

1 release file

0.3.36

1 release file

0.3.35

1 release file

0.3.34

1 release file

0.3.33

1 release file

0.3.32

1 release file

0.3.31

1 release file

0.3.30

1 release file

0.3.29

1 release file

0.3.28

1 release file

0.3.27

1 release file

0.3.26

1 release file

0.3.25

1 release file

0.3.24

1 release file

0.3.23

1 release file

0.3.22

1 release file

0.3.21

1 release file

0.3.20

1 release file

0.3.09

1 release file

0.3.08

1 release file

0.3.07

1 release file

0.3.06

1 release file

0.3.05

1 release file

0.3.04

1 release file

0.3.03

1 release file

0.3.02

1 release file

0.3.01

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page