Home » Download YouTube Videos in Python With PyTube

Download YouTube Videos in Python With PyTube

Java SE 11 Developer (Upgrade) [1Z0-817]
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Spring Framework Basics Video Course

In this article we will show you how easy it is to download video and audio from youtube using the pytube library

Legal waffle

It is generally illegal to download YouTube videos unless you have permission from the owner of the video. YouTube’s ever-changing and bewildering terms of service explicitly prohibit users from downloading videos without the prior consent of the video creator.

If you do you can face legal consequences – now we have gotten through that disclaimer.

Note that in this tutorial I am going to download a video from one of my own YouTube channels for the demo purpose – probably the only traffic it will get thanks to the algorithm

Install PyTube Library

You need to install the PyTube library. This library is on GitHub and can be installed quickly using pip, like this.

pip install pytube

Now let's get on with the interesting part – some code examples

Download video thumbnail

# youtube thumbnail grabber
from pytube import YouTube
import requests
from io import BytesIO
from PIL import Image
 
# paste the YouTube video URL here
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"
 
# create a YouTube object and get the video thumbnail and title
youtube = YouTube(url)
# Get the URL of the video's thumbnail image
thumbnail_url = youtube.thumbnail_url
 
# display the thumbnail image and title
response = requests.get(thumbnail_url)
img = Image.open(BytesIO(response.content))
img.show()

 

Get Youtube video title

PyTube provides an easy way to access the video title using the title attribute of the YouTube object.

# youtube video title program in python
from pytube import YouTube
import requests
from io import BytesIO
from PIL import Image
 
# paste the YouTube video URL here
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"
 
# create a YouTube object and get the video title
youtube = YouTube(url)
title = youtube.title
 
# Print the title of the video
print(title)

Run this on this particular video and you will see the following

5 minutes of lofi music mix 2 - chill

Get list of stream types

A stream is a single audio or video track of a YouTube video that can be downloaded. A video may have multiple available streams, each with different formats, resolutions, and file sizes

# Explore Stream Types to download youtube videos using python
from pytube import YouTube
 
# paste the YouTube video URL here
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"
 
# get the list of available streams
youtube = YouTube(url)
streams = youtube.streams.all()
 
# Print all stream types
print(streams)

When I ran this I saw the following in the repl

[<Stream: itag=”17″ mime_type=”video/3gpp” res=”144p” fps=”12fps” vcodec=”mp4v.20.3″ acodec=”mp4a.40.2″ progressive=”True” type=”video”>, <Stream: itag=”18″ mime_type=”video/mp4″ res=”360p” fps=”24fps” vcodec=”avc1.42001E” acodec=”mp4a.40.2″ progressive=”True” type=”video”>, <Stream: itag=”22″ mime_type=”video/mp4″ res=”720p” fps=”24fps” vcodec=”avc1.64001F” acodec=”mp4a.40.2″ progressive=”True” type=”video”>, <Stream: itag=”136″ mime_type=”video/mp4″ res=”720p” fps=”24fps” vcodec=”avc1.64001f” progressive=”False” type=”video”>, <Stream: itag=”134″ mime_type=”video/mp4″ res=”360p” fps=”24fps” vcodec=”avc1.4d401e” progressive=”False” type=”video”>, <Stream: itag=”160″ mime_type=”video/mp4″ res=”144p” fps=”24fps” vcodec=”avc1.4d400c” progressive=”False” type=”video”>, <Stream: itag=”139″ mime_type=”audio/mp4″ abr=”48kbps” acodec=”mp4a.40.5″ progressive=”False” type=”audio”>, <Stream: itag=”140″ mime_type=”audio/mp4″ abr=”128kbps” acodec=”mp4a.40.2″ progressive=”False” type=”audio”>, <Stream: itag=”251″ mime_type=”audio/webm” abr=”160kbps” acodec=”opus” progressive=”False” type=”audio”>]

Check list of audio tracks

from pytube import YouTube
 
# paste the YouTube video URL here
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"
 
# create a YouTube object and get the video stream
youtube = YouTube(url)
 
resolutions = ['144p', '240p', '360p', '480p', '720p', '1080p']
 
for resolution in resolutions:
    streams = youtube.streams.filter(progressive=True, res=resolution)
     
    if len(streams) > 0:
        print(resolution)
    else:
        print(resolution + ' Audio is not available')

On this video we saw the following results in the repl

144p
240p Audio is not available
360p
480p Audio is not available
720p
1080p Audio is not available

Download Youtube Video

Now we are going to look at 2 examples, the first is the video and the second will get the audio of our tremendous lofi music

In this example we download the video to an O drive – change that for your own needs

# Download youtube video
from pytube import YouTube
 
# paste the YouTube video URL here
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"
 
# create a YouTube object and get the video stream
youtube = YouTube(url)
video_stream = youtube.streams.filter(res="720p")
print(video_stream)
 
# set the download path and download the video
download_path = "O:\\"
video_stream.first().download(download_path)

In the repl you will see something like this

[<Stream: itag=”22″ mime_type=”video/mp4″ res=”720p” fps=”24fps” vcodec=”avc1.64001F” acodec=”mp4a.40.2″ progressive=”True” type=”video”>, <Stream: itag=”136″ mime_type=”video/mp4″ res=”720p” fps=”24fps” vcodec=”avc1.64001f” progressive=”False” type=”video”>]

Now you say, I just want the audio. let's do that

# Download youtube audio in python
from pytube import YouTube
 
# Set the URL of the video you want to download
url = "https://www.youtube.com/watch?v=aJrPquHlyGE"

# Create a YouTube object
yt = YouTube(url)
 
# Filter for streams that only contain audio
audio_stream = yt.streams.filter(only_audio=True)
print(audio_stream)
 
# set the download path
download_path = "O:\\"

# Download the first stream in the list
audio_stream[0].download(download_path)

The downloaded video will be in the default format for audio streams, which is .mp4 and not the ever popular .mp3 format

 

Links

https://pytube.io/en/latest/index.html

Cracking lofi youtube channel

https://github.com/programmershelp/maxpython/tree/main/modules/pytube

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More