Fix video thumbnails (#4095)

* Fix video thumbnails

* Fix import
This commit is contained in:
MeiMei 2019-02-02 23:30:34 +09:00 committed by syuilo
parent 2b0cb6d728
commit 84931003ea
2 changed files with 14 additions and 6 deletions

View file

@ -122,8 +122,6 @@ async function save(path: string, name: string, type: string, hash: string, size
} else if (type.startsWith('video/')) { } else if (type.startsWith('video/')) {
try { try {
thumbnail = await GenerateVideoThumbnail(path); thumbnail = await GenerateVideoThumbnail(path);
thumbnailExt = 'png';
thumbnailType = 'image/png';
} catch (e) { } catch (e) {
console.log(`GenerateVideoThumbnail failed: ${e}`); console.log(`GenerateVideoThumbnail failed: ${e}`);
} }

View file

@ -1,5 +1,6 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as tmp from 'tmp'; import * as tmp from 'tmp';
import * as sharp from 'sharp';
const ThumbnailGenerator = require('video-thumbnail-generator').default; const ThumbnailGenerator = require('video-thumbnail-generator').default;
export async function GenerateVideoThumbnail(path: string): Promise<Buffer> { export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
@ -15,18 +16,27 @@ export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
thumbnailPath: outDir, thumbnailPath: outDir,
}); });
await tg.generateOneByPercent(10, { await tg.generateOneByPercent(5, {
size: '498x280', size: '100%',
filename: 'output.png', filename: 'output.png',
}); });
const outPath = `${outDir}/output.png`; const outPath = `${outDir}/output.png`;
const buffer = fs.readFileSync(outPath); const thumbnail = await sharp(outPath)
.resize(498, 280, {
fit: 'inside',
withoutEnlargement: true
})
.jpeg({
quality: 85,
progressive: true
})
.toBuffer();
// cleanup // cleanup
fs.unlinkSync(outPath); fs.unlinkSync(outPath);
cleanup(); cleanup();
return buffer; return thumbnail;
} }