I often need to download videos from Youtube in mp3
format. There are many websites where you can convert Youtube videos to mp3
, but I am giving you a simple Python script that does the same job.
I am using youtube_dl. Command-line program to download videos from YouTube.com and other video sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform-specific. It should work on your Unix box, on Windows, or on macOS.
Let's jump to the code:
import youtube_dl
def run():
video_url = input("please enter youtube video url:")
video_info = youtube_dl.YoutubeDL().extract_info(
url = video_url,download=False
)
filename = f"{video_info['title']}.mp3"
options={
'format':'bestaudio/best',
'keepvideo':False,
'outtmpl':filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info['webpage_url']])
print("Download complete... {}".format(filename))
if __name__=='__main__':
run()
Just enter the URL of the song and the script will download that song in mp3
format, cool isn't it?
Thank you all.