Created
May 14, 2023 10:29
-
-
Save AIAnytime/1fb5696a8bfabc27928c4978d4e99272 to your computer and use it in GitHub Desktop.
PyTube Audio Snippet
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
# 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