documentation
Browse files- docs/user/quickstart.rst +128 -7
docs/user/quickstart.rst
CHANGED
|
@@ -16,17 +16,138 @@ Begin by importing the YouTube class::
|
|
| 16 |
|
| 17 |
>>> from pytube import YouTube
|
| 18 |
|
| 19 |
-
|
| 20 |
Now, let's try to download a video. For this example, let's take something
|
| 21 |
popular like PSY - Gangnam Style::
|
| 22 |
|
| 23 |
-
>>>
|
| 24 |
|
| 25 |
-
Now, we have a :class:`YouTube <pytube.YouTube>` object called ``
|
| 26 |
-
can get all the information we need from this object.
|
| 27 |
|
| 28 |
-
The pytube API makes all
|
| 29 |
-
|
| 30 |
|
| 31 |
-
>>>
|
| 32 |
PSY - GANGNAM STYLE(강남스타일) M/V
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
>>> from pytube import YouTube
|
| 18 |
|
|
|
|
| 19 |
Now, let's try to download a video. For this example, let's take something
|
| 20 |
popular like PSY - Gangnam Style::
|
| 21 |
|
| 22 |
+
>>> yt = YouTube('https://www.youtube.com/watch?v=9bZkp7q19f0')
|
| 23 |
|
| 24 |
+
Now, we have a :class:`YouTube <pytube.YouTube>` object called ``yt``.
|
|
|
|
| 25 |
|
| 26 |
+
The pytube API makes all information intuitive to access. For example, this is
|
| 27 |
+
how you would get the video's title::
|
| 28 |
|
| 29 |
+
>>> yt.title
|
| 30 |
PSY - GANGNAM STYLE(강남스타일) M/V
|
| 31 |
+
|
| 32 |
+
And this would be how you would get the thumbnail url::
|
| 33 |
+
|
| 34 |
+
>>> yt.thumbnail_url
|
| 35 |
+
'https://i.ytimg.com/vi/mTOYClXhJD0/default.jpg'
|
| 36 |
+
|
| 37 |
+
Neat, right? Next let's see the available media formats::
|
| 38 |
+
|
| 39 |
+
>>> yt.streams.all()
|
| 40 |
+
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
|
| 41 |
+
<Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
|
| 42 |
+
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
|
| 43 |
+
<Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
|
| 44 |
+
<Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
|
| 45 |
+
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
|
| 46 |
+
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
|
| 47 |
+
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">,
|
| 48 |
+
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
|
| 49 |
+
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
|
| 50 |
+
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
|
| 51 |
+
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
|
| 52 |
+
<Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">]
|
| 53 |
+
|
| 54 |
+
Let's say we want to get the first stream::
|
| 55 |
+
|
| 56 |
+
>>> stream = yt.streams.first()
|
| 57 |
+
>>> stream
|
| 58 |
+
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">
|
| 59 |
+
|
| 60 |
+
And to download it to the current working directory::
|
| 61 |
+
|
| 62 |
+
>>> stream.download()
|
| 63 |
+
|
| 64 |
+
You can also specify a destination path::
|
| 65 |
+
|
| 66 |
+
>>> stream.download('/tmp')
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
Working with streams
|
| 70 |
+
--------------------
|
| 71 |
+
|
| 72 |
+
Now let's explore the various options available for filtering streams. Begin by
|
| 73 |
+
running the following again (*note these results can change over time*)::
|
| 74 |
+
|
| 75 |
+
>>> yt.streams.all()
|
| 76 |
+
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
|
| 77 |
+
<Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
|
| 78 |
+
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
|
| 79 |
+
<Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
|
| 80 |
+
<Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
|
| 81 |
+
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
|
| 82 |
+
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
|
| 83 |
+
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">,
|
| 84 |
+
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
|
| 85 |
+
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
|
| 86 |
+
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
|
| 87 |
+
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
|
| 88 |
+
<Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
You may notice that some streams listed have both a video codec and audio
|
| 92 |
+
codec, while others have just video or just audio, this is a result of YouTube
|
| 93 |
+
supporting a streaming technique called Dynamic Adaptive Streaming over HTTP
|
| 94 |
+
(DASH).
|
| 95 |
+
|
| 96 |
+
In the context of pytube, the implications are for the highest quality streams;
|
| 97 |
+
you now need to download both the audio and video tracks and then post-process
|
| 98 |
+
them with software like FFmpeg to merge them.
|
| 99 |
+
|
| 100 |
+
The legacy streams that contain the audio and video in a single file (referred
|
| 101 |
+
to as "progressive download") are still available, but only for resolutions
|
| 102 |
+
720p and below.
|
| 103 |
+
|
| 104 |
+
To only view these progressive download streams::
|
| 105 |
+
|
| 106 |
+
>>> yt.streams.filter(progressive=True).all()
|
| 107 |
+
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
|
| 108 |
+
<Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
|
| 109 |
+
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
|
| 110 |
+
<Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
|
| 111 |
+
<Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">]
|
| 112 |
+
|
| 113 |
+
Conversely, if you only want to see the DASH streams (also referred to as
|
| 114 |
+
"adaptive") you can do::
|
| 115 |
+
|
| 116 |
+
>>> yt.streams.filter(adaptive=True).all()
|
| 117 |
+
[<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
|
| 118 |
+
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
|
| 119 |
+
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">,
|
| 120 |
+
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
|
| 121 |
+
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
|
| 122 |
+
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
|
| 123 |
+
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
|
| 124 |
+
<Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">]
|
| 125 |
+
|
| 126 |
+
Pytube allows you to filter on every property available (see
|
| 127 |
+
:py:meth:`pytube.StreamQuery.filter` for a complete list of filter options),
|
| 128 |
+
let's take a look at some common examples:
|
| 129 |
+
|
| 130 |
+
Query audio only streams
|
| 131 |
+
------------------------
|
| 132 |
+
|
| 133 |
+
To query the streams that contain only the audio track::
|
| 134 |
+
|
| 135 |
+
>>> yt.streams.filter(only_audio=True).all()
|
| 136 |
+
[<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
|
| 137 |
+
<Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">]
|
| 138 |
+
|
| 139 |
+
Query mp4 streams
|
| 140 |
+
-----------------
|
| 141 |
+
|
| 142 |
+
To query the streams that are encoded as mp4::
|
| 143 |
+
|
| 144 |
+
>>> yt.streams.filter(file_extension='mp4').all()
|
| 145 |
+
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
|
| 146 |
+
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
|
| 147 |
+
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
|
| 148 |
+
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
|
| 149 |
+
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f">,
|
| 150 |
+
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
|
| 151 |
+
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
|
| 152 |
+
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
|
| 153 |
+
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">]
|