Skip to content

Commit a3c373a

Browse files
committed
Thumbnail changes:
Added - Added a `thumbnail` prop to allow the alternate 1/2/3 thumbnails to be used Changed - Use i.ytimg.com instead of img.youtube.com, as per YouTube and its API - Allow the mq/hq/sd abbreviations YouTube uses to be passed for thumbnailRes
1 parent 3e555bf commit a3c373a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ npm i astro-lazy-youtube-embed
1010

1111
## Usage
1212

13+
### Thumbnails
14+
15+
Use `thumbnailRes` to control the resolution of the thumbnail used for the placeholder - you can pass the resolution (120, 320, 480, 640, or 1280), or one of the names YouTube maps resolutions to (default, medium/mq, high/hq, standard/sd, or maxres).
16+
17+
> [!CAUTION]
18+
> Not all videos will have higher resolution thumbnails available, especially older videos - check if the thumbnail is available when using higher resolutions.
19+
20+
Use `thumbnail` (1, 2, or 3) to use one of the 3 alternate thumbnails YouTube provides as the placeholder - these are screenshots from the video itself.
21+
22+
> [!NOTE]
23+
> Some older videos will have higher resolution thumbnails available for their alternate thumbnails than for the default thumbnail.
24+
25+
### Player parameters
26+
1327
Use `embedParams` to pass [YouTube IFrame Player API parameters](https://developers.google.com/youtube/player_parameters#Parameters) for the embed, e.g. to set start & stop times to play a particular section when clicked.
1428

1529
Default `embedParams` are:
@@ -21,7 +35,7 @@ Default `embedParams` are:
2135
}
2236
```
2337

24-
Use `thumbnailRes` to control the resolution of the thumbnail used for the placeholder.
38+
### Example
2539

2640
```astro
2741
---

YouTube.astro

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
interface Props extends astroHTML.JSX.IframeHTMLAttributes {
33
embedParams?: EmbedParams
4+
thumbnail?: Thumbnail
45
thumbnailRes?: ThumbnailRes
56
title: string
67
videoId: string
@@ -33,32 +34,38 @@ interface EmbedParams {
3334
3435
type ToggleParam = 0 | '0' | 1 | '1'
3536
36-
type ThumbnailRes = 120 | '120' | 'default' | 320 | '320' | 'medium' | 480 | '480' | 'high' | 640 | '640' | 'standard' | 1280 | '1280' | 'maxres'
37+
type Thumbnail = 'default' | 1 | '1' | 2 | '2' | 3 | '3'
38+
39+
type ThumbnailRes = 120 | '120' | 'default' | 320 | '320' | 'medium' | 'mq' | 480 | '480' | 'high' | 'hq' | 640 | '640' | 'standard' | 'sd' | 1280 | '1280' | 'maxres'
3740
3841
const QUALITY_PREFIXES = {
3942
120: '',
4043
default: '',
4144
320: 'mq',
4245
medium: 'mq',
46+
mq: 'mq',
4347
480: 'hq',
4448
high: 'hq',
49+
hq: 'hq',
4550
640: 'sd',
4651
standard: 'sd',
52+
sd: 'sd',
4753
1280: 'maxres',
4854
maxres: 'maxres',
4955
}
5056
5157
let {
5258
embedParams,
53-
thumbnailRes = 'standard',
59+
thumbnail = 'default',
60+
thumbnailRes = 'sd',
5461
title,
5562
videoId,
5663
...attrs
5764
} = Astro.props as Props
5865
5966
let params: EmbedParams = {autoplay: 1, ...embedParams}
6067
61-
let thumbnailUrl = `https://img.youtube.com/vi/${videoId}/${QUALITY_PREFIXES[thumbnailRes]}default.jpg`
68+
let thumbnailUrl = `https://i.ytimg.com/vi/${videoId}/${QUALITY_PREFIXES[thumbnailRes]}${thumbnail}.jpg`
6269
let embedQuery = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
6370
let embedUrl = `https://www.youtube.com/embed${videoId ? `/${videoId}` : ''}${embedQuery ? `?${embedQuery}` : ''}`
6471
let playButtonSvg = `<svg height="100%" version="1.1" viewBox="0 0 68 48" width="100%"><path d="M66.52,7.74c-0.78-2.93-2.49-5.41-5.42-6.19C55.79,.13,34,0,34,0S12.21,.13,6.9,1.55 C3.97,2.33,2.27,4.81,1.48,7.74C0.06,13.05,0,24,0,24s0.06,10.95,1.48,16.26c0.78,2.93,2.49,5.41,5.42,6.19 C12.21,47.87,34,48,34,48s21.79-0.13,27.1-1.55c2.93-0.78,4.64-3.26,5.42-6.19C67.94,34.95,68,24,68,24S67.94,13.05,66.52,7.74z" fill="#f00"></path><path d="M 45,24 27,14 27,34" fill="#fff"></path></svg>`

0 commit comments

Comments
 (0)