2022-02-27 03:07:39 +01:00
|
|
|
import * as fs from 'node:fs';
|
2022-06-14 16:02:14 +02:00
|
|
|
import { createTempDir } from '@/misc/create-temp.js';
|
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
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
.on('end', res)
|
|
|
|
.on('error', rej)
|
|
|
|
.screenshot({
|
2022-06-14 16:02:14 +02:00
|
|
|
folder: dir,
|
|
|
|
filename: 'out.png', // must have .png extension
|
2022-05-25 09:50:22 +02:00
|
|
|
count: 1,
|
|
|
|
timestamps: ['5%'],
|
|
|
|
});
|
2019-07-19 20:28:14 +02:00
|
|
|
});
|
2019-02-02 05:22:09 +01:00
|
|
|
|
2022-05-25 09:50:22 +02:00
|
|
|
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
|
2022-06-14 16:02:14 +02:00
|
|
|
return await convertToJpeg(`${dir}/out.png`, 498, 280);
|
2022-05-25 09:50:22 +02:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-02-02 05:22:09 +01:00
|
|
|
}
|