File size: 2,906 Bytes
68bf686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
![pytube](https://s3.amazonaws.com/assets.nickficano.com/pytube_logo.png)

# A lightwight, dependency-free YouTube Video download library, written in Python.

Downloading videos from YouTube shouldn't require some bloatware application,
its usually a niche condition you need to do so in the first place.

## 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.org](http://pyvideo.org/).

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.

## Principals 

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

## Planned Features

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

- Allow it to run as a command-line utility. 
- Making it compatible with Python 3.


## Usage

``` python
from youtube import YouTube

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

yt = YouTube()

# Set the video URL.
yt.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.

pp(yt.videos)

#[<Video: 3gp - 144p>,
# <Video: 3gp - 144p>,
# <Video: 3gp - 240p>,
# <Video: 3gp - 240p>,
# <Video: flv - 224p>,
# <Video: flv - 224p>,
# <Video: flv - 360p>,
# <Video: flv - 360p>,
# <Video: flv - 480p>,
# <Video: flv - 480p>,
# <Video: mp4 - 360p>,
# <Video: mp4 - 360p>,
# <Video: mp4 - 720p>,
# <Video: mp4 - 720p>,
# <Video: webm - 360p>,
# <Video: webm - 360p>,
# <Video: webm - 480p>,
# <Video: 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.filename = 'Dancing Scene from Pulp Fiction'

# You can also filter the criteria by filetype.

pp(yt.filter('flv'))

# [<Video: flv - 224p>,
# <Video: flv - 224p>,
# <Video: flv - 360p>,
# <Video: flv - 360p>,
# <Video: flv - 480p>,
# <Video: flv - 480p>]

# and by resolution
pp(yt.filter(res='480p'))

# [<Video: flv - 480p>,
# <Video: flv - 480p>,
# <Video: webm - 480p>,
# <Video: webm - 480p>]

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

video = yt.get('mp4', '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/')
```