1 import { copy } from 'fs-extra'
2 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, 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.2), avoid thumbnail processing
78 const thumbnailUrlChanged = !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
79 const thumbnailCreator = () => {
80 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
82 return copy(existingThumbnail.getPath(), ThumbnailModel.buildPath(type, filename))
85 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
88 function createVideoMiniatureFromExisting (options: {
90 video: MVideoThumbnail
92 automaticallyGenerated: boolean
94 keepOriginal?: boolean // default to false
96 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
98 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
99 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
101 return createThumbnailFromFunction({
107 automaticallyGenerated,
112 function generateVideoMiniature (options: {
113 video: MVideoThumbnail
114 videoFile: MVideoFile
117 const { video, videoFile, type } = options
119 const input = getVideoFilePath(video, videoFile)
121 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
122 const thumbnailCreator = videoFile.isAudio()
123 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
124 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
126 return createThumbnailFromFunction({
132 automaticallyGenerated: true,
137 function createPlaceholderThumbnail (options: {
139 video: MVideoThumbnail
143 const { fileUrl, video, type, size } = options
144 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
146 const thumbnail = existingThumbnail || new ThumbnailModel()
148 thumbnail.filename = filename
149 thumbnail.height = height
150 thumbnail.width = width
151 thumbnail.type = type
152 thumbnail.fileUrl = fileUrl
157 // ---------------------------------------------------------------------------
160 generateVideoMiniature,
161 createVideoMiniatureFromUrl,
162 createVideoMiniatureFromExisting,
163 createPlaceholderThumbnail,
164 createPlaylistMiniatureFromUrl,
165 createPlaylistMiniatureFromExisting
168 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
169 const filename = playlist.generateThumbnailName()
170 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
175 existingThumbnail: playlist.Thumbnail,
176 outputPath: join(basePath, filename),
177 height: size ? size.height : THUMBNAILS_SIZE.height,
178 width: size ? size.width : THUMBNAILS_SIZE.width
182 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
183 const existingThumbnail = Array.isArray(video.Thumbnails)
184 ? video.Thumbnails.find(t => t.type === type)
187 if (type === ThumbnailType.MINIATURE) {
188 const filename = video.generateThumbnailName()
189 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
195 outputPath: join(basePath, filename),
196 height: size ? size.height : THUMBNAILS_SIZE.height,
197 width: size ? size.width : THUMBNAILS_SIZE.width
201 if (type === ThumbnailType.PREVIEW) {
202 const filename = video.generatePreviewName()
203 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
209 outputPath: join(basePath, filename),
210 height: size ? size.height : PREVIEWS_SIZE.height,
211 width: size ? size.width : PREVIEWS_SIZE.width
218 async function createThumbnailFromFunction (parameters: {
219 thumbnailCreator: () => Promise<any>
224 automaticallyGenerated?: boolean
226 existingThumbnail?: MThumbnail
235 automaticallyGenerated = null,
239 const oldFilename = existingThumbnail
240 ? existingThumbnail.filename
243 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
245 thumbnail.filename = filename
246 thumbnail.height = height
247 thumbnail.width = width
248 thumbnail.type = type
249 thumbnail.fileUrl = fileUrl
250 thumbnail.automaticallyGenerated = automaticallyGenerated
251 thumbnail.previousThumbnailFilename = oldFilename
253 await thumbnailCreator()