2019-02-02 05:22:09 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as tmp from 'tmp';
|
2019-05-15 14:27:20 +02:00
|
|
|
import { IImage, convertToJpeg } from './image-processor';
|
2019-02-02 05:22:09 +01:00
|
|
|
const ThumbnailGenerator = require('video-thumbnail-generator').default;
|
|
|
|
|
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]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const tg = new ThumbnailGenerator({
|
|
|
|
sourcePath: path,
|
|
|
|
thumbnailPath: outDir,
|
|
|
|
});
|
|
|
|
|
2019-02-02 15:30:34 +01:00
|
|
|
await tg.generateOneByPercent(5, {
|
|
|
|
size: '100%',
|
2019-02-02 05:22:09 +01:00
|
|
|
filename: 'output.png',
|
|
|
|
});
|
|
|
|
|
|
|
|
const outPath = `${outDir}/output.png`;
|
|
|
|
|
2019-05-15 14:27:20 +02:00
|
|
|
const thumbnail = await convertToJpeg(outPath, 498, 280);
|
2019-02-02 05:22:09 +01:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
fs.unlinkSync(outPath);
|
|
|
|
cleanup();
|
|
|
|
|
2019-02-02 15:30:34 +01:00
|
|
|
return thumbnail;
|
2019-02-02 05:22:09 +01:00
|
|
|
}
|