EN
JavaScript - get YouTube image url by video id
0 points
In this article, we would like to show you how to get YouTube image url by video id using JavaScript.
Quick solution:
xxxxxxxxxx
1
var videoId = 'SOGpvHun6vg';
2
3
var url1 = `https://i.ytimg.com/vi/${videoId}/default.jpg`; // size: 120x90
4
var url2 = `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg`; // size: 320x180
5
var url3 = `https://i.ytimg.com/vi/${videoId}/hqdefault.jpg`; // size: 480x360
6
var url4 = `https://i.ytimg.com/vi/${videoId}/sddefault.jpg`; // size: 640x480
7
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. usehttp://i.ytimg.com
instead of:http://www.i.ytimg.com
.
Used resources:
- Video id:
SOGpvHun6vg
- Full link:
https://www.youtube.com/watch?v=SOGpvHun6vg
xxxxxxxxxx
1
const videoId = 'SOGpvHun6vg';
xxxxxxxxxx
1
https://i.ytimg.com/vi/${videoId}/${imageQuality}${imageNumber}.jpg
imageQuality | imageNumber | Description |
---|---|---|
'' | '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 |
image type | returned URLs |
---|---|
Main frame image |
xxxxxxxxxx 1 https://i.ytimg.com/vi/SOGpvHun6vg/default.jpg 2 https://i.ytimg.com/vi/SOGpvHun6vg/mqdefault.jpg 3 https://i.ytimg.com/vi/SOGpvHun6vg/hqdefault.jpg 4 https://i.ytimg.com/vi/SOGpvHun6vg/sddefault.jpg 5 https://i.ytimg.com/vi/SOGpvHun6vg/maxresdefault.jpg
|
Random frame images (1 -3 ) |
xxxxxxxxxx 1 https://i.ytimg.com/vi/SOGpvHun6vg/1.jpg 2 https://i.ytimg.com/vi/SOGpvHun6vg/mq1.jpg 3 https://i.ytimg.com/vi/SOGpvHun6vg/hq1.jpg 4 https://i.ytimg.com/vi/SOGpvHun6vg/sd1.jpg 5 https://i.ytimg.com/vi/SOGpvHun6vg/maxres1.jpg xxxxxxxxxx 1 https://i.ytimg.com/vi/SOGpvHun6vg/2.jpg 2 https://i.ytimg.com/vi/SOGpvHun6vg/mq2.jpg 3 https://i.ytimg.com/vi/SOGpvHun6vg/hq2.jpg 4 https://i.ytimg.com/vi/SOGpvHun6vg/sd2.jpg 5 https://i.ytimg.com/vi/SOGpvHun6vg/maxres2.jpg xxxxxxxxxx 1 https://i.ytimg.com/vi/SOGpvHun6vg/3.jpg 2 https://i.ytimg.com/vi/SOGpvHun6vg/mq3.jpg 3 https://i.ytimg.com/vi/SOGpvHun6vg/hq3.jpg 4 https://i.ytimg.com/vi/SOGpvHun6vg/sd3.jpg 5 https://i.ytimg.com/vi/SOGpvHun6vg/maxres3.jpg |
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.
xxxxxxxxxx
1
const createImageUrl = (videoId, imageQuality) => {
2
return 'https://i.ytimg.com/vi/' + videoId + '/' + imageQuality + 'default.jpg';
3
};
4
5
6
// Usage example:
7
8
const videoId = 'SOGpvHun6vg';
9
10
console.log(createImageUrl(videoId, '')); // size: 120x90
11
console.log(createImageUrl(videoId, 'mq')); // size: 320x180
12
console.log(createImageUrl(videoId, 'hq')); // size: 480x360
13
console.log(createImageUrl(videoId, 'sd')); // size: 640x480
14
console.log(createImageUrl(videoId, 'maxres')); // size: depends on YouTube and video resolution
In this example, we present a reusable functions that get the images using given YouTube video id and appends them inside the placeholderElement
.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<head>
5
<style>
6
7
img {
8
display: block;
9
}
10
11
</style>
12
</head>
13
<script>
14
15
function createImageUrl(videoId, imageQuality) {
16
return 'https://i.ytimg.com/vi/' + videoId + '/' + imageQuality + 'default.jpg';
17
}
18
19
function appendImageElement(parentElement, videoId, imageQuality, placeholderElement) {
20
var imageElement = document.createElement('img');
21
imageElement.src = createImageUrl(videoId, imageQuality);
22
parentElement.insertBefore(imageElement, placeholderElement);
23
}
24
25
26
// Usage example:
27
28
var videoId = 'SOGpvHun6vg';
29
30
appendImageElement(document.body, videoId, ''); // size: 120x90
31
appendImageElement(document.body, videoId, 'mq'); // size: 320x180
32
appendImageElement(document.body, videoId, 'hq'); // size: 480x360
33
appendImageElement(document.body, videoId, 'sd'); // size: 640x480
34
appendImageElement(document.body, videoId, 'maxres'); // size: depends on YouTube and video resolution
35
36
</script>
37
</body>
38
</html>