Languages
[Edit]
EN

JavaScript - get YouTube image url by video id

0 points
Created by:
Paris-Bateman
504

In this article, we would like to show you how to get YouTube image url by video id using JavaScript.

Quick solution:

var videoId = 'SOGpvHun6vg';

var url1 = `https://i.ytimg.com/vi/${videoId}/default.jpg`;        // size: 120x90
var url2 = `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg`;      // size: 320x180
var url3 = `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`;      // size: 480x360
var url4 = `https://i.ytimg.com/vi/${videoId}/sddefault.jpg`;      // size: 640x480
var url5 = `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`;  // size: depends on YouTube and video resolution

Warning:

To avoid common mistake, remember not to add www to the src, e.g. use http://i.ytimg.com instead of: http://www.i.ytimg.com.

 Used resources:

  • Video id: SOGpvHun6vg
  • Full link: https://www.youtube.com/watch?v=SOGpvHun6vg

 

Practical example

Video id

const videoId = 'SOGpvHun6vg';

URL Template

https://i.ytimg.com/vi/${videoId}/${imageQuality}${imageNumber}.jpg

Configurations

imageQualityimageNumberDescription
'''default', '1', '2' or '3'low quality
'mq''default', '1', '2' or '3'medium quality
'hq''default', '1', '2' or '3'high quality
'sd''default', '1', '2' or '3'standard quality
'maxres''default', '1', '2' or '3'maximum resolution

Fully builded URLs

image typereturned URLs
Main frame image
https://i.ytimg.com/vi/SOGpvHun6vg/default.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/mqdefault.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/hqdefault.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/sddefault.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/maxresdefault.jpg

Hint: hqdefault configuration we can replace with 0.

Random frame images (1-3)
https://i.ytimg.com/vi/SOGpvHun6vg/1.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/mq1.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/hq1.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/sd1.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/maxres1.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/2.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/mq2.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/hq2.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/sd2.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/maxres2.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/3.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/mq3.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/hq3.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/sd3.jpg
https://i.ytimg.com/vi/SOGpvHun6vg/maxres3.jpg

Runnable example

In this example, we create a function that accepts two arguments YouTube video id and image quality. It returns the src of the YouTube video thumbnail image.

// ONLINE-RUNNER:browser;

const createImageUrl = (videoId, imageQuality) => {
    return 'https://i.ytimg.com/vi/' + videoId + '/' + imageQuality + 'default.jpg';
};


// Usage example:

const videoId = 'SOGpvHun6vg';

console.log(createImageUrl(videoId, ''));       // size: 120x90
console.log(createImageUrl(videoId, 'mq'));     // size: 320x180
console.log(createImageUrl(videoId, 'hq'));     // size: 480x360
console.log(createImageUrl(videoId, 'sd'));     // size: 640x480
console.log(createImageUrl(videoId, 'maxres')); // size: depends on YouTube and video resolution

Reusable function

In this example, we present a reusable functions that get the images using given YouTube video id and appends them inside the placeholderElement.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <head>
    <style>

    img {
        display: block;
    }

    </style>
  </head>
  <script>
    
    function createImageUrl(videoId, imageQuality) {
        return 'https://i.ytimg.com/vi/' + videoId + '/' + imageQuality + 'default.jpg';
    }
    
    function appendImageElement(parentElement, videoId, imageQuality, placeholderElement) {
        var imageElement = document.createElement('img');
        imageElement.src = createImageUrl(videoId, imageQuality);
        parentElement.insertBefore(imageElement, placeholderElement);
    }


    // Usage example:
    
    var videoId = 'SOGpvHun6vg';

    appendImageElement(document.body, videoId, '');       // size: 120x90
    appendImageElement(document.body, videoId, 'mq');     // size: 320x180
    appendImageElement(document.body, videoId, 'hq');     // size: 480x360
    appendImageElement(document.body, videoId, 'sd');     // size: 640x480
    appendImageElement(document.body, videoId, 'maxres'); // size: depends on YouTube and video resolution

  </script>
</body>
</html>

 

See also

  1. JavaScript - create dynamically html element

Alternative titles

  1. JavaScript - get YouTube thumbnail image url by video id
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join