From 06bcfbd9f715055f2f00bb66149b1dba926d007a Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 14 Jan 2020 23:34:03 +0100 Subject: Downsample to the closest divisor standard framerate --- server/controllers/api/videos/index.ts | 3 ++- server/helpers/ffmpeg-utils.ts | 11 +++++++---- server/initializers/constants.ts | 2 ++ server/middlewares/validators/videos/videos.ts | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 8d4ff07eb..a593f7076 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -12,7 +12,8 @@ import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, - VIDEO_PRIVACIES + VIDEO_PRIVACIES, + VIDEO_TRANSCODING_FPS } from '../../../initializers/constants' import { changeVideoChannelShare, diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 00c32e99a..78f9ba07c 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts @@ -286,13 +286,16 @@ export { async function buildx264Command (command: ffmpeg.FfmpegCommand, options: TranscodeOptions) { let fps = await getVideoFileFPS(options.inputPath) - // On small/medium resolutions, limit FPS if ( + // On small/medium resolutions, limit FPS options.resolution !== undefined && options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && - fps > VIDEO_TRANSCODING_FPS.AVERAGE + fps > VIDEO_TRANSCODING_FPS.AVERAGE || + // If the video is doesn't match had standard + !VIDEO_TRANSCODING_FPS.HD_STANDARD.map(value => fps % value).includes(0) ) { - fps = VIDEO_TRANSCODING_FPS.AVERAGE + // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value + fps = VIDEO_TRANSCODING_FPS.STANDARD.sort((a, b) => fps % a - fps % b)[0] } command = await presetH264(command, options.inputPath, options.resolution, fps) @@ -305,7 +308,7 @@ async function buildx264Command (command: ffmpeg.FfmpegCommand, options: Transco if (fps) { // Hard FPS limits - if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.MAX + if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = VIDEO_TRANSCODING_FPS.HD_STANDARD.sort((a, b) => fps % a - fps % b)[0] else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN command = command.withFPS(fps) diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index e01ab8943..64803b1db 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -310,6 +310,8 @@ let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = { MIN: 10, + STANDARD: [24, 25, 30], + HD_STANDARD: [50, 60], AVERAGE: 30, MAX: 60, KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum) diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 6733d9dec..00ee375cb 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts @@ -81,7 +81,7 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([ duration = await getDurationFromVideoFile(videoFile.path) } catch (err) { logger.error('Invalid input file in videosAddValidator.', { err }) - res.status(400) + res.status(422) .json({ error: 'Invalid input file.' }) return cleanUpReqFiles(req) -- cgit v1.2.3