Skip to content

Instantly share code, notes, and snippets.

@bijoy26
Last active May 18, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijoy26/b208e3620b48437b4fedb1d6e49d11fb to your computer and use it in GitHub Desktop.
Save bijoy26/b208e3620b48437b4fedb1d6e49d11fb to your computer and use it in GitHub Desktop.
Extract YouTube thumbnail images of standard and max resolutions
#!/bin/bash
#####################################
# File: yt_grabber.sh
# Description: Extract YouTube thumbnail images of multiple resolutions
# Created: Monday, 17th May 2021
# Author: Anjum Rashid
# -----
# Last Modified: Tuesday, 18th May 2021
# -----
#####################################
protocol="https://"
api_endpoint="i.ytimg.com/vi"
default_url="https://youtu.be/FwRIMBMJj2s"
function YTthumbnailSearch(){
echo "Press '1' for a demo, '2' to enter your own URL : "
read choice
if [ "$choice" = '1' ]
then
echo "Going with default URL"
declare ARR=($(echo $default_url | tr "/" "\n"))
echo "Captured Video ID : ${ARR[2]}"
video_id=${ARR[2]}
# max resolution
thumbnail_maxres_url="https://${api_endpoint}/${video_id}/maxresdefault.jpg"
echo "Connecting to ${thumbnail_maxres_url}"
# standard resolution
thumbnail_standard_url="https://${api_endpoint}/${video_id}/sddefault.jpg"
echo "Connecting to ${thumbnail_standard_url}"
# make lowercase
lower_video_id=$(echo "$video_id" | tr '[:upper:]' '[:lower:]')
directory_path="downloads/$lower_video_id"
# check if directory exists
if [ -d $directory_path ]
then
echo "Directory "$lower_video_id" already exists."
else
echo "Downloading to "${lower_video_id}" directory"
mkdir $directory_path
fi
maxres_status_code=$(curl -s -I "${thumbnail_maxres_url}" | head -1 | awk {'print $2'})
sdres_status_code=$(curl -s -I "${thumbnail_standard_url}" | head -1 | awk {'print $2'})
# if encounters with 40X serie error
if [ $maxres_status_code -ne '404' ]
then
echo "Downloading max resolution"
curl -s $thumbnail_maxres_url > $directory_path/maxres.jpg
else
echo "Thumbnail max resolution not available!"
fi
if [ $sdres_status_code -ne '404' ]
then
echo "Downloading standard resolution"
curl -s $thumbnail_standard_url > $directory_path/sd.jpg
else
echo "Thumbnail standard resolution not available!"
fi
echo "Download complete! Check "${directory_path}" folder for the images!"
elif [ "$choice" = '2' ]
then
echo "Collect YouTube share link and paste here:"
read link
declare ARR=($(echo $link | tr "/" "\n"))
echo "Captured Video ID : ${ARR[2]}"
video_id=${ARR[2]}
thumbnail_maxres_url="https://${api_endpoint}/${video_id}/maxresdefault.jpg"
echo "Connecting to ${thumbnail_maxres_url}"
thumbnail_standard_url="https://${api_endpoint}/${video_id}/sddefault.jpg"
echo "Connecting to ${thumbnail_standard_url}"
lower_video_id=$(echo "$video_id" | tr '[:upper:]' '[:lower:]')
directory_path="downloads/$lower_video_id"
if [ -d $directory_path ]
then
echo "Directory "$lower_video_id" already exists."
else
echo "Downloading to "${lower_video_id}" directory"
mkdir $directory_path
fi
maxres_status_code=$(curl -s -I "${thumbnail_maxres_url}" | head -1 | awk {'print $2'})
sdres_status_code=$(curl -s -I "${thumbnail_standard_url}" | head -1 | awk {'print $2'})
# if encounters with 40X serie error
if [ $maxres_status_code -ne '404' ]
then
echo "Downloading max resolution"
curl -s $thumbnail_maxres_url > $directory_path/maxres.jpg
else
echo "Thumbnail max resolution not available!"
fi
if [ $sdres_status_code -ne '404' ]
then
echo "Downloading standard resolution"
curl -s $thumbnail_standard_url > $directory_path/sd.jpg
else
echo "Thumbnail standard resolution not available!"
fi
echo "Download complete! Check "${directory_path}" folder for the images!"
else
echo "Wrong choice"
fi
}
YTthumbnailSearch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment