From 75faf5b7873fd4aec149df35eb392e68bedf2ce4 Mon Sep 17 00:00:00 2001 From: mei23 Date: Sat, 2 Feb 2019 22:42:57 +0900 Subject: [PATCH 1/2] Fix video thumbnails --- src/services/drive/add-file.ts | 2 -- src/services/drive/generate-video-thumbnail.ts | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 1d5f31c50474..c6fa44a3ef37 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -122,8 +122,6 @@ async function save(path: string, name: string, type: string, hash: string, size } else if (type.startsWith('video/')) { try { thumbnail = await GenerateVideoThumbnail(path); - thumbnailExt = 'png'; - thumbnailType = 'image/png'; } catch (e) { console.log(`GenerateVideoThumbnail failed: ${e}`); } diff --git a/src/services/drive/generate-video-thumbnail.ts b/src/services/drive/generate-video-thumbnail.ts index 0ee69be4a7ef..613f5054fb64 100644 --- a/src/services/drive/generate-video-thumbnail.ts +++ b/src/services/drive/generate-video-thumbnail.ts @@ -1,5 +1,6 @@ import * as fs from 'fs'; import * as tmp from 'tmp'; +import sharp = require('sharp'); const ThumbnailGenerator = require('video-thumbnail-generator').default; export async function GenerateVideoThumbnail(path: string): Promise { @@ -15,18 +16,27 @@ export async function GenerateVideoThumbnail(path: string): Promise { thumbnailPath: outDir, }); - await tg.generateOneByPercent(10, { - size: '498x280', + await tg.generateOneByPercent(5, { + size: '100%', filename: '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 fs.unlinkSync(outPath); cleanup(); - return buffer; + return thumbnail; } From e003d29929ae4084fc94707c9f3979e7bf1f4b2b Mon Sep 17 00:00:00 2001 From: mei23 Date: Sat, 2 Feb 2019 23:27:02 +0900 Subject: [PATCH 2/2] Fix import --- src/services/drive/generate-video-thumbnail.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/drive/generate-video-thumbnail.ts b/src/services/drive/generate-video-thumbnail.ts index 613f5054fb64..14b3b98f97ea 100644 --- a/src/services/drive/generate-video-thumbnail.ts +++ b/src/services/drive/generate-video-thumbnail.ts @@ -1,6 +1,6 @@ import * as fs from 'fs'; import * as tmp from 'tmp'; -import sharp = require('sharp'); +import * as sharp from 'sharp'; const ThumbnailGenerator = require('video-thumbnail-generator').default; export async function GenerateVideoThumbnail(path: string): Promise {