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