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

31 lines
810 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 { convertToJpeg } from "./image-processor.js";
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%"],
});
});
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
return await convertToJpeg(`${dir}/out.png`, 498, 280);
} finally {
cleanup();
}
}