2022-02-27 03:07:39 +01:00
|
|
|
import * as fs from 'node:fs';
|
2019-02-02 05:22:09 +01:00
|
|
|
import * as tmp from 'tmp';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { IImage, convertToJpeg } from './image-processor.js';
|
2022-05-19 09:19:23 +02:00
|
|
|
import FFmpeg from 'fluent-ffmpeg';
|
2019-02-02 05:22:09 +01:00
|
|
|
|
2019-02-04 19:01:36 +01:00
|
|
|
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
2019-02-02 05:22:09 +01:00
|
|
|
const [outDir, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
|
|
|
tmp.dir((e, path, cleanup) => {
|
|
|
|
if (e) return rej(e);
|
|
|
|
res([path, cleanup]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-19 20:28:14 +02:00
|
|
|
await new Promise((res, rej) => {
|
|
|
|
FFmpeg({
|
2021-12-09 15:58:30 +01:00
|
|
|
source: path,
|
2019-07-19 20:28:14 +02:00
|
|
|
})
|
|
|
|
.on('end', res)
|
|
|
|
.on('error', rej)
|
|
|
|
.screenshot({
|
|
|
|
folder: outDir,
|
|
|
|
filename: 'output.png',
|
|
|
|
count: 1,
|
2021-12-09 15:58:30 +01:00
|
|
|
timestamps: ['5%'],
|
2019-07-19 20:28:14 +02:00
|
|
|
});
|
2019-02-02 05:22:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const outPath = `${outDir}/output.png`;
|
|
|
|
|
2022-04-28 04:14:03 +02:00
|
|
|
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
|
2019-05-15 14:27:20 +02:00
|
|
|
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
2019-02-02 05:22:09 +01:00
|
|
|
|
|
|
|
// cleanup
|
2019-07-19 20:28:14 +02:00
|
|
|
await fs.promises.unlink(outPath);
|
2019-02-02 05:22:09 +01:00
|
|
|
cleanup();
|
|
|
|
|
2019-02-02 15:30:34 +01:00
|
|
|
return thumbnail;
|
2019-02-02 05:22:09 +01:00
|
|
|
}
|