Skip to content

Commit

Permalink
Keep ratio for thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 27, 2018
1 parent ea99d15 commit 2667072
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 11 additions & 7 deletions server/helpers/ffmpeg-utils.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 = {
Expand Down
8 changes: 2 additions & 6 deletions server/models/video/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down

0 comments on commit 2667072

Please sign in to comment.