1 import { join } from 'path'
2 import { ThumbnailType } from '@shared/models'
3 import { generateImageFilename, generateImageFromVideoFile } from '../helpers/image-utils'
4 import { CONFIG } from '../initializers/config'
5 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
6 import { ThumbnailModel } from '../models/video/thumbnail'
7 import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
8 import { MThumbnail } from '../types/models/video/thumbnail'
9 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
10 import { downloadImageFromWorker } from './local-actor'
11 import { VideoPathManager } from './video-path-manager'
12 import { processImageFromWorker } from './worker/parent-process'
14 type ImageSize = { height?: number, width?: number }
16 function updatePlaylistMiniatureFromExisting (options: {
18 playlist: MVideoPlaylistThumbnail
19 automaticallyGenerated: boolean
20 keepOriginal?: boolean // default to false
23 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
24 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
25 const type = ThumbnailType.MINIATURE
27 const thumbnailCreator = () => {
28 return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
31 return updateThumbnailFromFunction({
37 automaticallyGenerated,
42 function updatePlaylistMiniatureFromUrl (options: {
44 playlist: MVideoPlaylistThumbnail
47 const { downloadUrl, playlist, size } = options
48 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
49 const type = ThumbnailType.MINIATURE
51 // Only save the file URL if it is a remote playlist
52 const fileUrl = playlist.isOwned()
56 const thumbnailCreator = () => {
57 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
60 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
63 function updateVideoMiniatureFromUrl (options: {
65 video: MVideoThumbnail
69 const { downloadUrl, video, type, size } = options
70 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
72 // Only save the file URL if it is a remote video
73 const fileUrl = video.isOwned()
77 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
79 // Do not change the thumbnail filename if the file did not change
80 const filename = thumbnailUrlChanged
82 : existingThumbnail.filename
84 const thumbnailCreator = () => {
85 if (thumbnailUrlChanged) {
86 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
89 return Promise.resolve()
92 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
95 function updateVideoMiniatureFromExisting (options: {
97 video: MVideoThumbnail
99 automaticallyGenerated: boolean
101 keepOriginal?: boolean // default to false
103 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
105 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
107 const thumbnailCreator = () => {
108 return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
111 return updateThumbnailFromFunction({
117 automaticallyGenerated,
122 function generateVideoMiniature (options: {
123 video: MVideoThumbnail
124 videoFile: MVideoFile
127 const { video, videoFile, type } = options
129 return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
130 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
132 const thumbnailCreator = videoFile.isAudio()
133 ? () => processImageFromWorker({
134 path: ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
135 destination: outputPath,
136 newSize: { width, height },
139 : () => generateImageFromVideoFile({
143 size: { height, width }
146 return updateThumbnailFromFunction({
152 automaticallyGenerated: true,
158 function updatePlaceholderThumbnail (options: {
160 video: MVideoThumbnail
164 const { fileUrl, video, type, size } = options
165 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
167 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
169 const thumbnail = existingThumbnail || new ThumbnailModel()
171 // Do not change the thumbnail filename if the file did not change
172 const filename = thumbnailUrlChanged
174 : existingThumbnail.filename
176 thumbnail.filename = filename
177 thumbnail.height = height
178 thumbnail.width = width
179 thumbnail.type = type
180 thumbnail.fileUrl = fileUrl
185 // ---------------------------------------------------------------------------
188 generateVideoMiniature,
189 updateVideoMiniatureFromUrl,
190 updateVideoMiniatureFromExisting,
191 updatePlaceholderThumbnail,
192 updatePlaylistMiniatureFromUrl,
193 updatePlaylistMiniatureFromExisting
196 function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
197 const existingUrl = existingThumbnail
198 ? existingThumbnail.fileUrl
201 // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
202 return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
205 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
206 const filename = playlist.generateThumbnailName()
207 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
212 existingThumbnail: playlist.Thumbnail,
213 outputPath: join(basePath, filename),
214 height: size ? size.height : THUMBNAILS_SIZE.height,
215 width: size ? size.width : THUMBNAILS_SIZE.width
219 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
220 const existingThumbnail = Array.isArray(video.Thumbnails)
221 ? video.Thumbnails.find(t => t.type === type)
224 if (type === ThumbnailType.MINIATURE) {
225 const filename = generateImageFilename()
226 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
232 outputPath: join(basePath, filename),
233 height: size ? size.height : THUMBNAILS_SIZE.height,
234 width: size ? size.width : THUMBNAILS_SIZE.width
238 if (type === ThumbnailType.PREVIEW) {
239 const filename = generateImageFilename()
240 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
246 outputPath: join(basePath, filename),
247 height: size ? size.height : PREVIEWS_SIZE.height,
248 width: size ? size.width : PREVIEWS_SIZE.width
255 async function updateThumbnailFromFunction (parameters: {
256 thumbnailCreator: () => Promise<any>
261 automaticallyGenerated?: boolean
263 existingThumbnail?: MThumbnail
272 automaticallyGenerated = null,
276 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
277 ? existingThumbnail.filename
280 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
282 thumbnail.filename = filename
283 thumbnail.height = height
284 thumbnail.width = width
285 thumbnail.type = type
286 thumbnail.fileUrl = fileUrl
287 thumbnail.automaticallyGenerated = automaticallyGenerated
289 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
291 await thumbnailCreator()