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";
|
2023-01-29 02:46:03 +01:00
|
|
|
import { convertToWebp } from "./image-processor.js";
|
2023-01-13 05:40:33 +01:00
|
|
|
import FFmpeg from "fluent-ffmpeg";
|
2019-02-02 05:22:09 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
export async function GenerateVideoThumbnail(source: string): Promise<IImage> {
|
2022-06-14 16:02:14 +02:00
|
|
|
const [dir, cleanup] = await createTempDir();
|
2019-02-02 05:22:09 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
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%"],
|
|
|
|
});
|
2019-07-19 20:28:14 +02:00
|
|
|
});
|
2019-02-02 05:22:09 +01:00
|
|
|
|
2023-01-29 02:46:03 +01:00
|
|
|
return await convertToWebp(`${dir}/out.png`, 996, 560);
|
2022-05-25 09:50:22 +02:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-02-02 05:22:09 +01:00
|
|
|
}
|