Skip to content

Instantly share code, notes, and snippets.

@alrafiabdullah
Created February 14, 2021 18:44
Show Gist options
  • Save alrafiabdullah/9742f198688e4de5d0a9a9f915e53c0e to your computer and use it in GitHub Desktop.
Save alrafiabdullah/9742f198688e4de5d0a9a9f915e53c0e to your computer and use it in GitHub Desktop.
A script to download audio file from YouTube link.
from pytube import YouTube
import validators
url = input("Enter a YouTube URL: ")
if not validators.url(url):
raise ValueError("Please enter a correct URL.")
if not "youtube" in url:
raise ValueError("Please enter a YouTube URL.")
print("Initializing...")
audio = YouTube(url)
tags = audio.streams.filter(only_audio=True)
audio_file = audio.streams.get_by_itag(tags[0].itag)
print("Download started...")
audio_file.download()
print(f"{audio.title} has been downloaded!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment