Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Last active March 19, 2023 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescalam/51c7d1cd1ad4e8c4b581f05c59881e39 to your computer and use it in GitHub Desktop.
Save jamescalam/51c7d1cd1ad4e8c4b581f05c59881e39 to your computer and use it in GitHub Desktop.
from pytube import YouTube # !pip install pytube
from pytube.exceptions import RegexMatchError
from tqdm.auto import tqdm # !pip install tqdm
# where to save
save_path = "./mp3"
for i, row in tqdm(videos_meta):
# url of video to be downloaded
url = f"https://youtu.be/{row['Video ID']}"
# try to create a YouTube vid object
try:
yt = YouTube(url)
except RegexMatchError:
print(f"RegexMatchError for '{url}'")
continue
itag = None
# we only want audio files
files = yt.streams.filter(only_audio=True)
for file in files:
# from audio files we grab the first audio for mp4 (eg mp3)
if file.mime_type == 'audio/mp4':
itag = file.itag
break
if itag is None:
# just incase no MP3 audio is found (shouldn't happen)
print("NO MP3 AUDIO FOUND")
continue
# get the correct mp3 'stream'
stream = yt.streams.get_by_itag(itag)
# downloading the audio
stream.download(
output_path=save_path,
filename=f"{row['Video ID']}.mp3"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment