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