Skip to content

Instantly share code, notes, and snippets.

@AIAnytime
Created May 14, 2023 10:29
Show Gist options
  • Save AIAnytime/1fb5696a8bfabc27928c4978d4e99272 to your computer and use it in GitHub Desktop.
Save AIAnytime/1fb5696a8bfabc27928c4978d4e99272 to your computer and use it in GitHub Desktop.
PyTube Audio Snippet
# PyTube function for YouTube video
def save_audio(url):
yt = YouTube(url)
video = yt.streams.filter(only_audio=True).first()
out_file = video.download()
base, ext = os.path.splitext(out_file)
file_name = base + '.mp3'
try:
os.rename(out_file, file_name)
except WindowsError:
os.remove(file_name)
os.rename(out_file, file_name)
audio_filename = Path(file_name).stem+'.mp3'
return audio_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment