Download YouTube Video and Audio Using Python

Handhika Yanuar Pratama
2 min readApr 20, 2024

--

Photo by Clay Banks on Unsplash

Python is a very powerful programming language, and YouTube, as you know, is a platform for watching videos and listening to music very easily. This tutorial wants to tell the ways to download videos from YouTube directly using a programming language.

The Traditional Method

As for the traditional method, whenever I want to download any video, I change “www” from the video URL into “ss” at the beginning of every video like this.

It will bring us to the download page from savefrom.net

Okay, that’s the traditional way. I want to make your code performance cool, like making it true.

The Code Ways

First of all, we should have the pytube library installed on our computer. You can install it using this command.

pip install pytube

After that, here is the program I was talking about.

from pytube import YouTube

urls = input("url:")

vid = YouTube(urls)

video_download = vid.streams.get_highest_resolution()
audio_download = vid.streams.get_audio_only()

entry = YouTube(urls).title

print(f"\nVideo found: {entry}\n")

print(f"Downloading Video...")
video_download.download(filename=f"{entry}.mp4")

print("Downloading Audio...")
audio_download.download(filename=f"{entry}.mp3")

print("Program Completed")

If you run the program, it will look very simple like this

And here are videos and music that have already been downloaded using the program

Conclusions

Python offers us a very usable script that could be used as a daily script. Well, today, everyone prefers streaming cause it doesn’t take up any space on our devices. But it’s a great start for learning Python because we are creating real-world programs.

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer

Responses (4)