Created
December 17, 2020 11:35
-
-
Save tripulse/d261a578dd61cd3579168fdd7860d1f9 to your computer and use it in GitHub Desktop.
YouTube video thumbnail downloader script.
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
#!/usr/bin/env python | |
# https://gist.github.com/tripulse/e319d24bbc7b8b48f73336f7bc230e0c | |
""" | |
yt-thumb: a script to download thumbnails of YouTube videos of quantized set | |
of qualities (lowest, low, mid, high, highest). This tool nor the method it | |
uses imposes any restrictions on downloading thumbnails. | |
Qualities and their co-respoding width, height dimensions: | |
* lowest 120x90 | |
* low 320x180 | |
* mid 480x360 | |
* high 640x480 | |
* highest 1280x720 | |
""" | |
import re | |
from urllib.request import urlopen | |
from urllib.error import URLError | |
from argparse import ArgumentParser, FileType, RawTextHelpFormatter | |
from functools import partial | |
from sys import stdout | |
from shutil import copyfileobj | |
def extract_id_from_url(url: str): | |
# Stripped version of the RegEx: | |
# https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L372-L427 | |
return re.compile(r"""(?x)^( | |
(?:https?://|//) | |
(?:(?:(?:(?:\w+\.)?[yY][oO][uU][tT][uU][bB][eE](?:-nocookie|kids)?\.com/| | |
(?:www\.)?deturl\.com/www\.youtube\.com/| | |
(?:www\.)?pwnyoutube\.com/| | |
(?:www\.)?hooktube\.com/| | |
(?:www\.)?yourepeat\.com/| | |
youtube\.googleapis\.com/) | |
(?:.*?\#/)?(?:(?:(?:v|embed|e)/(?!videoseries))|(?:(?:(?:watch|movie)(?:_popup)?(?:\.php)?/?)?(?:\?|\#!?)(?:.*?[&;])??v=))) | |
|(?:youtu\.be|vid\.plus|zwearz\.com/watch|)/|(?:www\.)?cleanvideosearch\.com/media/action/yt/watch\?videoId=))? | |
(?P<id>[0-9A-Za-z_-]{11})(?!.*?\blist=(?:([0-9A-Za-z_-]{34})|WL))(?(1).+)?$""").match(url).groupdict().get('id') | |
class YouTubeThumbnailGrabber(ArgumentParser): | |
Q2T = { 'highest': 'maxresdefault', | |
'high' : 'sddefault', | |
'med' : 'hqdefault', | |
'low' : 'mqdefault', | |
'lowest' : 'default', } | |
def __init__(self): | |
ArgumentParser.__init__(self, | |
description= __doc__, | |
formatter_class= RawTextHelpFormatter) | |
self.add_argument('URL', | |
help= "YouTube-like video URL to grab (eg. 'youtu.be').") | |
self.add_argument('OUT', | |
nargs= '?', | |
default= '%(id)s.%(ext)s', | |
help= "output path of the downloaded thumbnail.\n" | |
"Tips: place '%%(id)s' to replace with video ID,\n" | |
" place '%%(ext)s' to replace format extension.") | |
self.add_argument('-q', | |
default= '/'.join(self.Q2T.keys()), | |
dest= 'qualities', | |
choices= self.Q2T.keys(), | |
help= "quality scale of the thumbnail.") | |
self.args = self.parse_args() | |
def main(self): | |
video_id = extract_id_from_url(self.args.URL) | |
for imgq in self.args.qualities.split('/'): | |
for imgfmt, ext in [('vi_webp','webp'),('vi','jpg')]: | |
try: | |
img = urlopen(f'https://i1.ytimg.com/{imgfmt}/{video_id}/{self.Q2T[imgq]}.{ext}') | |
copyfileobj(img, open(self.args.OUT % {'id': video_id, 'ext': ext}, 'wb')) | |
exit() | |
except URLError: | |
print(f'ERROR: {imgq} quality as {ext} not found!') | |
continue | |
if __name__ == "__main__": | |
YouTubeThumbnailGrabber().main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment