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