summaryrefslogtreecommitdiff
path: root/src/services/drive/generate-video-thumbnail.ts
blob: f0adc7c3388744ea28c50df6cf2b1abe2df4f711 (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
34
35
36
37
import * as fs from 'fs';
import * as tmp from 'tmp';
import { IImage, convertToJpeg } from './image-processor';
import * as FFmpeg from 'fluent-ffmpeg';

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]);
		});
	});

	await new Promise((res, rej) => {
		FFmpeg({
			source: path
		})
		.on('end', res)
		.on('error', rej)
		.screenshot({
			folder: outDir,
			filename: 'output.png',
			count: 1,
			timestamps: ['5%']
		});
	});

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

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

	// cleanup
	await fs.promises.unlink(outPath);
	cleanup();

	return thumbnail;
}