summaryrefslogtreecommitdiff
path: root/src/services/drive/generate-video-thumbnail.ts
blob: 5d7efff27b5564ded60de57d95c313810fd560cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as fs from 'fs';
import * as tmp from 'tmp';
import { IImage, ConvertToJpeg } from './image-processor';
const ThumbnailGenerator = require('video-thumbnail-generator').default;

export async function GenerateVideoThumbnail(path: string): Promise<IImage> {
	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,
	});

	await tg.generateOneByPercent(5, {
		size: '100%',
		filename: 'output.png',
	});

	const outPath = `${outDir}/output.png`;

	const thumbnail = await ConvertToJpeg(outPath, 498, 280);

	// cleanup
	fs.unlinkSync(outPath);
	cleanup();

	return thumbnail;
}