2019-02-02 06:22:09 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as tmp from 'tmp';
|
2019-02-02 16:30:34 +02:00
|
|
|
import * as sharp from 'sharp';
|
2019-02-02 06:22:09 +02:00
|
|
|
const ThumbnailGenerator = require('video-thumbnail-generator').default;
|
|
|
|
|
|
|
|
export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
|
|
|
|
const [outDir, cleanup] = await new Promise<[string, any]>((res, rej) => {
|
|
|
|
tmp.dir((e, path, cleanup) => {
|
|
|
|
if (e) return rej(e);
|
|
|
|
res([path, cleanup]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const tg = new ThumbnailGenerator({
|
|
|
|
sourcePath: path,
|
|
|
|
thumbnailPath: outDir,
|
|
|
|
});
|
|
|
|
|
2019-02-02 16:30:34 +02:00
|
|
|
await tg.generateOneByPercent(5, {
|
|
|
|
size: '100%',
|
2019-02-02 06:22:09 +02:00
|
|
|
filename: 'output.png',
|
|
|
|
});
|
|
|
|
|
|
|
|
const outPath = `${outDir}/output.png`;
|
|
|
|
|
2019-02-02 16:30:34 +02:00
|
|
|
const thumbnail = await sharp(outPath)
|
|
|
|
.resize(498, 280, {
|
|
|
|
fit: 'inside',
|
|
|
|
withoutEnlargement: true
|
|
|
|
})
|
|
|
|
.jpeg({
|
|
|
|
quality: 85,
|
|
|
|
progressive: true
|
|
|
|
})
|
|
|
|
.toBuffer();
|
2019-02-02 06:22:09 +02:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
fs.unlinkSync(outPath);
|
|
|
|
cleanup();
|
|
|
|
|
2019-02-02 16:30:34 +02:00
|
|
|
return thumbnail;
|
2019-02-02 06:22:09 +02:00
|
|
|
}
|