1 import { copy } from 'fs-extra'
2 import { join } from 'path'
3 import { logger } from '@server/helpers/logger'
4 import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
5 import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
6 import { processImage } from '../helpers/image-utils'
7 import { downloadImage } from '../helpers/requests'
8 import { CONFIG } from '../initializers/config'
9 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
10 import { ThumbnailModel } from '../models/video/thumbnail'
11 import { MVideoFile, MVideoThumbnail } from '../types/models'
12 import { MThumbnail } from '../types/models/video/thumbnail'
13 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
14 import { getVideoFilePath } from './video-paths'
16 type ImageSize = { height: number, width: number }
18 function createPlaylistMiniatureFromExisting (options: {
20 playlist: MVideoPlaylistThumbnail
21 automaticallyGenerated: boolean
22 keepOriginal?: boolean // default to false
25 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
26 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
27 const type = ThumbnailType.MINIATURE
29 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
30 return createThumbnailFromFunction({
36 automaticallyGenerated,
41 function createPlaylistMiniatureFromUrl (options: {
43 playlist: MVideoPlaylistThumbnail
46 const { downloadUrl, playlist, size } = options
47 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
48 const type = ThumbnailType.MINIATURE
50 // Only save the file URL if it is a remote playlist
51 const fileUrl = playlist.isOwned()
55 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
56 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
59 function createVideoMiniatureFromUrl (options: {
61 video: MVideoThumbnail
65 const { downloadUrl, video, type, size } = options
66 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
68 // Only save the file URL if it is a remote video
69 const fileUrl = video.isOwned()
73 // If the thumbnail URL did not change
74 const existingUrl = existingThumbnail
75 ? existingThumbnail.fileUrl
78 // If the thumbnail URL did not change and has a unique filename (introduced in 3.2), avoid thumbnail processing
79 const thumbnailUrlChanged = !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
81 // Do not change the thumbnail filename if the file did not change
82 const filename = thumbnailUrlChanged
84 : existingThumbnail.filename
86 const thumbnailCreator = () => {
87 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
89 return Promise.resolve()
92 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
95 function createVideoMiniatureFromExisting (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)
106 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
108 return createThumbnailFromFunction({
114 automaticallyGenerated,
119 function generateVideoMiniature (options: {
120 video: MVideoThumbnail
121 videoFile: MVideoFile
124 const { video, videoFile, type } = options
126 const input = getVideoFilePath(video, videoFile)
128 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
129 const thumbnailCreator = videoFile.isAudio()
130 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
131 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
133 return createThumbnailFromFunction({
139 automaticallyGenerated: true,
144 function createPlaceholderThumbnail (options: {
146 video: MVideoThumbnail
150 const { fileUrl, video, type, size } = options
151 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
153 const thumbnail = existingThumbnail || new ThumbnailModel()
155 thumbnail.filename = filename
156 thumbnail.height = height
157 thumbnail.width = width
158 thumbnail.type = type
159 thumbnail.fileUrl = fileUrl
164 // ---------------------------------------------------------------------------
167 generateVideoMiniature,
168 createVideoMiniatureFromUrl,
169 createVideoMiniatureFromExisting,
170 createPlaceholderThumbnail,
171 createPlaylistMiniatureFromUrl,
172 createPlaylistMiniatureFromExisting
175 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
176 const filename = playlist.generateThumbnailName()
177 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
182 existingThumbnail: playlist.Thumbnail,
183 outputPath: join(basePath, filename),
184 height: size ? size.height : THUMBNAILS_SIZE.height,
185 width: size ? size.width : THUMBNAILS_SIZE.width
189 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
190 const existingThumbnail = Array.isArray(video.Thumbnails)
191 ? video.Thumbnails.find(t => t.type === type)
194 if (type === ThumbnailType.MINIATURE) {
195 const filename = video.generateThumbnailName()
196 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
202 outputPath: join(basePath, filename),
203 height: size ? size.height : THUMBNAILS_SIZE.height,
204 width: size ? size.width : THUMBNAILS_SIZE.width
208 if (type === ThumbnailType.PREVIEW) {
209 const filename = video.generatePreviewName()
210 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
216 outputPath: join(basePath, filename),
217 height: size ? size.height : PREVIEWS_SIZE.height,
218 width: size ? size.width : PREVIEWS_SIZE.width
225 async function createThumbnailFromFunction (parameters: {
226 thumbnailCreator: () => Promise<any>
231 automaticallyGenerated?: boolean
233 existingThumbnail?: MThumbnail
242 automaticallyGenerated = null,
246 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
247 ? existingThumbnail.filename
250 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
252 thumbnail.filename = filename
253 thumbnail.height = height
254 thumbnail.width = width
255 thumbnail.type = type
256 thumbnail.fileUrl = fileUrl
257 thumbnail.automaticallyGenerated = automaticallyGenerated
259 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
261 await thumbnailCreator()