2019-02-02 13:22:09 +09:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as tmp from 'tmp';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { IImage, convertToJpeg } from './image-processor';
|
2019-07-20 03:28:14 +09:00
|
|
|
import * as FFmpeg from 'fluent-ffmpeg';
|
2019-02-02 13:22:09 +09:00
|
|
|
|
2019-02-05 03:01:36 +09:00
|
|
|
export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
|
2019-02-02 13:22:09 +09: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-20 03:28:14 +09:00
|
|
|
await new Promise((res, rej) => {
|
|
|
|
FFmpeg({
|
2021-12-09 23:58:30 +09:00
|
|
|
source: path,
|
2019-07-20 03:28:14 +09:00
|
|
|
})
|
|
|
|
.on('end', res)
|
|
|
|
.on('error', rej)
|
|
|
|
.screenshot({
|
|
|
|
folder: outDir,
|
|
|
|
filename: 'output.png',
|
|
|
|
count: 1,
|
2021-12-09 23:58:30 +09:00
|
|
|
timestamps: ['5%'],
|
2019-07-20 03:28:14 +09:00
|
|
|
});
|
2019-02-02 13:22:09 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
const outPath = `${outDir}/output.png`;
|
|
|
|
|
2019-05-15 21:27:20 +09:00
|
|
|
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
2019-02-02 13:22:09 +09:00
|
|
|
|
|
|
|
// cleanup
|
2019-07-20 03:28:14 +09:00
|
|
|
await fs.promises.unlink(outPath);
|
2019-02-02 13:22:09 +09:00
|
|
|
cleanup();
|
|
|
|
|
2019-02-02 23:30:34 +09:00
|
|
|
return thumbnail;
|
2019-02-02 13:22:09 +09:00
|
|
|
}
|