File size: 6,504 Bytes
3c3f846
 
 
6d01af7
f80bb17
 
 
 
867e21e
 
 
 
e39ac5b
55b5e2f
e39ac5b
bd801d8
4ef9def
6d01af7
a7a246f
 
 
 
3c3f846
 
6d01af7
8b61afa
24c111e
53e575c
24c111e
037dfcd
24c111e
037dfcd
47d0ce8
4ef9def
6d01af7
3c3f846
 
6d01af7
0b85c2d
3c3f846
 
 
4fda591
3c3f846
 
0b85c2d
3c3f846
 
9a8ee23
3c3f846
 
 
d30f19e
3c3f846
 
8740488
3c3f846
 
44c7902
3c3f846
9db1239
3c3f846
8740488
 
 
 
 
 
 
 
 
3c3f846
8740488
 
3c3f846
 
11b97fc
3c3f846
8740488
3c3f846
 
4fda591
3c3f846
 
54d8944
3c3f846
8740488
 
 
3c3f846
8740488
3c3f846
 
11b97fc
8740488
3c3f846
8740488
54d8944
3c3f846
8740488
2ffed80
3c3f846
8740488
3c3f846
 
 
 
 
 
 
54d8944
3c3f846
 
 
 
 
 
 
 
 
 
 
2ffed80
8740488
3c3f846
 
8740488
3c3f846
 
 
 
ed61858
3c3f846
a376a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e82fe42
82e4ebb
 
 
 
 
e82fe42
 
 
 
 
 
 
 
 
82e4ebb
 
 
 
 
 
e82fe42
a376a30
 
 
 
 
 
 
 
 
 
 
 
93c0502
a376a30
86b6ff8
 
 
5eb3c78
86b6ff8
 
 
 
6471923
f2d9574
 
e82fe42
 
 
 
 
f2d9574
e82fe42
f2d9574
e82fe42
e2ae723
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
======
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://coveralls.io/repos/nficano/pytube/badge.svg?branch=master&service=github
  :alt: Coverage
  :target: https://coveralls.io/github/nficano/pytube?branch=master

*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 <https://github.com/nficano/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.

.. code:: bash

    pip install pytube


Library usage
=============

.. code:: python

    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:

.. code:: bash

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


Same thing for specifying a resolution:

.. code:: bash

   $ 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

.. code:: bash

   $ 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

.. code:: bash

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



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

.. code:: bash

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

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

.. code:: bash

   $ 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:

.. code:: bash

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