From 1e3a5b25c367b47e49808b443c504769db21ebe6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 31 May 2024 09:02:34 +0200 Subject: [PATCH] Plan to have multiple sizes in the future --- config/default.yaml | 20 ++++++++++--------- config/production.yaml.example | 12 +++++++++++ .../core/initializers/checker-after-init.ts | 4 ++++ .../core/initializers/checker-before-init.ts | 2 +- server/core/initializers/config.ts | 13 +----------- server/core/initializers/constants.ts | 14 ++++++------- .../shared/object-to-model-attributes.ts | 4 ++-- 7 files changed, 38 insertions(+), 31 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index bfae6490f..8e9c70deb 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -469,16 +469,18 @@ thumbnails: # Increasing this value will increase CPU and memory usage when generating the thumbnail, especially for high video resolution # Minimum value is 2 frames_to_analyze: 50 - size: - width: 280 - height: 157 - min_width: 150 -previews: - size: - width: 850 - height: 480 - min_width: 400 + # Only two sizes are currently supported for now (not less, not more) + # 1 size for the thumbnail (displayed in video miniatures) + # 1 size for the preview (displayed in the video player) + sizes: + - + width: 280 + height: 157 + + - + width: 850 + height: 480 stats: # Display registration requests stats (average response time, total requests...) diff --git a/config/production.yaml.example b/config/production.yaml.example index 7da979b13..a43c08bd3 100644 --- a/config/production.yaml.example +++ b/config/production.yaml.example @@ -468,6 +468,18 @@ thumbnails: # Minimum value is 2 frames_to_analyze: 50 + # Only two sizes are currently supported for now (not less, not more) + # 1 size for the thumbnail (displayed in video miniatures) + # 1 size for the preview (displayed in the video player) + sizes: + - + width: 280 + height: 157 + + - + width: 850 + height: 480 + stats: # Display registration requests stats (average response time, total requests...) registration_requests: diff --git a/server/core/initializers/checker-after-init.ts b/server/core/initializers/checker-after-init.ts index 75da82c46..34792b756 100644 --- a/server/core/initializers/checker-after-init.ts +++ b/server/core/initializers/checker-after-init.ts @@ -377,4 +377,8 @@ function checkThumbnailsConfig () { if (CONFIG.THUMBNAILS.GENERATION_FROM_VIDEO.FRAMES_TO_ANALYZE < 2) { throw new Error('thumbnails.generation_from_video.frames_to_analyze must be a number greater than 1') } + + if (!isArray(CONFIG.THUMBNAILS.SIZES) || CONFIG.THUMBNAILS.SIZES.length !== 2) { + throw new Error('thumbnails.sizes must be an array of 2 sizes') + } } diff --git a/server/core/initializers/checker-before-init.ts b/server/core/initializers/checker-before-init.ts index b0d218b8a..9dbd1fcad 100644 --- a/server/core/initializers/checker-before-init.ts +++ b/server/core/initializers/checker-before-init.ts @@ -40,7 +40,7 @@ function checkMissedConfig () { 'video_studio.enabled', 'video_studio.remote_runners.enabled', 'video_file.update.enabled', 'remote_runners.stalled_jobs.vod', 'remote_runners.stalled_jobs.live', - 'thumbnails.generation_from_video.frames_to_analyze', + 'thumbnails.generation_from_video.frames_to_analyze', 'thumbnails.sizes', 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'import.videos.concurrency', 'import.videos.timeout', 'import.video_channel_synchronization.enabled', 'import.video_channel_synchronization.max_per_user', 'import.video_channel_synchronization.check_interval', 'import.video_channel_synchronization.videos_limit_per_synchronization', diff --git a/server/core/initializers/config.ts b/server/core/initializers/config.ts index 0e1ea1411..d7749ce9a 100644 --- a/server/core/initializers/config.ts +++ b/server/core/initializers/config.ts @@ -372,18 +372,7 @@ const CONFIG = { GENERATION_FROM_VIDEO: { FRAMES_TO_ANALYZE: config.get('thumbnails.generation_from_video.frames_to_analyze') }, - SIZE: { - WIDTH: config.get('thumbnails.size.width'), - HEIGHT: config.get('thumbnails.size.height'), - MIN_WIDTH: config.get('thumbnails.size.min_width'), - } - }, - PREVIEWS: { - SIZE: { - WIDTH: config.get('previews.size.width'), - HEIGHT: config.get('previews.size.height'), - MIN_WIDTH: config.get('previews.size.min_width'), - } + SIZES: config.get<{ width: number, height: number }[]>('thumbnails.sizes') }, STATS: { REGISTRATION_REQUESTS: { diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.ts index e0a3cecb0..002ab9ee6 100644 --- a/server/core/initializers/constants.ts +++ b/server/core/initializers/constants.ts @@ -1,4 +1,4 @@ -import { randomInt } from '@peertube/peertube-core-utils' +import { maxBy, minBy, randomInt } from '@peertube/peertube-core-utils' import { AbuseState, AbuseStateType, @@ -903,14 +903,14 @@ const STATIC_MAX_AGE = { // Videos thumbnail size const THUMBNAILS_SIZE = { - width: CONFIG.THUMBNAILS.SIZE.WIDTH || 280, - height: CONFIG.THUMBNAILS.SIZE.HEIGHT || 157, - minWidth: CONFIG.THUMBNAILS.SIZE.MIN_WIDTH || 150 + width: minBy(CONFIG.THUMBNAILS.SIZES, 'width').width, + height: minBy(CONFIG.THUMBNAILS.SIZES, 'width').height, + minRemoteWidth: 150 } const PREVIEWS_SIZE = { - width: CONFIG.PREVIEWS.SIZE.WIDTH || 850, - height: CONFIG.PREVIEWS.SIZE.HEIGHT || 480, - minWidth: CONFIG.PREVIEWS.SIZE.MIN_WIDTH || 400 + width: maxBy(CONFIG.THUMBNAILS.SIZES, 'width').width, + height: maxBy(CONFIG.THUMBNAILS.SIZES, 'width').height, + minRemoteWidth: 400 } const ACTOR_IMAGES_SIZE: { [key in ActorImageType_Type]: { width: number, height: number }[] } = { [ActorImageType.AVATAR]: [ // 1/1 ratio diff --git a/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts b/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts index 75685376b..05aab3160 100644 --- a/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts +++ b/server/core/lib/activitypub/videos/shared/object-to-model-attributes.ts @@ -30,7 +30,7 @@ import { basename, extname } from 'path' import { getDurationFromActivityStream } from '../../activity.js' export function getThumbnailFromIcons (videoObject: VideoObject) { - let validIcons = videoObject.icon.filter(i => i.width > THUMBNAILS_SIZE.minWidth) + let validIcons = videoObject.icon.filter(i => i.width > THUMBNAILS_SIZE.minRemoteWidth) // Fallback if there are not valid icons if (validIcons.length === 0) validIcons = videoObject.icon @@ -38,7 +38,7 @@ export function getThumbnailFromIcons (videoObject: VideoObject) { } export function getPreviewFromIcons (videoObject: VideoObject) { - const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minWidth) + const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minRemoteWidth) return maxBy(validIcons, 'width') }