Skip to main content
Pypi Build status Coverage

pytube is a lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

A call for contributors

With pytube and python-lambda both continuing to gain momentum, I’m calling for contributors to help build out new features, review pull requests, fix bugs, and maintain overall code quality. If you’re interested, please email me at nficano[at]gmail.com.

Description

YouTube is the most popular video-sharing platform in the world and as a hacker you may encounter a situation where you want to script something to download videos. For this I present to you pytube.

pytube is a lightweight library written in Python. It has no third party dependencies and aims to be highly reliable.

pytube makes zero assumptions, meaning there is no built-in method to get say the “best” quality video, pytube simply exposes all the available formats and resolutions, giving you the developer the power to define what “best” is.

pytube also makes pipelining easy, allowing you to specify callback functions for different download events, such as on progress or on complete.

Finally pytube also includes a command-line utility, allowing you to quickly download videos right from terminal.

Installation

Download using pip via pypi.

pip install pytube

Library usage

from pytube import YouTube

# not necessary, just for demo purposes.
from pprint import pprint

yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")

# Once set, you can see all the codec and quality options YouTube has made
# available for the particular video by printing videos.

print(yt.get_videos())

# [<Video: MPEG-4 Visual (.3gp) - 144p>,
#  <Video: MPEG-4 Visual (.3gp) - 240p>,
#  <Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>,
#  <Video: H.264 (.mp4) - 360p>,
#  <Video: H.264 (.mp4) - 720p>,
#  <Video: VP8 (.webm) - 360p>,
#  <Video: VP8 (.webm) - 480p>]

# The filename is automatically generated based on the video title.  You
# can override this by manually setting the filename.

# view the auto generated filename:
print(yt.filename)

# Pulp Fiction - Dancing Scene [HD]

# set the filename:
yt.set_filename('Dancing Scene from Pulp Fiction')

# You can also filter the criteria by filetype.
print(yt.filter('flv'))

# [<Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>]

# Notice that the list is ordered by lowest resolution to highest. If you
# wanted the highest resolution available for a specific file type, you
# can simply do:
print(yt.filter('mp4')[-1])
# <Video: H.264 (.mp4) - 720p>

# You can also get all videos for a given resolution
print(yt.filter(resolution='480p'))

# [<Video: H.264 (.flv) - 480p>,
#  <Video: VP8 (.webm) - 480p>]

# To select a video by a specific resolution and filetype you can use the get
# method.

video = yt.get('mp4', '720p')

# NOTE: get() can only be used if and only if one object matches your criteria.
# for example:

print(yt.videos)

#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]

# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...

video = yt.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.

# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).

# Okay, let's download it! (a destination directory is required)
video.download('/tmp/')

Command-line usage

You can download a video by simply passing the -e (or --extension=) switch and setting it to the desired filetype:

$ pytube -e mp4 http://www.youtube.com/watch?v=Ik-RsDGPI5Y

Same thing for specifying a resolution:

$ pytube -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y

When run without a resolution or extension, it shows a list of available formats to download

$ pytube http://www.youtube.com/watch?v=Ik-RsDGPI5Y
  Resolution      Extension
 ----------------------------
 0  3gp             144p
 1  3gp             240p
 2  mp4             360p
 3  mp4             720p
 4  webm            360p
 Enter choice:

You can see a list of available formats by passing the -s (or --show-available) flag

$ pytube -s http://www.youtube.com/watch?v=Ik-RsDGPI5Y

You can also specify a download file path (-p or --path=):

$ pytube -e mp4 -p ~/Downloads/ http://www.youtube.com/watch?v=Ik-RsDGPI5Y

and/or optionally choose the filename (-f or --filename=):

$ pytube -e mp4 -f "Dancing Scene from Pulp Fiction" http://www.youtube.com/watch?v=Ik-RsDGPI5Y

Development

Development of this happens on GitHub, patches including tests, documentation are very welcome, as well as bug reports and feature contributions are welcome! Also please open an issue if this tool does not function as you’d expect.

How to release updates

If this is the first time you’re releasing to pypi, you’ll need to run: pip install -r tests/dev_requirements.txt.

Once complete, execute the following commands:

$ git checkout master
$ bumpversion [major|minor|patch]
$
$ python setup.py sdist bdist_wheel upload
$
$ bumpversion --no-tag patch
$ git push origin master --tags

Release files for pytube 6.4.0

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

Source distribution (sdist)

Source distribution for pytube 6.4.0
File Size Uploaded
pytube-6.4.0.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytube 6.4.0
File Interpreter ABI Platform
pytube-6.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 41.4 kB

Release files / pytube-6.4.0.tar.gz

Download URL pytube-6.4.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7702a8384804787fb281ecfd616e1627060a313c0d4253f5aa95b7c378f8d664
BLAKE2b-256 checksum
How to use checksums
c676d0867321ea304d1d53e16ebb6d3628f8d684eb7fd5a9e1494d66cad17d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pytube-6.4.0-py3-none-any.whl

Download URL pytube-6.4.0-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08fdac6076cea91a3cb3b2b0f4647101d89f13dab30dd164f9e1828cfb3c914f
BLAKE2b-256 checksum
How to use checksums
5da786d366ab5b4958bce519cbf10f5f675267a75d59629ee8f6b062e24130e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

12.1.3

2 release files

12.1.2

2 release files

12.1.1

2 release files

12.0.0

2 release files

11.0.2

2 release files

11.0.1

2 release files

10.9.3

2 release files

10.8.5

2 release files

10.8.3

2 release files

10.8.2

2 release files

10.7.2

2 release files

10.6.1

2 release files

10.6.0

2 release files

10.5.1

2 release files

10.4.1

2 release files

10.0.0

2 release files

9.7.3

2 release files

9.7.2

2 release files

9.7.0

2 release files

9.6.0

2 release files

9.5.3

2 release files

9.5.2

2 release files

9.5.1

2 release files

9.5.0

2 release files

9.4.0

2 release files

9.3.5

2 release files

9.3.3

2 release files

9.3.2

2 release files

9.3.0

2 release files

9.2.3

2 release files

9.2.2

2 release files

9.2.0

2 release files

9.1.0

2 release files

9.0.6

2 release files

9.0.4

2 release files

9.0.2

2 release files

9.0.0

2 release files

8.0.2

2 release files

8.0.0

2 release files

7.0.18

2 release files

7.0.16

2 release files

7.0.14

2 release files

7.0.12

2 release files

7.0.10

2 release files

7.0.9

2 release files

7.0.8

2 release files

7.0.6

2 release files

7.0.4

2 release files

7.0.2

2 release files

7.0.0

2 release files

6.4.2

2 release files

This release

6.4.0 This release

2 release files

6.3.0

2 release files

6.2.2

2 release files

6.2.1

1 release file

6.1.11

1 release file

6.1.10

1 release file

6.1.8

1 release file

6.1.5

1 release file

6.1.4

1 release file

6.1.3

1 release file

6.1.2

1 release file

6.1.0

1 release file

6.0.2

1 release file

6.0.1

1 release file

6.0.0

1 release file

5.1.0

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.3.3

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

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