How To Download YouTube Playlist Using Python - by Ashutosh Krishna - Python in Plain English
How To Download YouTube Playlist Using Python - by Ashutosh Krishna - Python in Plain English
Listen Share
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 1/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
In this tutorial, you will be using the Pytube library in Python to download entire
YouTube playlists in various resolutions, including the high-quality 2160p
resolution. Pytube is a lightweight library that allows easy access to YouTube videos
and metadata. With Pytube, we can easily extract video URLs, download videos, and
even extract audio from videos. So, let’s dive into the tutorial and learn how to
download a YouTube playlist using Python.
If you’re in a hurry, jump to the GitHub gist to get the code immediately.
Prerequisites
To follow this tutorial, you should have a basic understanding of Python
programming language, including installing Python packages using pip.
You will also need to install the Pytube library, which can be installed using pip.
Open a command prompt or terminal window and type the following command:
Apart from Python and Pytube, you will also need to have FFmpeg installed on your
system. FFmpeg is a command-line tool used for handling multimedia files. It is
required for Pytube to be able to download videos in various resolutions. You can
download FFmpeg from the official website and follow the installation instructions
for your operating system.
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 2/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
import os
import re
from pytube import Playlist, YouTube
module to handle regular expressions, and pytube module to download the YouTube
playlist.
if not os.path.exists(playlist_name):
os.mkdir(playlist_name)
It first creates a new Playlist object using the Pytube library and extracts the name
of the playlist. It then checks if a folder with the same name exists in the current
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 3/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
working directory. If not, it creates a new folder with the playlist name.
Note that the re.sub() function replaces any non-alphanumeric characters in the
playlist title with a hyphen ("-") character. We do so because folder and file names in
most file systems have restrictions on the characters they can contain.
objects. You first iterate through the list of videos in the playlist using a for loop
and enumerate function.:
The start parameter set to 1 to start counting from 1 instead of 0 because you're
going to use the index in the filenames. Thus it makes sense to start from 1.
Next, create a YouTube object for each video using its watch URL, and specify to use
OAuth for authentication:
Recently, there were several issues where PyTube was throwing a KeyError. The fix was to
use the use_oauth=True parameter. When you run the code for the first time, it will ask
you to login into your YouTube account.
Filter the available video streams to select the one with the desired resolution, and
get its filename:
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 4/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
Determine the full path and filename for the video file, and check if it already
exists. If it does, skip downloading this video and move on to the next one:
If the desired resolution is not available, download the video with the highest
resolution instead:
if not video_streams:
highest_resolution_stream = video.streams.get_highest_resolution()
video_name = highest_resolution_stream.default_filename
print(f"Downloading {video_name} in {highest_resolution_stream.resoluti
highest_resolution_stream.download(filename=video_path)
If the desired resolution is available, you don’t have to download it directly. If you do
so, the downloaded video won’t have sound. Instead, download both the video and
audio streams separately, and merge them using the FFmpeg library to create the
final video file. Finally, rename the merged file and delete the temporary video and
audio files:
if not video_streams:
...
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 5/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
else:
video_stream = video_streams.first()
video_name = video_stream.default_filename
print(f"Downloading video for {video_name} in {resolution}")
video_stream.download(filename="video.mp4")
audio_stream = video.streams.get_audio_only()
print(f"Downloading audio for {video_name}")
audio_stream.download(filename="audio.mp4")
The video stream is initially downloaded as video.mp4 and the audio stream is
downloaded as audio.mp4 . Next, the ffmpeg command takes two input files
( video.mp4 and audio.mp4 ), copies the video codec from the input file and uses the
AAC codec for audio, and saves the output as final.mp4 . The output file is created by
merging the video and audio streams from the two input files.
After ffmpeg finishes processing, the final.mp4 is renamed video_path and the
video and audio stream files are deleted.
if not os.path.exists(playlist_name):
os.mkdir(playlist_name)
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 6/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
video_streams = video.streams.filter(res=resolution)
if not video_streams:
highest_resolution_stream = video.streams.get_highest_resolution()
video_name = highest_resolution_stream.default_filename
print(f"Downloading {video_name} in {highest_resolution_stream.reso
highest_resolution_stream.download(filename=video_path)
else:
video_stream = video_streams.first()
video_name = video_stream.default_filename
print(f"Downloading video for {video_name} in {resolution}")
video_stream.download(filename="video.mp4")
audio_stream = video.streams.get_audio_only()
print(f"Downloading audio for {video_name}")
audio_stream.download(filename="audio.mp4")
print("----------------------------------")
A line of dashes is also printed after every iteration to separate each video as they
are downloaded.
Main Function
Let’s create the main function that takes input from the user for the playlist URL and
desired video resolution. It then calls the download_playlist function with the user's
inputs.
if __name__ == "__main__":
playlist_url = input("Enter the playlist url: ")
resolutions = ["240p", "360p", "480p", "720p", "1080p", "1440p", "2160p"]
resolution = input(f"Please select a resolution {resolutions}: ")
download_playlist(playlist_url, resolution)
Completed Code
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 7/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
import os
import re
from pytube import Playlist, YouTube
if not os.path.exists(playlist_name):
os.mkdir(playlist_name)
video_streams = video.streams.filter(res=resolution)
if not video_streams:
highest_resolution_stream = video.streams.get_highest_resolution()
video_name = highest_resolution_stream.default_filename
print(
f"Downloading {video_name} in {highest_resolution_stream.resolution}")
highest_resolution_stream.download(filename=video_path)
else:
video_stream = video_streams.first()
video_name = video_stream.default_filename
print(f"Downloading video for {video_name} in {resolution}")
video_stream.download(filename="video.mp4")
audio_stream = video.streams.get_audio_only()
print(f"Downloading audio for {video_name}")
audio_stream.download(filename="audio.mp4")
os.system(
"ffmpeg -y -i video.mp4 -i audio.mp4 -c:v copy -c:a aac final.mp4 -loglevel quiet -
os.rename("final.mp4", video_path)
os.remove("video.mp4")
os.remove("audio.mp4")
print("----------------------------------")
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 8/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
if __name__ == "__main__":
playlist_url = input("Enter the playlist url: ")
resolutions = ["240p", "360p", "480p", "720p", "1080p", "1440p", "2160p"]
resolution = input(f"Please select a resolution {resolutions}: ")
download_playlist(playlist_url, resolution)
Using this script, you can download all the videos from a YouTube Playlist in all
supported resolutions (including 1080p and 2160p).
Enter the playlist url and the resolution you wish to download.
Make sure you have ffmpeg setup on your machine. Check the tutorial for the setup
guide.
You can download the videos in the following resolutions: 144p, 240p, 360p, 480p, 720p,
1080p, 1440p, 2160p
Check the video for available resolutions.
Conclusion
In this tutorial, you learned how you can use PyTube and FFmpeg libraries to
download videos from a YouTube playlist in high resolutions such as 1080p, 1440p,
and even 2160p.
I hope you found the tutorial helpful. If so, don’t forget to star the GitHub gist and
share this tutorial with others.
The code has been tested with Pytube 12.1.3. It may not work properly at a later point in
time.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and
Discord.
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 9/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
Follow
141 1
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 10/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
1K 12
1.6K 46
Ashutosh Krishna
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 12/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
Bosko Savic
56
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 13/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
Gopeshkhandelwal
Lists
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 14/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
Ritik khandelwal
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 15/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
141
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 16/17
1/16/24, 10:18 PM How to Download YouTube Playlist Using Python | by Ashutosh Krishna | Python in Plain English
SarahDev
242 1
https://python.plainenglish.io/how-to-download-youtube-playlist-using-python-4e832989e017 17/17