]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/thumbnail.ts
Optimize remote thumbnail processing
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
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'
15
16 type ImageSize = { height: number, width: number }
17
18 function createPlaylistMiniatureFromExisting (options: {
19 inputPath: string
20 playlist: MVideoPlaylistThumbnail
21 automaticallyGenerated: boolean
22 keepOriginal?: boolean // default to false
23 size?: ImageSize
24 }) {
25 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
26 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
27 const type = ThumbnailType.MINIATURE
28
29 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
30 return createThumbnailFromFunction({
31 thumbnailCreator,
32 filename,
33 height,
34 width,
35 type,
36 automaticallyGenerated,
37 existingThumbnail
38 })
39 }
40
41 function createPlaylistMiniatureFromUrl (options: {
42 downloadUrl: string
43 playlist: MVideoPlaylistThumbnail
44 size?: ImageSize
45 }) {
46 const { downloadUrl, playlist, size } = options
47 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
48 const type = ThumbnailType.MINIATURE
49
50 // Only save the file URL if it is a remote playlist
51 const fileUrl = playlist.isOwned()
52 ? null
53 : downloadUrl
54
55 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
56 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
57 }
58
59 function createVideoMiniatureFromUrl (options: {
60 downloadUrl: string
61 video: MVideoThumbnail
62 type: ThumbnailType
63 size?: ImageSize
64 }) {
65 const { downloadUrl, video, type, size } = options
66 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
67
68 // Only save the file URL if it is a remote video
69 const fileUrl = video.isOwned()
70 ? null
71 : downloadUrl
72
73 // If the thumbnail URL did not change
74 const existingUrl = existingThumbnail
75 ? existingThumbnail.fileUrl
76 : null
77
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 })
82
83 return copy(existingThumbnail.getPath(), ThumbnailModel.buildPath(type, filename))
84 }
85
86 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
87 }
88
89 function createVideoMiniatureFromExisting (options: {
90 inputPath: string
91 video: MVideoThumbnail
92 type: ThumbnailType
93 automaticallyGenerated: boolean
94 size?: ImageSize
95 keepOriginal?: boolean // default to false
96 }) {
97 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
98
99 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
100 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
101
102 return createThumbnailFromFunction({
103 thumbnailCreator,
104 filename,
105 height,
106 width,
107 type,
108 automaticallyGenerated,
109 existingThumbnail
110 })
111 }
112
113 function generateVideoMiniature (options: {
114 video: MVideoThumbnail
115 videoFile: MVideoFile
116 type: ThumbnailType
117 }) {
118 const { video, videoFile, type } = options
119
120 const input = getVideoFilePath(video, videoFile)
121
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 })
126
127 return createThumbnailFromFunction({
128 thumbnailCreator,
129 filename,
130 height,
131 width,
132 type,
133 automaticallyGenerated: true,
134 existingThumbnail
135 })
136 }
137
138 function createPlaceholderThumbnail (options: {
139 fileUrl: string
140 video: MVideoThumbnail
141 type: ThumbnailType
142 size: ImageSize
143 }) {
144 const { fileUrl, video, type, size } = options
145 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
146
147 const thumbnail = existingThumbnail || new ThumbnailModel()
148
149 thumbnail.filename = filename
150 thumbnail.height = height
151 thumbnail.width = width
152 thumbnail.type = type
153 thumbnail.fileUrl = fileUrl
154
155 return thumbnail
156 }
157
158 // ---------------------------------------------------------------------------
159
160 export {
161 generateVideoMiniature,
162 createVideoMiniatureFromUrl,
163 createVideoMiniatureFromExisting,
164 createPlaceholderThumbnail,
165 createPlaylistMiniatureFromUrl,
166 createPlaylistMiniatureFromExisting
167 }
168
169 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
170 const filename = playlist.generateThumbnailName()
171 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
172
173 return {
174 filename,
175 basePath,
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
180 }
181 }
182
183 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
184 const existingThumbnail = Array.isArray(video.Thumbnails)
185 ? video.Thumbnails.find(t => t.type === type)
186 : undefined
187
188 if (type === ThumbnailType.MINIATURE) {
189 const filename = video.generateThumbnailName()
190 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
191
192 return {
193 filename,
194 basePath,
195 existingThumbnail,
196 outputPath: join(basePath, filename),
197 height: size ? size.height : THUMBNAILS_SIZE.height,
198 width: size ? size.width : THUMBNAILS_SIZE.width
199 }
200 }
201
202 if (type === ThumbnailType.PREVIEW) {
203 const filename = video.generatePreviewName()
204 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
205
206 return {
207 filename,
208 basePath,
209 existingThumbnail,
210 outputPath: join(basePath, filename),
211 height: size ? size.height : PREVIEWS_SIZE.height,
212 width: size ? size.width : PREVIEWS_SIZE.width
213 }
214 }
215
216 return undefined
217 }
218
219 async function createThumbnailFromFunction (parameters: {
220 thumbnailCreator: () => Promise<any>
221 filename: string
222 height: number
223 width: number
224 type: ThumbnailType
225 automaticallyGenerated?: boolean
226 fileUrl?: string
227 existingThumbnail?: MThumbnail
228 }) {
229 const {
230 thumbnailCreator,
231 filename,
232 width,
233 height,
234 type,
235 existingThumbnail,
236 automaticallyGenerated = null,
237 fileUrl = null
238 } = parameters
239
240 const oldFilename = existingThumbnail
241 ? existingThumbnail.filename
242 : undefined
243
244 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
245
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
253
254 await thumbnailCreator()
255
256 return thumbnail
257 }