diff options
author | Kim <1877318+kimsible@users.noreply.github.com> | 2020-04-20 10:28:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-20 10:28:38 +0200 |
commit | b1770a0af464ad6350d156245b1abcc1395e142e (patch) | |
tree | 0712a2cd912ecd3f41084134d513920a6f723fb6 /server/controllers/api/videos | |
parent | 8f31261f77c6e521917b3f629b223ccc8df50960 (diff) | |
download | PeerTube-b1770a0af464ad6350d156245b1abcc1395e142e.tar.gz PeerTube-b1770a0af464ad6350d156245b1abcc1395e142e.tar.zst PeerTube-b1770a0af464ad6350d156245b1abcc1395e142e.zip |
Add thumbnail / preview generation from url on the fly (#2646)
* Add thumbnails generation on the fly to URL import
* Display generated preview to import first edit
* Use ternary to get type inference
* Move preview/thumbnail test just after import
Co-authored-by: kimsible <kimsible@users.noreply.github.com>
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r-- | server/controllers/api/videos/import.ts | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index f4630375e..fb2de5dc0 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts | |||
@@ -23,7 +23,7 @@ import { move, readFile } from 'fs-extra' | |||
23 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' | 23 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' |
24 | import { CONFIG } from '../../../initializers/config' | 24 | import { CONFIG } from '../../../initializers/config' |
25 | import { sequelizeTypescript } from '../../../initializers/database' | 25 | import { sequelizeTypescript } from '../../../initializers/database' |
26 | import { createVideoMiniatureFromExisting } from '../../../lib/thumbnail' | 26 | import { createVideoMiniatureFromExisting, createVideoMiniatureFromUrl } from '../../../lib/thumbnail' |
27 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' | 27 | import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' |
28 | import { | 28 | import { |
29 | MChannelAccountDefault, | 29 | MChannelAccountDefault, |
@@ -153,8 +153,25 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) | |||
153 | 153 | ||
154 | const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo) | 154 | const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo) |
155 | 155 | ||
156 | const thumbnailModel = await processThumbnail(req, video) | 156 | let thumbnailModel: MThumbnail |
157 | const previewModel = await processPreview(req, video) | 157 | |
158 | // Process video thumbnail from request.files | ||
159 | thumbnailModel = await processThumbnail(req, video) | ||
160 | |||
161 | // Process video thumbnail from url if processing from request.files failed | ||
162 | if (!thumbnailModel) { | ||
163 | thumbnailModel = await processThumbnailFromUrl(youtubeDLInfo.thumbnailUrl, video) | ||
164 | } | ||
165 | |||
166 | let previewModel: MThumbnail | ||
167 | |||
168 | // Process video preview from request.files | ||
169 | previewModel = await processPreview(req, video) | ||
170 | |||
171 | // Process video preview from url if processing from request.files failed | ||
172 | if (!previewModel) { | ||
173 | previewModel = await processPreviewFromUrl(youtubeDLInfo.thumbnailUrl, video) | ||
174 | } | ||
158 | 175 | ||
159 | const tags = body.tags || youtubeDLInfo.tags | 176 | const tags = body.tags || youtubeDLInfo.tags |
160 | const videoImportAttributes = { | 177 | const videoImportAttributes = { |
@@ -200,9 +217,8 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response) | |||
200 | const payload = { | 217 | const payload = { |
201 | type: 'youtube-dl' as 'youtube-dl', | 218 | type: 'youtube-dl' as 'youtube-dl', |
202 | videoImportId: videoImport.id, | 219 | videoImportId: videoImport.id, |
203 | thumbnailUrl: youtubeDLInfo.thumbnailUrl, | 220 | generateThumbnail: !thumbnailModel, |
204 | downloadThumbnail: !thumbnailModel, | 221 | generatePreview: !previewModel, |
205 | downloadPreview: !previewModel, | ||
206 | fileExt: youtubeDLInfo.fileExt | 222 | fileExt: youtubeDLInfo.fileExt |
207 | ? `.${youtubeDLInfo.fileExt}` | 223 | ? `.${youtubeDLInfo.fileExt}` |
208 | : '.mp4' | 224 | : '.mp4' |
@@ -261,6 +277,24 @@ async function processPreview (req: express.Request, video: VideoModel) { | |||
261 | return undefined | 277 | return undefined |
262 | } | 278 | } |
263 | 279 | ||
280 | async function processThumbnailFromUrl (url: string, video: VideoModel) { | ||
281 | try { | ||
282 | return createVideoMiniatureFromUrl(url, video, ThumbnailType.MINIATURE) | ||
283 | } catch (err) { | ||
284 | logger.warn('Cannot generate video thumbnail %s for %s.', url, video.url, { err }) | ||
285 | return undefined | ||
286 | } | ||
287 | } | ||
288 | |||
289 | async function processPreviewFromUrl (url: string, video: VideoModel) { | ||
290 | try { | ||
291 | return createVideoMiniatureFromUrl(url, video, ThumbnailType.PREVIEW) | ||
292 | } catch (err) { | ||
293 | logger.warn('Cannot generate video preview %s for %s.', url, video.url, { err }) | ||
294 | return undefined | ||
295 | } | ||
296 | } | ||
297 | |||
264 | function insertIntoDB (parameters: { | 298 | function insertIntoDB (parameters: { |
265 | video: MVideoThumbnailAccountDefault | 299 | video: MVideoThumbnailAccountDefault |
266 | thumbnailModel: MThumbnail | 300 | thumbnailModel: MThumbnail |