iceshrimp-161sh/packages/backend/src/services/drive/generate-video-thumbnail.ts

30 lines
706 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import * as fs from "node:fs";
import { createTempDir } from "@/misc/create-temp.js";
import type { IImage } from "./image-processor.js";
import { convertToWebp } from "./image-processor.js";
2023-01-13 05:40:33 +01:00
import FFmpeg from "fluent-ffmpeg";
export async function GenerateVideoThumbnail(source: string): Promise<IImage> {
const [dir, cleanup] = await createTempDir();
try {
await new Promise((res, rej) => {
FFmpeg({
source,
})
2023-01-13 05:40:33 +01:00
.on("end", res)
.on("error", rej)
.screenshot({
folder: dir,
filename: "out.png", // must have .png extension
count: 1,
timestamps: ["5%"],
});
});
return await convertToWebp(`${dir}/out.png`, 996, 560);
} finally {
cleanup();
}
}