2022-02-27 03:07:39 +01:00
|
|
|
import * as fs from 'node:fs';
|
2022-05-25 09:50:22 +02:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import { createTemp } 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> {
|
|
|
|
const [file, cleanup] = await createTemp();
|
|
|
|
const parsed = path.parse(file);
|
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({
|
|
|
|
folder: parsed.dir,
|
|
|
|
filename: parsed.base,
|
|
|
|
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-12 17:25:06 +02:00
|
|
|
return await convertToJpeg(file, 498, 280);
|
2022-05-25 09:50:22 +02:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
2019-02-02 05:22:09 +01:00
|
|
|
}
|