1 import chaiJsonSchema = require('chai-json-schema')
2 import { copy, move } from 'fs-extra'
3 import { join } from 'path'
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, 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`)
80 const thumbnailCreator = () => {
81 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
83 return copy(existingThumbnail.getPath(), ThumbnailModel.buildPath(type, filename))
86 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
89 function createVideoMiniatureFromExisting (options: {
91 video: MVideoThumbnail
93 automaticallyGenerated: boolean
95 keepOriginal?: boolean // default to false
97 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
99 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
100 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
102 return createThumbnailFromFunction({
108 automaticallyGenerated,
113 function generateVideoMiniature (options: {
114 video: MVideoThumbnail
115 videoFile: MVideoFile
118 const { video, videoFile, type } = options
120 const input = getVideoFilePath(video, videoFile)
122 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
123 const thumbnailCreator = videoFile.isAudio()
124 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
125 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
127 return createThumbnailFromFunction({
133 automaticallyGenerated: true,
138 function createPlaceholderThumbnail (options: {
140 video: MVideoThumbnail
144 const { fileUrl, video, type, size } = options
145 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
147 const thumbnail = existingThumbnail || new ThumbnailModel()
149 thumbnail.filename = filename
150 thumbnail.height = height
151 thumbnail.width = width
152 thumbnail.type = type
153 thumbnail.fileUrl = fileUrl
158 // ---------------------------------------------------------------------------
161 generateVideoMiniature,
162 createVideoMiniatureFromUrl,
163 createVideoMiniatureFromExisting,
164 createPlaceholderThumbnail,
165 createPlaylistMiniatureFromUrl,
166 createPlaylistMiniatureFromExisting
169 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
170 const filename = playlist.generateThumbnailName()
171 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
176 existingThumbnail: playlist.Thumbnail,
177 outputPath: join(basePath, filename),
178 height: size ? size.height : THUMBNAILS_SIZE.height,
179 width: size ? size.width : THUMBNAILS_SIZE.width
183 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
184 const existingThumbnail = Array.isArray(video.Thumbnails)
185 ? video.Thumbnails.find(t => t.type === type)
188 if (type === ThumbnailType.MINIATURE) {
189 const filename = video.generateThumbnailName()
190 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
196 outputPath: join(basePath, filename),
197 height: size ? size.height : THUMBNAILS_SIZE.height,
198 width: size ? size.width : THUMBNAILS_SIZE.width
202 if (type === ThumbnailType.PREVIEW) {
203 const filename = video.generatePreviewName()
204 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
210 outputPath: join(basePath, filename),
211 height: size ? size.height : PREVIEWS_SIZE.height,
212 width: size ? size.width : PREVIEWS_SIZE.width
219 async function createThumbnailFromFunction (parameters: {
220 thumbnailCreator: () => Promise<any>
225 automaticallyGenerated?: boolean
227 existingThumbnail?: MThumbnail
236 automaticallyGenerated = null,
240 const oldFilename = existingThumbnail
241 ? existingThumbnail.filename
244 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
246 thumbnail.filename = filename
247 thumbnail.height = height
248 thumbnail.width = width
249 thumbnail.type = type
250 thumbnail.fileUrl = fileUrl
251 thumbnail.automaticallyGenerated = automaticallyGenerated
252 thumbnail.previousThumbnailFilename = oldFilename
254 await thumbnailCreator()