File size: 4,884 Bytes
3c3f846
 
 
6d01af7
3c3f846
6d01af7
3c3f846
 
6d01af7
3c3f846
 
 
6d01af7
630c299
 
 
d465490
630c299
 
 
3c3f846
 
6d01af7
3c3f846
 
 
 
 
 
 
 
 
4fda591
3c3f846
 
 
 
 
eb9f6ea
3c3f846
 
 
 
 
eb9f6ea
3c3f846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a8ee23
3c3f846
 
 
 
 
 
 
 
 
4fda591
3c3f846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68dad8e
11b97fc
3c3f846
 
 
 
4fda591
3c3f846
 
 
 
 
 
 
 
 
 
 
 
11b97fc
3c3f846
 
 
eb6c785
3c3f846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4dd656
3c3f846
 
 
 
 
 
 
 
 
 
 
 
 
 
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
======
pytube
======

A lightweight, dependency-free Python library for downloading YouTube Videos.

Description
===========

Downloading videos from YouTube shouldn't require some bloatware application,
it's usually a niche condition you want to do so in the first place. So I
present to you, PyTube!

Requirements
============

- Python 2.6+ (2.7 or 3.4 recommended)
- PIP (for some installation methods)
- GIT (for some installation methods)

Installation
============

If you are on Mac OS X or Linux, chances are that one of the following two commands will work for you:

Installation
============

Using PIP via PyPI

.. code:: bash

    pip install pytube

Using PIP via Github

.. code:: bash

    pip install git+git://github.com/NFicano/pytube.git@0.2.1#egg=pytube

Adding to your ``requirements.txt`` file (run ``pip install -r requirements.txt`` afterwards)

.. code:: bash

    git+ssh://git@github.com/NFicano/pytube.git@0.2.1#egg=pytube

Manually via GIT

.. code:: bash

    git clone git://github.com/NFicano/pytube.git pytube
    cd pytube
    python setup.py install

Roadmap
=======

The only features I see implementing in the near future are:

- refactor console printing into separate command-line utility.
- Add nosetests
- Add Sphinx documentation

Usage Example
=============

.. code:: python

    from pytube import YouTube

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

    yt = YouTube()

    # Set the video URL.
    yt.from_url("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 perticular video by printing videos.

    pprint(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>]

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

    # view the auto generated filename:
    from __future__ import print_function
    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.

    pprint(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
    pprint(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:

    pprint(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>]

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

    video = yt.get('mp4')
    # MultipleObjectsReturned: get() returned more than one object -- it returned 2!

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

    # Okay, let's download it!
    video.download()

    # Downloading: Pulp Fiction - Dancing Scene.mp4 Bytes: 37561829
    # 37561829  [100.00%]

    # Note: If you wanted to choose the output directory, simply pass it as an
    # argument to the download method.
    video.download('/tmp/')


Background
==========

After missing the deadline to register for PyCon 2012, I decided to write what
became PyTube and crawler to collect all the YouTube links for the talks
on PyVideos_.

To avoid having to encode them to mp4 (so I could watch them on my iPhone)
I wrote it so you could specify an encoding format.

In recently weeks interest has picked up in the project, so I decided to
dedicate more time to further its development and actively maintain it.

Philosophy
==========

My only real goal for this is to never require any third party dependancies,
to keep it simple and make it reliable.

.. _PyVideos: http://pyvideo.org/