Created
August 31, 2023 19:34
-
-
Save mitkof6/26267f94b58797951bcadeabbcad6ae8 to your computer and use it in GitHub Desktop.
Youtube to mp3 downloader with playlist option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import pytube | |
import argparse | |
parser = argparse.ArgumentParser(description="Youtube to MP3") | |
parser.add_argument("url") | |
parser.add_argument( | |
"--playlist", | |
default=False, | |
action=argparse.BooleanOptionalAction | |
) | |
def download_video(video): | |
print(f"Downloading: {video.title}") | |
audio = video.streams.filter(only_audio=True).first() | |
filename = re.sub(r"[^\w\d\.]+", "_", f"{video.title}.mp3") | |
audio.download("tmp", filename) | |
print("Download completed") | |
if __name__ == "__main__": | |
args = parser.parse_args() | |
try: | |
if args.playlist: | |
playlist = pytube.Playlist(str(args.url)) | |
for video in playlist.videos: | |
download_video(video) | |
else: | |
download_video(pytube.YouTube(str(args.url))) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment