File size: 10,978 Bytes
3c3f846
 
 
6d01af7
f80bb17
 
 
 
867e21e
 
 
 
6513d40
 
305c884
6513d40
f84714c
305c884
12d8cde
 
8c2ee02
 
 
 
4ef9def
6d01af7
af737f9
 
 
 
 
 
 
 
 
 
 
 
 
4463e34
 
 
 
 
 
 
 
 
 
 
 
3c3f846
4463e34
6d01af7
0b85c2d
3c3f846
28b8b1f
3c3f846
4fda591
3c3f846
dc9f179
4463e34
3c3f846
dc9f179
3c3f846
aaadb3c
3c3f846
623602b
f5271a3
3c3f846
dc9f179
3c3f846
dc9f179
3c3f846
dc9f179
3c3f846
dc9f179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c3f846
dc9f179
3c3f846
dc9f179
3c3f846
dc9f179
3c3f846
dc9f179
a376a30
28b8b1f
a376a30
dc9f179
 
 
 
 
 
a376a30
dc9f179
a376a30
28b8b1f
a376a30
dc9f179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82e4ebb
28b8b1f
82e4ebb
dc9f179
 
 
 
 
 
e82fe42
82e4ebb
dc9f179
82e4ebb
28b8b1f
82e4ebb
dc9f179
 
 
 
 
 
 
 
 
 
e82fe42
a376a30
dc9f179
a376a30
28b8b1f
a376a30
dc9f179
 
 
 
 
a376a30
dc9f179
a376a30
28b8b1f
a376a30
dc9f179
 
a376a30
86b6ff8
dc9f179
e82fe42
28b8b1f
e82fe42
28b8b1f
82c4e89
dc9f179
28b8b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
======
pytube
======

.. image:: https://img.shields.io/pypi/v/pytube.svg
  :alt: Pypi
  :target: https://pypi.python.org/pypi/pytube/

.. image:: https://travis-ci.org/nficano/pytube.svg?branch=master
   :alt: Build status
   :target: https://travis-ci.org/nficano/pytube

.. image:: https://readthedocs.org/projects/python-pytube/badge/?version=latest
  :alt: Documentation Status
  :target: http://python-pytube.readthedocs.io/en/latest/?badge=latest

.. image:: https://coveralls.io/repos/github/nficano/pytube/badge.svg?branch=master#23e6f7ac56dd3bde
  :alt: Code Coverage
  :target: https://coveralls.io/github/nficano/pytube?branch=master

.. image:: https://img.shields.io/pypi/pyversions/pytube.svg
  :alt: Python Versions
  :target: https://pypi.python.org/pypi/pytube/

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

-------------------

**Behold, a perfect balance of simplicity versus flexibility**::

    >>> YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()
    >>> yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')
    >>> yt.streams
    ... .filter(progressive=True, file_extension='mp4')
    ... .order_by('resolution')
    ... .desc()
    ... .first()
    ... .download()

Features
--------

- Support for Both Progressive & DASH Streams
- Easily Register ``on_download_progress`` & ``on_download_complete`` callbacks
- Command-line Interfaced Included
- Caption Track Support
- Outputs Caption Tracks to .srt format (SubRip Subtitle)
- Ability to Capture Thumbnail URL.
- Extensively Documented Source Code
- No Third-Party Dependencies

Installation
------------

Download using pip via pypi.

.. code-block:: bash

    pip install pytube

Getting started
---------------

Let's begin with showing how easy it is to download a video with pytube:

.. code-block:: python

   >>> from pytube import YouTube
   >>> YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.first().download()

This example will download the highest quality progressive download stream available.

Next, let's explore how we would view what video streams are available:

.. code-block:: python

   >>> yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')
   >>> yt.streams.all()
   [<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
   <Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
   <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
   <Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
   <Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
   <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
   <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9">,
   <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
   <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">,
   <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e">,
   <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">,
   <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
   <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">,
   <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
   <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">,
   <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
   <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">,
   <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
   <Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">,
   <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">,
   <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">,
   <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">]

You may notice that some streams listed have both a video codec and audio codec, while others have just video or just audio, this is a result of YouTube supporting a streaming technique called Dynamic Adaptive Streaming over HTTP (DASH).

In the context of pytube, the implications are for the highest quality streams; you now need to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them.

The legacy streams that contain the audio and video in a single file (referred to as "progressive download") are still available, but only for resolutions 720p and below.

To only view these progressive download streams:

.. code-block:: python

    >>> yt.streams.filter(progressive=True).all()
    [<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
    <Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
    <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
    <Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
    <Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">]

Conversely, if you only want to see the DASH streams (also referred to as "adaptive") you can do:

.. code-block:: python

    >>> yt.streams.filter(adaptive=True).all()
    [<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
    <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9">,
    <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
    <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">,
    <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e">,
    <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">,
    <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
    <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">,
    <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
    <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">,
    <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
    <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">,
    <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
    <Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">,
    <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">,
    <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">,
    <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">]


Pytube allows you to filter on every property available (see the documentation for the complete list), let's take a look at some of the most useful ones.

To list the audio only streams:

.. code-block:: python

    >>> yt.streams.filter(only_audio=True).all()
    [<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
    <Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">,
    <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">,
    <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">,
    <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">]


To list only ``mp4`` streams:

.. code-block:: python

    >>> yt.streams.filter(subtype='mp4').all()
    [<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
    <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
    <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
    <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
    <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e">,
    <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
    <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
    <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
    <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">]


Multiple filters can also be specified:

.. code-block:: python

    >>> yt.streams.filter(subtype='mp4', progressive=True).all()
    >>> # this can also be expressed as:
    >>> yt.streams.filter(subtype='mp4').filter(progressive=True).all()
    [<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
    <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">]

You also have an interface to select streams by their itag, without needing to filter:

.. code-block:: python

    >>> yt.streams.get_by_itag(22)
    <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">


If you need to optimize for a specific feature, such as the "highest resolution" or "lowest average bitrate":

.. code-block:: python

    >>> yt.streams.filter(progressive=True).order_by('resolution').desc().all()

Note that ``order_by`` cannot be used if your attribute is undefined in any of the Stream instances, so be sure to apply a filter to remove those before calling it.

If your application requires post-processing logic, pytube allows you to specify an "on download complete" callback function:

.. code-block:: python

    >>> def convert_to_aac(stream, file_handle):
            # do work
    >>> yt.register_on_complete_callback(convert_to_aac)


Similarly, if your application requires on-download progress logic, pytube exposes a callback for this as well:

.. code-block:: python

    >>> def show_progress_bar(stream, chunk, file_handle, bytes_remaining):
            # do work
    >>> yt.register_on_progress_callback(show_progress_bar)



Command-line interface
======================

pytube also ships with a tiny cli interface for downloading and probing videos.

Let's start with downloading:

.. code-block:: bash

    pytube http://youtube.com/watch?v=9bZkp7q19f0 --itag=22

To view available streams:

.. code-block:: bash

    pytube http://youtube.com/watch?v=9bZkp7q19f0 --list


Finally, if you're filing a bug report, the cli contains a switch called ``--build-playback-report``, which bundles up the state, allowing others to easily replay your issue.