]>
Commit | Line | Data |
---|---|---|
1664bc60 | 1 | import { copy } from 'fs-extra' |
a35a2279 | 2 | import { join } from 'path' |
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 | |
e8bafea3 | 65 | const { filename, 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 | ||
77 | // If the thumbnail URL did not change and has a unique filename (introduced in 3.2), avoid thumbnail processing | |
78 | const thumbnailUrlChanged = !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`) | |
79 | const thumbnailCreator = () => { | |
80 | if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height }) | |
81 | ||
82 | return copy(existingThumbnail.getPath(), ThumbnailModel.buildPath(type, filename)) | |
83 | } | |
84 | ||
9cc8d43e | 85 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl }) |
e8bafea3 C |
86 | } |
87 | ||
1ef65f4c C |
88 | function createVideoMiniatureFromExisting (options: { |
89 | inputPath: string | |
90 | video: MVideoThumbnail | |
91 | type: ThumbnailType | |
92 | automaticallyGenerated: boolean | |
65af03a2 | 93 | size?: ImageSize |
a35a2279 | 94 | keepOriginal?: boolean // default to false |
1ef65f4c | 95 | }) { |
a35a2279 | 96 | const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options |
1ef65f4c | 97 | |
e8bafea3 | 98 | const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
1ef65f4c | 99 | const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) |
e8bafea3 | 100 | |
a35a2279 C |
101 | return createThumbnailFromFunction({ |
102 | thumbnailCreator, | |
103 | filename, | |
104 | height, | |
105 | width, | |
106 | type, | |
107 | automaticallyGenerated, | |
108 | existingThumbnail | |
109 | }) | |
e8bafea3 C |
110 | } |
111 | ||
a35a2279 C |
112 | function generateVideoMiniature (options: { |
113 | video: MVideoThumbnail | |
114 | videoFile: MVideoFile | |
115 | type: ThumbnailType | |
116 | }) { | |
117 | const { video, videoFile, type } = options | |
118 | ||
d7a25329 | 119 | const input = getVideoFilePath(video, videoFile) |
e8bafea3 | 120 | |
536598cf C |
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 }) | |
e8bafea3 | 125 | |
a35a2279 C |
126 | return createThumbnailFromFunction({ |
127 | thumbnailCreator, | |
128 | filename, | |
129 | height, | |
130 | width, | |
131 | type, | |
132 | automaticallyGenerated: true, | |
133 | existingThumbnail | |
134 | }) | |
e8bafea3 C |
135 | } |
136 | ||
a35a2279 C |
137 | function createPlaceholderThumbnail (options: { |
138 | fileUrl: string | |
139 | video: MVideoThumbnail | |
140 | type: ThumbnailType | |
141 | size: ImageSize | |
142 | }) { | |
143 | const { fileUrl, video, type, size } = options | |
e8bafea3 C |
144 | const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
145 | ||
a1587156 | 146 | const thumbnail = existingThumbnail || new ThumbnailModel() |
e8bafea3 C |
147 | |
148 | thumbnail.filename = filename | |
149 | thumbnail.height = height | |
150 | thumbnail.width = width | |
151 | thumbnail.type = type | |
9cc8d43e | 152 | thumbnail.fileUrl = fileUrl |
e8bafea3 C |
153 | |
154 | return thumbnail | |
155 | } | |
156 | ||
157 | // --------------------------------------------------------------------------- | |
158 | ||
159 | export { | |
3acc5084 C |
160 | generateVideoMiniature, |
161 | createVideoMiniatureFromUrl, | |
162 | createVideoMiniatureFromExisting, | |
e8bafea3 | 163 | createPlaceholderThumbnail, |
3acc5084 C |
164 | createPlaylistMiniatureFromUrl, |
165 | createPlaylistMiniatureFromExisting | |
e8bafea3 C |
166 | } |
167 | ||
453e83ea | 168 | function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) { |
e8bafea3 C |
169 | const filename = playlist.generateThumbnailName() |
170 | const basePath = CONFIG.STORAGE.THUMBNAILS_DIR | |
171 | ||
172 | return { | |
173 | filename, | |
174 | basePath, | |
175 | existingThumbnail: playlist.Thumbnail, | |
176 | outputPath: join(basePath, filename), | |
177 | height: size ? size.height : THUMBNAILS_SIZE.height, | |
178 | width: size ? size.width : THUMBNAILS_SIZE.width | |
179 | } | |
180 | } | |
181 | ||
453e83ea | 182 | function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) { |
e8bafea3 C |
183 | const existingThumbnail = Array.isArray(video.Thumbnails) |
184 | ? video.Thumbnails.find(t => t.type === type) | |
185 | : undefined | |
186 | ||
3acc5084 | 187 | if (type === ThumbnailType.MINIATURE) { |
e8bafea3 C |
188 | const filename = video.generateThumbnailName() |
189 | const basePath = CONFIG.STORAGE.THUMBNAILS_DIR | |
190 | ||
191 | return { | |
192 | filename, | |
193 | basePath, | |
194 | existingThumbnail, | |
195 | outputPath: join(basePath, filename), | |
196 | height: size ? size.height : THUMBNAILS_SIZE.height, | |
197 | width: size ? size.width : THUMBNAILS_SIZE.width | |
198 | } | |
199 | } | |
200 | ||
201 | if (type === ThumbnailType.PREVIEW) { | |
202 | const filename = video.generatePreviewName() | |
203 | const basePath = CONFIG.STORAGE.PREVIEWS_DIR | |
204 | ||
205 | return { | |
206 | filename, | |
207 | basePath, | |
208 | existingThumbnail, | |
209 | outputPath: join(basePath, filename), | |
210 | height: size ? size.height : PREVIEWS_SIZE.height, | |
211 | width: size ? size.width : PREVIEWS_SIZE.width | |
212 | } | |
213 | } | |
214 | ||
215 | return undefined | |
216 | } | |
217 | ||
218 | async function createThumbnailFromFunction (parameters: { | |
a1587156 C |
219 | thumbnailCreator: () => Promise<any> |
220 | filename: string | |
221 | height: number | |
222 | width: number | |
223 | type: ThumbnailType | |
224 | automaticallyGenerated?: boolean | |
225 | fileUrl?: string | |
453e83ea | 226 | existingThumbnail?: MThumbnail |
e8bafea3 | 227 | }) { |
a35a2279 C |
228 | const { |
229 | thumbnailCreator, | |
230 | filename, | |
231 | width, | |
232 | height, | |
233 | type, | |
234 | existingThumbnail, | |
235 | automaticallyGenerated = null, | |
236 | fileUrl = null | |
237 | } = parameters | |
238 | ||
239 | const oldFilename = existingThumbnail | |
240 | ? existingThumbnail.filename | |
241 | : undefined | |
6302d599 | 242 | |
a35a2279 | 243 | const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel() |
e8bafea3 C |
244 | |
245 | thumbnail.filename = filename | |
246 | thumbnail.height = height | |
247 | thumbnail.width = width | |
248 | thumbnail.type = type | |
9cc8d43e | 249 | thumbnail.fileUrl = fileUrl |
65af03a2 | 250 | thumbnail.automaticallyGenerated = automaticallyGenerated |
a35a2279 | 251 | thumbnail.previousThumbnailFilename = oldFilename |
e8bafea3 C |
252 | |
253 | await thumbnailCreator() | |
254 | ||
255 | return thumbnail | |
256 | } |