1 import { join } from 'path'
2 import { ThumbnailType } from '@shared/models'
3 import { generateImageFilename, generateImageFromVideoFile, processImage } from '../helpers/image-utils'
4 import { downloadImage } from '../helpers/requests'
5 import { CONFIG } from '../initializers/config'
6 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
7 import { ThumbnailModel } from '../models/video/thumbnail'
8 import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
9 import { MThumbnail } from '../types/models/video/thumbnail'
10 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
11 import { VideoPathManager } from './video-path-manager'
13 type ImageSize = { height?: number, width?: number }
15 function updatePlaylistMiniatureFromExisting (options: {
17 playlist: MVideoPlaylistThumbnail
18 automaticallyGenerated: boolean
19 keepOriginal?: boolean // default to false
22 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
23 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
24 const type = ThumbnailType.MINIATURE
26 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
27 return updateThumbnailFromFunction({
33 automaticallyGenerated,
38 function updatePlaylistMiniatureFromUrl (options: {
40 playlist: MVideoPlaylistThumbnail
43 const { downloadUrl, playlist, size } = options
44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
45 const type = ThumbnailType.MINIATURE
47 // Only save the file URL if it is a remote playlist
48 const fileUrl = playlist.isOwned()
52 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
53 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
56 function updateVideoMiniatureFromUrl (options: {
58 video: MVideoThumbnail
62 const { downloadUrl, video, type, size } = options
63 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
65 // Only save the file URL if it is a remote video
66 const fileUrl = video.isOwned()
70 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
72 // Do not change the thumbnail filename if the file did not change
73 const filename = thumbnailUrlChanged
75 : existingThumbnail.filename
77 const thumbnailCreator = () => {
78 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
80 return Promise.resolve()
83 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
86 function updateVideoMiniatureFromExisting (options: {
88 video: MVideoThumbnail
90 automaticallyGenerated: boolean
92 keepOriginal?: boolean // default to false
94 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
96 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
97 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
99 return updateThumbnailFromFunction({
105 automaticallyGenerated,
110 function generateVideoMiniature (options: {
111 video: MVideoThumbnail
112 videoFile: MVideoFile
115 const { video, videoFile, type } = options
117 return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
118 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
120 const thumbnailCreator = videoFile.isAudio()
121 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
122 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
124 return updateThumbnailFromFunction({
130 automaticallyGenerated: true,
136 function updatePlaceholderThumbnail (options: {
138 video: MVideoThumbnail
142 const { fileUrl, video, type, size } = options
143 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
145 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
147 const thumbnail = existingThumbnail || new ThumbnailModel()
149 // Do not change the thumbnail filename if the file did not change
150 const filename = thumbnailUrlChanged
152 : existingThumbnail.filename
154 thumbnail.filename = filename
155 thumbnail.height = height
156 thumbnail.width = width
157 thumbnail.type = type
158 thumbnail.fileUrl = fileUrl
163 // ---------------------------------------------------------------------------
166 generateVideoMiniature,
167 updateVideoMiniatureFromUrl,
168 updateVideoMiniatureFromExisting,
169 updatePlaceholderThumbnail,
170 updatePlaylistMiniatureFromUrl,
171 updatePlaylistMiniatureFromExisting
174 function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
175 const existingUrl = existingThumbnail
176 ? existingThumbnail.fileUrl
179 // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
180 return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
183 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
184 const filename = playlist.generateThumbnailName()
185 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
190 existingThumbnail: playlist.Thumbnail,
191 outputPath: join(basePath, filename),
192 height: size ? size.height : THUMBNAILS_SIZE.height,
193 width: size ? size.width : THUMBNAILS_SIZE.width
197 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
198 const existingThumbnail = Array.isArray(video.Thumbnails)
199 ? video.Thumbnails.find(t => t.type === type)
202 if (type === ThumbnailType.MINIATURE) {
203 const filename = generateImageFilename()
204 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
210 outputPath: join(basePath, filename),
211 height: size ? size.height : THUMBNAILS_SIZE.height,
212 width: size ? size.width : THUMBNAILS_SIZE.width
216 if (type === ThumbnailType.PREVIEW) {
217 const filename = generateImageFilename()
218 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
224 outputPath: join(basePath, filename),
225 height: size ? size.height : PREVIEWS_SIZE.height,
226 width: size ? size.width : PREVIEWS_SIZE.width
233 async function updateThumbnailFromFunction (parameters: {
234 thumbnailCreator: () => Promise<any>
239 automaticallyGenerated?: boolean
241 existingThumbnail?: MThumbnail
250 automaticallyGenerated = null,
254 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
255 ? existingThumbnail.filename
258 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
260 thumbnail.filename = filename
261 thumbnail.height = height
262 thumbnail.width = width
263 thumbnail.type = type
264 thumbnail.fileUrl = fileUrl
265 thumbnail.automaticallyGenerated = automaticallyGenerated
267 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
269 await thumbnailCreator()