]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Keep ratio for thumbnails
authorChocobozzz <me@florianbigard.com>
Tue, 27 Feb 2018 10:29:24 +0000 (11:29 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 27 Feb 2018 10:29:24 +0000 (11:29 +0100)
server/helpers/ffmpeg-utils.ts
server/models/video/video.ts

index ad6f2f867f657ab916e7f506893ceef4d43589f6..57911bc7313ec071089e13fef4efb52e4883411a 100644 (file)
@@ -1,6 +1,8 @@
 import * as ffmpeg from 'fluent-ffmpeg'
 import { VideoResolution } from '../../shared/models/videos'
 import { CONFIG, MAX_VIDEO_TRANSCODING_FPS } from '../initializers'
+import { processImage } from './image-utils'
+import { join } from 'path'
 
 async function getVideoFileHeight (path: string) {
   const videoStream = await getVideoFileStream(path)
@@ -34,23 +36,25 @@ function getDurationFromVideoFile (path: string) {
   })
 }
 
-function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: string) {
+async function generateImageFromVideoFile (fromPath: string, folder: string, imageName: string, size: { width: number, height: number }) {
+  const pendingImageName = 'pending-' + imageName
+
   const options = {
-    filename: imageName,
+    filename: pendingImageName,
     count: 1,
     folder
   }
 
-  if (size !== undefined) {
-    options['size'] = size
-  }
-
-  return new Promise<string>((res, rej) => {
+  await new Promise<string>((res, rej) => {
     ffmpeg(fromPath)
       .on('error', rej)
       .on('end', () => res(imageName))
       .thumbnail(options)
   })
+
+  const pendingImagePath = join(folder, pendingImageName)
+  const destination = join(folder, imageName)
+  await processImage({ path: pendingImagePath }, destination, size)
 }
 
 type TranscodeOptions = {
index 839b81a8b2c5abeb53f8679fde7c3a9ca2cee734..4e065e377dd9bcd0ba3e29299d2efe28b1f8c185 100644 (file)
@@ -793,24 +793,20 @@ export class VideoModel extends Model<VideoModel> {
   }
 
   createPreview (videoFile: VideoFileModel) {
-    const imageSize = PREVIEWS_SIZE.width + 'x' + PREVIEWS_SIZE.height
-
     return generateImageFromVideoFile(
       this.getVideoFilePath(videoFile),
       CONFIG.STORAGE.PREVIEWS_DIR,
       this.getPreviewName(),
-      imageSize
+      PREVIEWS_SIZE
     )
   }
 
   createThumbnail (videoFile: VideoFileModel) {
-    const imageSize = THUMBNAILS_SIZE.width + 'x' + THUMBNAILS_SIZE.height
-
     return generateImageFromVideoFile(
       this.getVideoFilePath(videoFile),
       CONFIG.STORAGE.THUMBNAILS_DIR,
       this.getThumbnailName(),
-      imageSize
+      THUMBNAILS_SIZE
     )
   }