Skip to content

Instantly share code, notes, and snippets.

@SunjunKim
Last active September 30, 2023 06:43
Show Gist options
  • Save SunjunKim/3c368e9ba464de9cb67abcbd95318291 to your computer and use it in GitHub Desktop.
Save SunjunKim/3c368e9ba464de9cb67abcbd95318291 to your computer and use it in GitHub Desktop.
Download music (=audio) from any YouTube link
# This script will download the audio of the YouTube video and save it in the same directory as your Python script.
# The audio file will be in .mp4 format, which is the default audio format for YouTube videos.
# Please remember to replace 'your_url' with the actual URL of the YouTube video you want to download.
# Also, please be aware of and respect the terms of service of YouTube and any applicable copyright laws when downloading videos or audio.
from pytube import YouTube
import re
import os
def download_youtube_audio(link):
yt = YouTube(link)
print(yt.title)
title = re.sub('[\\\\/:*?"<>|]', '_', yt.title).strip()
print(title)
yt.streams.get_audio_only().download(filename=os.path.join(f"{title}.mp4"))
link = "your_url"
download_youtube_audio(link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment