Extract a poster image from videos (not efficient yet)

This commit is contained in:
Alex Gleason 2025-02-28 09:58:14 -06:00
parent 813026e734
commit 43ec58085a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,4 +1,4 @@
import { transcodeVideo } from '@ditto/transcode';
import { extractVideoFrame, transcodeVideo } from '@ditto/transcode';
import { HTTPException } from '@hono/hono/http-exception';
import { logi } from '@soapbox/logi';
import { crypto } from '@std/crypto';
@ -62,6 +62,8 @@ export async function uploadFile(
const m = tags.find(([key]) => key === 'm')?.[1];
const dim = tags.find(([key]) => key === 'dim')?.[1];
const size = tags.find(([key]) => key === 'size')?.[1];
const image = tags.find(([key]) => key === 'image')?.[1];
const thumb = tags.find(([key]) => key === 'thumb')?.[1];
const blurhash = tags.find(([key]) => key === 'blurhash')?.[1];
if (!x) {
@ -77,6 +79,22 @@ export async function uploadFile(
tags.push(['size', file.size.toString()]);
}
if (baseType === 'video' && (!image || !thumb)) {
const tmp = new URL('file://' + await Deno.makeTempFile());
await Deno.writeFile(tmp, file.stream());
const bytes = await extractVideoFrame(tmp);
const [[, url]] = await uploader.upload(new File([bytes], 'thumb.jpg', { type: 'image/jpeg' }), { signal });
await Deno.remove(tmp);
if (!image) {
tags.push(['image', url]);
}
if (!thumb) {
tags.push(['thumb', url]);
}
}
// If the uploader didn't already, try to get a blurhash and media dimensions.
// This requires `MEDIA_ANALYZE=true` to be configured because it comes with security tradeoffs.
if (conf.mediaAnalyze && (!blurhash || !dim)) {