|
| 1 | +<script lang="ts" module> |
| 2 | + /** |
| 3 | + Removes cache created by <VideoKnob /> component. |
| 4 | + */ |
| 5 | + export async function cleanVideoKnobCache() { |
| 6 | + await caches.delete('svelte-knobs/cache'); |
| 7 | + } |
| 8 | +</script> |
| 9 | + |
1 | 10 | <script lang="ts"> |
2 | 11 | import { normalize } from './params.js'; |
3 | 12 | import { onMount } from 'svelte'; |
|
12 | 21 | numberOfFrames: number; |
13 | 22 | width?: number; |
14 | 23 | height?: number; |
| 24 | + cacheFrames?: boolean; |
15 | 25 | } & SharedKnobProps; |
16 | 26 |
|
17 | 27 | let { |
|
33 | 43 | height = 80, |
34 | 44 | disabled = false, |
35 | 45 | draggable = true, |
| 46 | + cacheFrames = false, |
36 | 47 | colors = {} |
37 | 48 | }: Props = $props(); |
38 | 49 |
|
|
63 | 74 | ctx?.fillText('Loading...', 0, canvas.height / 2); |
64 | 75 | }); |
65 | 76 |
|
66 | | - onMount(() => { |
67 | | - if (!source) return; |
68 | | -
|
69 | | - frames.splice(0); |
70 | | -
|
71 | | - const video = document.createElement('video'); |
72 | | - video.src = source; |
| 77 | + async function loadImage(src: string) { |
| 78 | + return new Promise<HTMLImageElement>((resolve, reject) => { |
| 79 | + const image = new Image(); |
| 80 | + image.src = src; |
| 81 | + image.onerror = reject; |
| 82 | + image.onload = () => resolve(image); |
| 83 | + }); |
| 84 | + } |
73 | 85 |
|
74 | | - video.onloadeddata = async () => { |
75 | | - if (video.duration === 0) throw Error('Video is empty'); |
76 | | - duration = video.duration; |
| 86 | + async function loadFromCache() { |
| 87 | + const cache = await caches.open('svelte-knobs/cache'); |
77 | 88 |
|
78 | | - const fps = duration / numberOfFrames; |
79 | | - const decoderCanvas = document.createElement('canvas'); |
80 | | - const dctx = decoderCanvas.getContext('2d'); |
| 89 | + let i = 0; |
| 90 | + let hasReadCache = false; |
81 | 91 |
|
82 | | - decoderCanvas.width = width; |
83 | | - decoderCanvas.height = height; |
| 92 | + while (i < numberOfFrames) { |
| 93 | + const response = await cache.match(`${source}:${i}`); |
| 94 | + if (!response) break; |
84 | 95 |
|
85 | | - console.time('Decoding video'); |
| 96 | + const url = URL.createObjectURL(await response.blob()); |
| 97 | + const image = await loadImage(url); |
| 98 | + frames.push(image); |
86 | 99 |
|
87 | | - let i = -1; |
| 100 | + if (i === 0) { |
| 101 | + ctx?.clearRect(0, 0, canvas.width, canvas.height); |
| 102 | + ctx?.drawImage(image, 0, 0); |
| 103 | + } |
88 | 104 |
|
89 | | - async function decodeFrame() { |
90 | | - if (video.currentTime >= video.duration) return; |
91 | | - if (!dctx) throw Error('Failed to create canvas context'); |
| 105 | + if (i === numberOfFrames - 1) hasReadCache = true; |
| 106 | + i++; |
| 107 | + } |
92 | 108 |
|
93 | | - dctx.clearRect(0, 0, decoderCanvas.width, decoderCanvas.height); |
94 | | - dctx.drawImage(video, 0, 0, decoderCanvas.width, decoderCanvas.height); |
| 109 | + return hasReadCache; |
| 110 | + } |
95 | 111 |
|
96 | | - return new Promise<void>((resolve, reject) => { |
97 | | - decoderCanvas.toBlob((blob) => { |
98 | | - if (!blob) return reject('Failed to create canvas blob'); |
| 112 | + async function decodeVideo() { |
| 113 | + const video = document.createElement('video'); |
| 114 | + video.src = source; |
99 | 115 |
|
100 | | - const url = URL.createObjectURL(blob); |
101 | | - const image = new Image(); |
| 116 | + const decoderCanvas = document.createElement('canvas'); |
| 117 | + const dctx = decoderCanvas.getContext('2d'); |
| 118 | + if (!dctx) throw new Error('Failed to create canvas context'); |
| 119 | +
|
| 120 | + return new Promise<void>((resolve, reject) => { |
| 121 | + video.onerror = reject; |
| 122 | + video.onloadeddata = async () => { |
| 123 | + if (video.duration === 0) throw new Error('Video is empty'); |
| 124 | + duration = video.duration; |
| 125 | + const fps = video.duration / numberOfFrames; |
| 126 | + decoderCanvas.width = width; |
| 127 | + decoderCanvas.height = height; |
| 128 | +
|
| 129 | + let i = -1; |
| 130 | + while (++i < numberOfFrames) { |
| 131 | + video.currentTime = i * fps; |
| 132 | + await new Promise((resolve) => video.requestVideoFrameCallback(resolve)); |
| 133 | +
|
| 134 | + dctx.clearRect(0, 0, decoderCanvas.width, decoderCanvas.height); |
| 135 | + dctx.drawImage(video, 0, 0, decoderCanvas.width, decoderCanvas.height); |
| 136 | +
|
| 137 | + const blob = await new Promise<Blob>((resolve, reject) => { |
| 138 | + decoderCanvas.toBlob( |
| 139 | + (blob) => (blob ? resolve(blob) : reject('Failed to create canvas blob')), |
| 140 | + 'image/webp' |
| 141 | + ); |
| 142 | + }); |
| 143 | +
|
| 144 | + const url = URL.createObjectURL(blob); |
| 145 | + const image = await loadImage(url); |
| 146 | + frames.push(image); |
| 147 | +
|
| 148 | + if (i === 0) { |
| 149 | + ctx?.clearRect(0, 0, canvas.width, canvas.height); |
| 150 | + ctx?.drawImage(image, 0, 0); |
| 151 | + } |
| 152 | +
|
| 153 | + const response = new Response(blob, { headers: { 'Content-Type': 'image/webp' } }); |
| 154 | +
|
| 155 | + if (cacheFrames) { |
| 156 | + const cache = await caches.open('svelte-knobs/cache'); |
| 157 | + await cache.put(`${source}:${i}`, response); |
| 158 | + } |
| 159 | + } |
| 160 | +
|
| 161 | + resolve(); |
| 162 | + }; |
| 163 | + }); |
| 164 | + } |
102 | 165 |
|
103 | | - image.src = url; |
104 | | - image.onerror = reject; |
105 | | - image.onload = () => { |
106 | | - if (i === 0) { |
107 | | - ctx?.clearRect(0, 0, canvas.width, canvas.height); |
108 | | - ctx?.drawImage(image, 0, 0); |
109 | | - } |
| 166 | + // @ts-expect-error oops |
| 167 | + onMount(async () => { |
| 168 | + ctx = canvas.getContext('2d'); |
| 169 | + if (ctx) ctx.fillText('Loading...', 0, canvas.height / 2); |
110 | 170 |
|
111 | | - frames.push(image); |
112 | | - resolve(); |
113 | | - }; |
114 | | - }, 'image/webp'); |
115 | | - }); |
116 | | - } |
| 171 | + console.time('Decoding video'); |
117 | 172 |
|
118 | | - async function waitForFrame(video: HTMLVideoElement) { |
119 | | - return new Promise((resolve) => video.requestVideoFrameCallback(resolve)); |
| 173 | + if (cacheFrames) { |
| 174 | + const hasReadCache = await loadFromCache(); |
| 175 | + if (hasReadCache) { |
| 176 | + console.log('cache hit'); |
| 177 | + console.timeEnd('Decoding video'); |
| 178 | + return; |
120 | 179 | } |
| 180 | + } |
121 | 181 |
|
122 | | - while (i < numberOfFrames) { |
123 | | - video.currentTime = ++i * fps; |
124 | | -
|
125 | | - await waitForFrame(video); |
126 | | - await decodeFrame(); |
127 | | - } |
| 182 | + frames.splice(0); |
| 183 | + await decodeVideo(); |
128 | 184 |
|
129 | | - console.timeEnd('Decoding video'); |
130 | | - }; |
| 185 | + console.timeEnd('Decoding video'); |
131 | 186 |
|
132 | 187 | return () => { |
133 | 188 | for (const { src } of frames) { |
|
0 commit comments