Python Mini Projects for Everyone-3

Kaan ÇUKUR
2 min readNov 21, 2022

--

Do your own video and Mp3 downloader.

Python mini projects continue with download video or Mp3 from Youtube.

I am sure that everybody search on Google “Youtube Mp3 Download” at least once lifetime :)

Why don’t we create our downloader on Python, let’s have a look.

First we will install or API(Pytube).

!pip install pytube

I am using Google Colab, so i copied path from “Sample Data” file and copied url from Youtube. We will see which song i download at the end.

# https://www.youtube.com/watch?v=iy0CN6O6q-M => Youtube link which we download
# path=/content/sample_data => Path copied from Colab

We will write a basic function, you can find explanations on the command line.

def Downloader():
import pytube # Import library
url=input("Please paste your youtube link : ") # Take url from user
MediaType=input("Mp3 or Mp4 ? : ") # I add two option Mp3 and mP4
path="/content/sample_data" # My video or Mp3 file will dowload ths path.
if MediaType=='mp4': # Case 1 if my mediatype is Mp4
print("Mp4 file is downloading...")
pytube.YouTube(url).streams.get_highest_resolution().download(output_path=path,filename="video.mp4") # then download Mp4 file.
else:
print("Mp3 file is downloading...") #İf is not Mp4 so if is it mp3
pytube.YouTube(url).streams.get_audio_only().download(output_path=path,filename="video.mp3") # then download Mp3 file.
return('Download finished.')

Not hard, isn’t it :) Let’s try.

You can see Mp4 file in “Sample Data” file.

Enjoy, Leave comment your first music which one you tried. You can find another mini projects on my Medium and LinkedIn Profile.

Also you can find full notebook in my github profile.

--

--