1 import { join } from 'path'
2 import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
3 import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
4 import { processImage } from '../helpers/image-utils'
5 import { downloadImage } from '../helpers/requests'
6 import { CONFIG } from '../initializers/config'
7 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
8 import { ThumbnailModel } from '../models/video/thumbnail'
9 import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
10 import { MThumbnail } from '../types/models/video/thumbnail'
11 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
12 import { getVideoFilePath } from './video-paths'
14 type ImageSize = { height: number, width: number }
16 function createPlaylistMiniatureFromExisting (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 = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
28 return createThumbnailFromFunction({
34 automaticallyGenerated,
39 function createPlaylistMiniatureFromUrl (options: {
41 playlist: MVideoPlaylistThumbnail
44 const { downloadUrl, playlist, size } = options
45 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
46 const type = ThumbnailType.MINIATURE
48 // Only save the file URL if it is a remote playlist
49 const fileUrl = playlist.isOwned()
53 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
54 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
57 function createVideoMiniatureFromUrl (options: {
59 video: MVideoThumbnail
63 const { downloadUrl, video, type, size } = options
64 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
66 // Only save the file URL if it is a remote video
67 const fileUrl = video.isOwned()
71 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
73 // Do not change the thumbnail filename if the file did not change
74 const filename = thumbnailUrlChanged
76 : existingThumbnail.filename
78 const thumbnailCreator = () => {
79 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
81 return Promise.resolve()
84 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
87 function createVideoMiniatureFromExisting (options: {
89 video: MVideoThumbnail
91 automaticallyGenerated: boolean
93 keepOriginal?: boolean // default to false
95 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
97 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
98 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
100 return createThumbnailFromFunction({
106 automaticallyGenerated,
111 function generateVideoMiniature (options: {
112 video: MVideoThumbnail
113 videoFile: MVideoFile
116 const { video, videoFile, type } = options
118 const input = getVideoFilePath(video, videoFile)
120 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
121 const thumbnailCreator = videoFile.isAudio()
122 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
123 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
125 return createThumbnailFromFunction({
131 automaticallyGenerated: true,
136 function createPlaceholderThumbnail (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 createVideoMiniatureFromUrl,
168 createVideoMiniatureFromExisting,
169 createPlaceholderThumbnail,
170 createPlaylistMiniatureFromUrl,
171 createPlaylistMiniatureFromExisting
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 = video.generateThumbnailName()
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 = video.generatePreviewName()
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 createThumbnailFromFunction (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()