Welcome folks today in this blog post we will be building a youtube video thumbnail downloader web app using bootstrap4 and javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an index.html
file and copy paste the below code
index.html
1 2 3 4 5 6 |
<nav class="navbar container-fluid site-gradient text-center" id="nav"> <a style="outline:none;" id="header-links" <h1 id="header">YOUTUBE THUMBNAIL DOWNLOADER</h1> </a> </nav> <br> |
Here we have the navigation bar of the application. Inside it we have the heading which says youtube thumbnail downloader and for this we are using the bootstrap classes.
Making the Input Field
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<div class="container text-center" id="main"> <div id="result" class="hidden"> <!-- THUMBNAIL PREVIEW --> <img class="shadow" id="thumbnailpreview" src="images/placeholder.gif" onerror="this.style.display='none'" /> <!-- THUMBNAIL PREVIEW --> <hr style="width:50vw;"> <!-- INPUT AREA --> <div class="input-group"> <input id="ytlink" autofocus type="text" class="form-control shadow" placeholder="ENTER A YOUTUBE VIDEO URL HERE" spellcheck="false" onsubmit="youtube_thumbnail_downloader(ytlink.value);ShowMain()" autocomplete="off" /> </div> <button id="thumbdloadbtn" class="thumbdloadbtn site-gradient" type="button" onclick="youtube_thumbnail_downloader(ytlink.value);ShowMain()">FETCH</button> <!-- INPUT AREA --> </div> </div> |
As you can see we are building the input fields and using the bootstrap classes. And also we have the youtube video thumbnail preview. For this image preview we are giving the bootstrap classes.
Here as you can see we have the input field and we also have the button to fetch the youtube thumbnail in different formats. And now you need to download the image as an attachment.
1 2 3 4 5 6 7 8 9 10 11 12 |
<div id="download-buttons"> <a target="_blank" id="thumbnailhq" download><button class="thumbdloadbtn site-gradient shadow">HIGH QUALITY</button></a> <br> <a target="_blank" id="thumbnailmq" download><button class="thumbdloadbtn site-gradient shadow">MEDIUM QUALITY</button></a> <br> <a target="_blank" id="thumbnaillq" download><button class="thumbdloadbtn site-gradient shadow">LOW QUALITY</button></a> <br> <a onclick="location.reload();"><button class="thumbdloadbtn site-gradient shadow">RELOAD</button></a> </div> |
As you can see we have different buttons to download the video thumbnail in different formats. And also we have the reload button to reload the application.
Writing the CSS Styles
Now we will write the css styles for the application. Now just create the style.css file and copy paste the below code
style.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
body { font-family: 'Dosis', sans-serif; background: #ffffff; height: 100%; width: 100%; color: #ffffff; background: -webkit-linear-gradient(bottom, #bababa, #ffffff); /* For Safari */ background: -o-linear-gradient(top, #bababa, #ffffff); /* For Opera */ background: -moz-linear-gradient(top, #bababa, #ffffff); /* For Firefox*/ background: linear-gradient(to top, #bababa, #ffffff); /* Standard syntax */ } a, a:focus, a:hover { color: #ffffff; } ::-webkit-scrollbar { display: none; } ::scrollbar { display: none; } ::-moz-selection { /* Code for Firefox */ color: #ffffff; background: #00775A; } ::selection { color: white; background: #00775A; } input { caret-color: #040157; } input:active { -webkit-box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); -moz-box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); } .shadow { -webkit-box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); } .shadow:hover { -webkit-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); } .site-gradient { background: #000000; background: -webkit-linear-gradient(left bottom, #040157, #00775A); /* For Safari */ background: -o-linear-gradient(top right, #040157, #00775A); /* For Opera */ background: -moz-linear-gradient(top right, #14131f, #00775A); /* For Firefox*/ background: linear-gradient(to top right, #040157, #00775A); /* Standard syntax */ -webkit-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); -moz-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); } #nav { justify-content: center; text-align: justify; padding: 1.5rem 0; position: relative; z-index: 500; } #header-links { text-decoration: none; align-items: center; } #header { font-size: 2rem; white-space: normal; font-weight: 500; margin: 0 0.25rem; } #social-media { width: 100%; } #social-media-icons { text-decoration: none; margin: 0 -0.25rem; } #social { width: 2.5rem; height: 2.5rem; } #main { margin: auto; display: flex; min-height: 100vh; height: 100%; width: 100vw; align-items: center; justify-content: center; } #thumbnailpreview { display: flex; margin: -5rem auto auto auto; position: relative; text-align: center; justify-content: center; max-width: 75vw; border: 0.25rem solid #ffffff; -webkit-transition: all 1s ease; transition: all 1s ease; -moz-transition: all ease 1s; -o-transition: all ease 1s; } #thumbnailpreview:hover { transform: scale(1.05); -webkit-transition: all 1s ease; transition: all 1s ease; -moz-transition: all ease 1s; -o-transition: all ease 1s; } #ytlink { color: #000000; text-align: center; padding: 1.5rem 0; margin: 0.25rem; border-radius: 10rem; font-weight: 500; font-size: 1rem; width: 90vw; border: none; } #thumbdloadbtn { color: #ffffff; padding: 0.75rem; border-radius: 15rem; letter-spacing: 0.5rem; font-weight: 500; font-size: 1.25rem; } #thumbdloadbtn:active { filter: brightness(1.5); } .thumbdloadbtn { color: #ffffff; border: none; white-space: nowrap; min-width: 50vw; padding: 0.75rem; margin: 0.5rem 0; text-align: center; border-radius: 15rem; font-weight: 700; font-size: 1.25rem; } .thumbdloadbtn:active { filter: brightness(1.5); } @media only screen and (max-height: 440px) { #nav { padding: 0.25rem; } #main { margin: 3rem auto; } } @media only screen and (max-height: 380px) { #header { font-size: 3vw; } #main { margin: 5rem auto; } } @media only screen and (max-height: 280px) { #main { margin: 7rem auto; } } |
Writing the Javascript Code
Now we will write the javascript code to download the youtube video thumbnail. Here first of all we will be writing the javascript function when we click the button to download the thumbnail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function youtube_thumbnail_downloader(url) { regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/; match = url.match(regExp); if (match && match[1].length == 11) { vidurl = match[1]; thumbnailpreview = 'http://img.youtube.com/vi/'+vidurl+'/mqdefault.jpg'; thumbnailhq = 'http://img.youtube.com/vi/'+vidurl+'/maxresdefault.jpg'; thumbnailmq = 'http://img.youtube.com/vi/'+vidurl+'/hqdefault.jpg'; thumbnaillq = 'http://img.youtube.com/vi/'+vidurl+'/mqdefault.jpg'; } else { alert("The URL you have entered maybe incorrect. Please Enter a correct URL."); location.reload(); } document.getElementById('thumbnailpreview').src = thumbnailpreview; document.getElementById('thumbnailhq').href = thumbnailhq; document.getElementById('thumbnailmq').href = thumbnailmq; document.getElementById('thumbnaillq').href = thumbnaillq; } $(document).keypress(function(event) {if (event.which == 115 && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {event.preventDefault();return false;}return true;}); $("#download-buttons").hide(); $("#thumbdloadbtn").click(function(){ $("#download-buttons").slideDown(500).fadeIn(250); $("#thumbdloadbtn").hide(); $(".input-group").hide(); }); |
Here we are using the regular expression guys to first of all check if the youtube url entered is valid or not. If it is not valid then we will be showing an alert message that youtube url is not valid. And also if the url is valid then we will be constructing the youtube thumbnail images in various qualities. The quality of the thumbnail generated is in standard quality, medium quality and high quality. We are also using some fading animations with the help of jquery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$('img').error(function () { $(this).attr('src', '#.png'); }); // Or, hide them $("img").error(function () { $(this).hide(); }); $('#ytlink').keydown(function (e) { var keyCode = (e.keyCode ? e.keyCode : e.which); if (keyCode == 13) { $('#thumbdloadbtn').trigger('click'); } }); |
And also in this block of code we are extracting the video thumbnails when we press the enter key from the keyboard to submit the form. For this we have attached the keydown event listener.
Full Source Code
Now we will be showing you all the source code of youtube thumbnail downloader web app
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
<!DOCTYPE html> <html lang="en"> <head> <title>YouTube Thumbnail Downloader</title> <!-- Required meta tags --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> <style> body { font-family: 'Dosis', sans-serif; background: #ffffff; height: 100%; width: 100%; color: #ffffff; background: -webkit-linear-gradient(bottom, #bababa, #ffffff); /* For Safari */ background: -o-linear-gradient(top, #bababa, #ffffff); /* For Opera */ background: -moz-linear-gradient(top, #bababa, #ffffff); /* For Firefox*/ background: linear-gradient(to top, #bababa, #ffffff); /* Standard syntax */ } a, a:focus, a:hover { color: #ffffff; } ::-webkit-scrollbar { display: none; } ::scrollbar { display: none; } ::-moz-selection { /* Code for Firefox */ color: #ffffff; background: #00775A; } ::selection { color: white; background: #00775A; } input { caret-color: #040157; } input:active { -webkit-box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); -moz-box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); box-shadow: inset 0px 0px 50px 5px rgba(4, 1, 87, 0.5); } .shadow { -webkit-box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.5); } .shadow:hover { -webkit-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); -moz-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.5); } .site-gradient { background: #000000; background: -webkit-linear-gradient(left bottom, #040157, #00775A); /* For Safari */ background: -o-linear-gradient(top right, #040157, #00775A); /* For Opera */ background: -moz-linear-gradient(top right, #14131f, #00775A); /* For Firefox*/ background: linear-gradient(to top right, #040157, #00775A); /* Standard syntax */ -webkit-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); -moz-box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); box-shadow: 0px 0px 25px 0px rgba(0, 0, 0, 0.25); } #nav { justify-content: center; text-align: justify; padding: 1.5rem 0; position: relative; z-index: 500; } #header-links { text-decoration: none; align-items: center; } #header { font-size: 2rem; white-space: normal; font-weight: 500; margin: 0 0.25rem; } #social-media { width: 100%; } #social-media-icons { text-decoration: none; margin: 0 -0.25rem; } #social { width: 2.5rem; height: 2.5rem; } #main { margin: auto; display: flex; min-height: 100vh; height: 100%; width: 100vw; align-items: center; justify-content: center; } #thumbnailpreview { display: flex; margin: -5rem auto auto auto; position: relative; text-align: center; justify-content: center; max-width: 75vw; border: 0.25rem solid #ffffff; -webkit-transition: all 1s ease; transition: all 1s ease; -moz-transition: all ease 1s; -o-transition: all ease 1s; } #thumbnailpreview:hover { transform: scale(1.05); -webkit-transition: all 1s ease; transition: all 1s ease; -moz-transition: all ease 1s; -o-transition: all ease 1s; } #ytlink { color: #000000; text-align: center; padding: 1.5rem 0; margin: 0.25rem; border-radius: 10rem; font-weight: 500; font-size: 1rem; width: 90vw; border: none; } #thumbdloadbtn { color: #ffffff; padding: 0.75rem; border-radius: 15rem; letter-spacing: 0.5rem; font-weight: 500; font-size: 1.25rem; } #thumbdloadbtn:active { filter: brightness(1.5); } .thumbdloadbtn { color: #ffffff; border: none; white-space: nowrap; min-width: 50vw; padding: 0.75rem; margin: 0.5rem 0; text-align: center; border-radius: 15rem; font-weight: 700; font-size: 1.25rem; } .thumbdloadbtn:active { filter: brightness(1.5); } @media only screen and (max-height: 440px) { #nav { padding: 0.25rem; } #main { margin: 3rem auto; } } @media only screen and (max-height: 380px) { #header { font-size: 3vw; } #main { margin: 5rem auto; } } @media only screen and (max-height: 280px) { #main { margin: 7rem auto; } } </style> <link href="https://fonts.googleapis.com/css?family=Dosis:500" rel="stylesheet"> </head> <body> <nav class="navbar container-fluid site-gradient text-center" id="nav"> <a style="outline:none;" id="header-links" <h1 id="header">YOUTUBE THUMBNAIL DOWNLOADER</h1> </a> </nav> <br> <div class="container text-center" id="main"> <div id="result" class="hidden"> <!-- THUMBNAIL PREVIEW --> <img class="shadow" id="thumbnailpreview" src="images/placeholder.gif" onerror="this.style.display='none'" /> <!-- THUMBNAIL PREVIEW --> <hr style="width:50vw;"> <!-- INPUT AREA --> <div class="input-group"> <input id="ytlink" autofocus type="text" class="form-control shadow" placeholder="ENTER A YOUTUBE VIDEO URL HERE" spellcheck="false" onsubmit="youtube_thumbnail_downloader(ytlink.value);ShowMain()" autocomplete="off" /> </div> <button id="thumbdloadbtn" class="thumbdloadbtn site-gradient" type="button" onclick="youtube_thumbnail_downloader(ytlink.value);ShowMain()">FETCH</button> <!-- INPUT AREA --> <!-- Download Buttons --> <div id="download-buttons"> <a target="_blank" id="thumbnailhq" download><button class="thumbdloadbtn site-gradient shadow">HIGH QUALITY</button></a> <br> <a target="_blank" id="thumbnailmq" download><button class="thumbdloadbtn site-gradient shadow">MEDIUM QUALITY</button></a> <br> <a target="_blank" id="thumbnaillq" download><button class="thumbdloadbtn site-gradient shadow">LOW QUALITY</button></a> <br> <a onclick="location.reload();"><button class="thumbdloadbtn site-gradient shadow">RELOAD</button></a> </div> <!-- Download Buttons --> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> function youtube_thumbnail_downloader(url) { regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/; match = url.match(regExp); if (match && match[1].length == 11) { vidurl = match[1]; thumbnailpreview = 'http://img.youtube.com/vi/' + vidurl + '/mqdefault.jpg'; thumbnailhq = 'http://img.youtube.com/vi/' + vidurl + '/maxresdefault.jpg'; thumbnailmq = 'http://img.youtube.com/vi/' + vidurl + '/hqdefault.jpg'; thumbnaillq = 'http://img.youtube.com/vi/' + vidurl + '/mqdefault.jpg'; } else { alert("The URL you have entered maybe incorrect. Please Enter a correct URL."); location.reload(); } document.getElementById('thumbnailpreview').src = thumbnailpreview; document.getElementById('thumbnailhq').href = thumbnailhq; document.getElementById('thumbnailmq').href = thumbnailmq; document.getElementById('thumbnaillq').href = thumbnaillq; } $(document).keypress(function (event) { if (event.which == 115 && (event.ctrlKey || event.metaKey) || (event.which == 19)) { event.preventDefault(); return false; } return true; }); $("#download-buttons").hide(); $("#thumbdloadbtn").click(function () { $("#download-buttons").slideDown(500).fadeIn(250); $("#thumbdloadbtn").hide(); $(".input-group").hide(); }); </script> <script type="text/javascript"> // Replace source $('img').error(function () { $(this).attr('src', '#.png'); }); // Or, hide them $("img").error(function () { $(this).hide(); }); $('#ytlink').keydown(function (e) { var keyCode = (e.keyCode ? e.keyCode : e.which); if (keyCode == 13) { $('#thumbdloadbtn').trigger('click'); } }); </script> </body> |