]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/thumbnail.ts
49317df28ca0dad66d68cd75dd965d635c55be5a
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
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'
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: updatedFilename, 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
81 // Do not change the thumbnail filename if the file did not change
82 const filename = thumbnailUrlChanged
83 ? updatedFilename
84 : existingThumbnail.filename
85
86 const thumbnailCreator = () => {
87 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
88
89 return Promise.resolve()
90 }
91
92 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
93 }
94
95 function createVideoMiniatureFromExisting (options: {
96 inputPath: string
97 video: MVideoThumbnail
98 type: ThumbnailType
99 automaticallyGenerated: boolean
100 size?: ImageSize
101 keepOriginal?: boolean // default to false
102 }) {
103 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
104
105 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
106 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
107
108 return createThumbnailFromFunction({
109 thumbnailCreator,
110 filename,
111 height,
112 width,
113 type,
114 automaticallyGenerated,
115 existingThumbnail
116 })
117 }
118
119 function generateVideoMiniature (options: {
120 video: MVideoThumbnail
121 videoFile: MVideoFile
122 type: ThumbnailType
123 }) {
124 const { video, videoFile, type } = options
125
126 const input = getVideoFilePath(video, videoFile)
127
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 })
132
133 return createThumbnailFromFunction({
134 thumbnailCreator,
135 filename,
136 height,
137 width,
138 type,
139 automaticallyGenerated: true,
140 existingThumbnail
141 })
142 }
143
144 function createPlaceholderThumbnail (options: {
145 fileUrl: string
146 video: MVideoThumbnail
147 type: ThumbnailType
148 size: ImageSize
149 }) {
150 const { fileUrl, video, type, size } = options
151 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
152
153 const thumbnail = existingThumbnail || new ThumbnailModel()
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 buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
176 const filename = playlist.generateThumbnailName()
177 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
178
179 return {
180 filename,
181 basePath,
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
186 }
187 }
188
189 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
190 const existingThumbnail = Array.isArray(video.Thumbnails)
191 ? video.Thumbnails.find(t => t.type === type)
192 : undefined
193
194 if (type === ThumbnailType.MINIATURE) {
195 const filename = video.generateThumbnailName()
196 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
197
198 return {
199 filename,
200 basePath,
201 existingThumbnail,
202 outputPath: join(basePath, filename),
203 height: size ? size.height : THUMBNAILS_SIZE.height,
204 width: size ? size.width : THUMBNAILS_SIZE.width
205 }
206 }
207
208 if (type === ThumbnailType.PREVIEW) {
209 const filename = video.generatePreviewName()
210 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
211
212 return {
213 filename,
214 basePath,
215 existingThumbnail,
216 outputPath: join(basePath, filename),
217 height: size ? size.height : PREVIEWS_SIZE.height,
218 width: size ? size.width : PREVIEWS_SIZE.width
219 }
220 }
221
222 return undefined
223 }
224
225 async function createThumbnailFromFunction (parameters: {
226 thumbnailCreator: () => Promise<any>
227 filename: string
228 height: number
229 width: number
230 type: ThumbnailType
231 automaticallyGenerated?: boolean
232 fileUrl?: string
233 existingThumbnail?: MThumbnail
234 }) {
235 const {
236 thumbnailCreator,
237 filename,
238 width,
239 height,
240 type,
241 existingThumbnail,
242 automaticallyGenerated = null,
243 fileUrl = null
244 } = parameters
245
246 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
247 ? existingThumbnail.filename
248 : undefined
249
250 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
251
252 thumbnail.filename = filename
253 thumbnail.height = height
254 thumbnail.width = width
255 thumbnail.type = type
256 thumbnail.fileUrl = fileUrl
257 thumbnail.automaticallyGenerated = automaticallyGenerated
258
259 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
260
261 await thumbnailCreator()
262
263 return thumbnail
264 }