1 import { join } from 'path'
3 import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
4 import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
5 import { processImage } from '../helpers/image-utils'
6 import { downloadImage } from '../helpers/requests'
7 import { CONFIG } from '../initializers/config'
8 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
9 import { ThumbnailModel } from '../models/video/thumbnail'
10 import { MVideoFile, MVideoThumbnail } from '../types/models'
11 import { MThumbnail } from '../types/models/video/thumbnail'
12 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
13 import { getVideoFilePath } from './video-paths'
15 type ImageSize = { height: number, width: number }
17 function createPlaylistMiniatureFromExisting (options: {
19 playlist: MVideoPlaylistThumbnail
20 automaticallyGenerated: boolean
21 keepOriginal?: boolean // default to false
24 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
25 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
26 const type = ThumbnailType.MINIATURE
28 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
29 return createThumbnailFromFunction({
35 automaticallyGenerated,
40 function createPlaylistMiniatureFromUrl (options: {
42 playlist: MVideoPlaylistThumbnail
45 const { downloadUrl, playlist, size } = options
46 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
47 const type = ThumbnailType.MINIATURE
49 // Only save the file URL if it is a remote playlist
50 const fileUrl = playlist.isOwned()
54 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
55 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
58 function createVideoMiniatureFromUrl (options: {
60 video: MVideoThumbnail
64 const { downloadUrl, video, type, size } = options
65 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
67 // Only save the file URL if it is a remote video
68 const fileUrl = video.isOwned()
72 // If the thumbnail URL did not change
73 const existingUrl = existingThumbnail
74 ? existingThumbnail.fileUrl
77 // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
78 const thumbnailUrlChanged = !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
80 // Do not change the thumbnail filename if the file did not change
81 const filename = thumbnailUrlChanged
83 : existingThumbnail.filename
85 const thumbnailCreator = () => {
86 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
88 return Promise.resolve()
91 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
94 function createVideoMiniatureFromExisting (options: {
96 video: MVideoThumbnail
98 automaticallyGenerated: boolean
100 keepOriginal?: boolean // default to false
102 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
104 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
105 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
107 return createThumbnailFromFunction({
113 automaticallyGenerated,
118 function generateVideoMiniature (options: {
119 video: MVideoThumbnail
120 videoFile: MVideoFile
123 const { video, videoFile, type } = options
125 const input = getVideoFilePath(video, videoFile)
127 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
128 const thumbnailCreator = videoFile.isAudio()
129 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
130 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
132 return createThumbnailFromFunction({
138 automaticallyGenerated: true,
143 function createPlaceholderThumbnail (options: {
145 video: MVideoThumbnail
149 const { fileUrl, video, type, size } = options
150 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
152 const thumbnail = existingThumbnail || new ThumbnailModel()
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 buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
175 const filename = playlist.generateThumbnailName()
176 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
181 existingThumbnail: playlist.Thumbnail,
182 outputPath: join(basePath, filename),
183 height: size ? size.height : THUMBNAILS_SIZE.height,
184 width: size ? size.width : THUMBNAILS_SIZE.width
188 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
189 const existingThumbnail = Array.isArray(video.Thumbnails)
190 ? video.Thumbnails.find(t => t.type === type)
193 if (type === ThumbnailType.MINIATURE) {
194 const filename = video.generateThumbnailName()
195 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
201 outputPath: join(basePath, filename),
202 height: size ? size.height : THUMBNAILS_SIZE.height,
203 width: size ? size.width : THUMBNAILS_SIZE.width
207 if (type === ThumbnailType.PREVIEW) {
208 const filename = video.generatePreviewName()
209 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
215 outputPath: join(basePath, filename),
216 height: size ? size.height : PREVIEWS_SIZE.height,
217 width: size ? size.width : PREVIEWS_SIZE.width
224 async function createThumbnailFromFunction (parameters: {
225 thumbnailCreator: () => Promise<any>
230 automaticallyGenerated?: boolean
232 existingThumbnail?: MThumbnail
241 automaticallyGenerated = null,
245 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
246 ? existingThumbnail.filename
249 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
251 thumbnail.filename = filename
252 thumbnail.height = height
253 thumbnail.width = width
254 thumbnail.type = type
255 thumbnail.fileUrl = fileUrl
256 thumbnail.automaticallyGenerated = automaticallyGenerated
258 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
260 await thumbnailCreator()