Files
useful/programs/download youtube.py
2024-07-13 00:53:45 +03:00

38 lines
940 B
Python

import pytube
def get_video(url):
print('----------------')
print(f'Getting video {url}')
youtube = pytube.YouTube(url)
return youtube
def download_video(youtube, require_audio=False, resolution=None, ext='webm'):
print(f'Found video {youtube.title}. Searching for streams...')
streams = youtube.streams.filter(file_extension=ext)
# Filter resolution
if resolution:
streams = streams.filter(res=resolution)
# Filter audio
if require_audio:
streams = [x for x in streams if x.includes_audio_track]
if streams:
stream = max(streams, key=lambda x: x.filesize)
print(f'Found stream {stream}. Attempting download...')
stream.download()
print('Download complete')
else:
print('Failed to find stream')
urls = [
'https://www.youtube.com/watch?v=aChm3M8hZvo',
'https://www.youtube.com/watch?v=x74OJ8A855Q',
'https://www.youtube.com/watch?v=oNj8-yO0bWs'
]
for url in urls:
yt = get_video(url)
download_video(yt)