mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Improve blurhash performance of HD images
This commit is contained in:
parent
9051a59733
commit
8c61efd75e
1 changed files with 21 additions and 15 deletions
|
|
@ -113,25 +113,26 @@ export async function uploadFile(
|
||||||
const video = probe?.streams.find((stream) => stream.codec_type === 'video');
|
const video = probe?.streams.find((stream) => stream.codec_type === 'video');
|
||||||
|
|
||||||
if (video && video.width && video.height) {
|
if (video && video.width && video.height) {
|
||||||
|
const { width, height } = video;
|
||||||
|
|
||||||
if (!dim) {
|
if (!dim) {
|
||||||
tags.push(['dim', `${video.width}x${video.height}`]);
|
tags.push(['dim', `${width}x${height}`]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!blurhash) {
|
if (!blurhash) {
|
||||||
try {
|
try {
|
||||||
const img = sharp(bytes);
|
const { data, info } = await sharp(bytes)
|
||||||
const { width, height } = await img.metadata();
|
.raw()
|
||||||
|
.ensureAlpha()
|
||||||
|
.resize({
|
||||||
|
width: width > height ? undefined : 64,
|
||||||
|
height: height > width ? undefined : 64,
|
||||||
|
fit: 'inside',
|
||||||
|
})
|
||||||
|
.toBuffer({ resolveWithObject: true });
|
||||||
|
|
||||||
if (!blurhash && (width && height)) {
|
const blurhash = encode(new Uint8ClampedArray(data), info.width, info.height, 4, 4);
|
||||||
const pixels = await img
|
tags.push(['blurhash', blurhash]);
|
||||||
.raw()
|
|
||||||
.ensureAlpha()
|
|
||||||
.toBuffer({ resolveWithObject: false })
|
|
||||||
.then((buffer) => new Uint8ClampedArray(buffer));
|
|
||||||
|
|
||||||
const blurhash = encode(pixels, width, height, 4, 4);
|
|
||||||
tags.push(['blurhash', blurhash]);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logi({ level: 'error', ns: 'ditto.upload.analyze', error: errorJson(e) });
|
logi({ level: 'error', ns: 'ditto.upload.analyze', error: errorJson(e) });
|
||||||
}
|
}
|
||||||
|
|
@ -156,6 +157,11 @@ export async function uploadFile(
|
||||||
const pixels = await img
|
const pixels = await img
|
||||||
.raw()
|
.raw()
|
||||||
.ensureAlpha()
|
.ensureAlpha()
|
||||||
|
.resize({
|
||||||
|
width: width > height ? undefined : 64,
|
||||||
|
height: height > width ? undefined : 64,
|
||||||
|
fit: 'inside',
|
||||||
|
})
|
||||||
.toBuffer({ resolveWithObject: false })
|
.toBuffer({ resolveWithObject: false })
|
||||||
.then((buffer) => new Uint8ClampedArray(buffer));
|
.then((buffer) => new Uint8ClampedArray(buffer));
|
||||||
|
|
||||||
|
|
@ -179,8 +185,6 @@ export async function uploadFile(
|
||||||
|
|
||||||
dittoUploads.set(upload.id, upload);
|
dittoUploads.set(upload.id, upload);
|
||||||
|
|
||||||
perf.mark('end');
|
|
||||||
|
|
||||||
const timing = [
|
const timing = [
|
||||||
perf.measure('probe', 'probe-start', 'probe-end'),
|
perf.measure('probe', 'probe-start', 'probe-end'),
|
||||||
perf.measure('transcode', 'transcode-start', 'transcode-end'),
|
perf.measure('transcode', 'transcode-start', 'transcode-end'),
|
||||||
|
|
@ -192,6 +196,8 @@ export async function uploadFile(
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
perf.mark('end');
|
||||||
|
|
||||||
logi({
|
logi({
|
||||||
level: 'info',
|
level: 'info',
|
||||||
ns: 'ditto.upload',
|
ns: 'ditto.upload',
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue