]>
Commit | Line | Data |
---|---|---|
e8bafea3 C |
1 | import { VideoFileModel } from '../models/video/video-file' |
2 | import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils' | |
3 | import { CONFIG } from '../initializers/config' | |
536598cf | 4 | import { PREVIEWS_SIZE, THUMBNAILS_SIZE, ASSETS_PATH } from '../initializers/constants' |
e8bafea3 C |
5 | import { VideoModel } from '../models/video/video' |
6 | import { ThumbnailModel } from '../models/video/thumbnail' | |
7 | import { ThumbnailType } from '../../shared/models/videos/thumbnail.type' | |
8 | import { processImage } from '../helpers/image-utils' | |
9 | import { join } from 'path' | |
10 | import { downloadImage } from '../helpers/requests' | |
11 | import { VideoPlaylistModel } from '../models/video/video-playlist' | |
12 | ||
13 | type ImageSize = { height: number, width: number } | |
14 | ||
3acc5084 | 15 | function createPlaylistMiniatureFromExisting (inputPath: string, playlist: VideoPlaylistModel, keepOriginal = false, size?: ImageSize) { |
e8bafea3 | 16 | const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size) |
3acc5084 | 17 | const type = ThumbnailType.MINIATURE |
e8bafea3 | 18 | |
2fb5b3a5 | 19 | const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) |
e8bafea3 C |
20 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail }) |
21 | } | |
22 | ||
9cc8d43e | 23 | function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: VideoPlaylistModel, size?: ImageSize) { |
e8bafea3 | 24 | const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size) |
3acc5084 | 25 | const type = ThumbnailType.MINIATURE |
e8bafea3 | 26 | |
9cc8d43e C |
27 | const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height }) |
28 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl }) | |
e8bafea3 C |
29 | } |
30 | ||
9cc8d43e | 31 | function createVideoMiniatureFromUrl (fileUrl: string, video: VideoModel, type: ThumbnailType, size?: ImageSize) { |
e8bafea3 | 32 | const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
9cc8d43e | 33 | const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height }) |
e8bafea3 | 34 | |
9cc8d43e | 35 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl }) |
e8bafea3 C |
36 | } |
37 | ||
3acc5084 | 38 | function createVideoMiniatureFromExisting (inputPath: string, video: VideoModel, type: ThumbnailType, size?: ImageSize) { |
e8bafea3 | 39 | const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
2fb5b3a5 | 40 | const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }) |
e8bafea3 C |
41 | |
42 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail }) | |
43 | } | |
44 | ||
3acc5084 | 45 | function generateVideoMiniature (video: VideoModel, videoFile: VideoFileModel, type: ThumbnailType) { |
e8bafea3 C |
46 | const input = video.getVideoFilePath(videoFile) |
47 | ||
536598cf C |
48 | const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type) |
49 | const thumbnailCreator = videoFile.isAudio() | |
50 | ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true) | |
51 | : () => generateImageFromVideoFile(input, basePath, filename, { height, width }) | |
e8bafea3 C |
52 | |
53 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail }) | |
54 | } | |
55 | ||
9cc8d43e | 56 | function createPlaceholderThumbnail (fileUrl: string, video: VideoModel, type: ThumbnailType, size: ImageSize) { |
e8bafea3 C |
57 | const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
58 | ||
59 | const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel() | |
60 | ||
61 | thumbnail.filename = filename | |
62 | thumbnail.height = height | |
63 | thumbnail.width = width | |
64 | thumbnail.type = type | |
9cc8d43e | 65 | thumbnail.fileUrl = fileUrl |
e8bafea3 C |
66 | |
67 | return thumbnail | |
68 | } | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | export { | |
3acc5084 C |
73 | generateVideoMiniature, |
74 | createVideoMiniatureFromUrl, | |
75 | createVideoMiniatureFromExisting, | |
e8bafea3 | 76 | createPlaceholderThumbnail, |
3acc5084 C |
77 | createPlaylistMiniatureFromUrl, |
78 | createPlaylistMiniatureFromExisting | |
e8bafea3 C |
79 | } |
80 | ||
81 | function buildMetadataFromPlaylist (playlist: VideoPlaylistModel, size: ImageSize) { | |
82 | const filename = playlist.generateThumbnailName() | |
83 | const basePath = CONFIG.STORAGE.THUMBNAILS_DIR | |
84 | ||
85 | return { | |
86 | filename, | |
87 | basePath, | |
88 | existingThumbnail: playlist.Thumbnail, | |
89 | outputPath: join(basePath, filename), | |
90 | height: size ? size.height : THUMBNAILS_SIZE.height, | |
91 | width: size ? size.width : THUMBNAILS_SIZE.width | |
92 | } | |
93 | } | |
94 | ||
95 | function buildMetadataFromVideo (video: VideoModel, type: ThumbnailType, size?: ImageSize) { | |
96 | const existingThumbnail = Array.isArray(video.Thumbnails) | |
97 | ? video.Thumbnails.find(t => t.type === type) | |
98 | : undefined | |
99 | ||
3acc5084 | 100 | if (type === ThumbnailType.MINIATURE) { |
e8bafea3 C |
101 | const filename = video.generateThumbnailName() |
102 | const basePath = CONFIG.STORAGE.THUMBNAILS_DIR | |
103 | ||
104 | return { | |
105 | filename, | |
106 | basePath, | |
107 | existingThumbnail, | |
108 | outputPath: join(basePath, filename), | |
109 | height: size ? size.height : THUMBNAILS_SIZE.height, | |
110 | width: size ? size.width : THUMBNAILS_SIZE.width | |
111 | } | |
112 | } | |
113 | ||
114 | if (type === ThumbnailType.PREVIEW) { | |
115 | const filename = video.generatePreviewName() | |
116 | const basePath = CONFIG.STORAGE.PREVIEWS_DIR | |
117 | ||
118 | return { | |
119 | filename, | |
120 | basePath, | |
121 | existingThumbnail, | |
122 | outputPath: join(basePath, filename), | |
123 | height: size ? size.height : PREVIEWS_SIZE.height, | |
124 | width: size ? size.width : PREVIEWS_SIZE.width | |
125 | } | |
126 | } | |
127 | ||
128 | return undefined | |
129 | } | |
130 | ||
131 | async function createThumbnailFromFunction (parameters: { | |
132 | thumbnailCreator: () => Promise<any>, | |
133 | filename: string, | |
134 | height: number, | |
135 | width: number, | |
136 | type: ThumbnailType, | |
9cc8d43e | 137 | fileUrl?: string, |
e8bafea3 C |
138 | existingThumbnail?: ThumbnailModel |
139 | }) { | |
9cc8d43e | 140 | const { thumbnailCreator, filename, width, height, type, existingThumbnail, fileUrl = null } = parameters |
e8bafea3 C |
141 | |
142 | const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel() | |
143 | ||
144 | thumbnail.filename = filename | |
145 | thumbnail.height = height | |
146 | thumbnail.width = width | |
147 | thumbnail.type = type | |
9cc8d43e | 148 | thumbnail.fileUrl = fileUrl |
e8bafea3 C |
149 | |
150 | await thumbnailCreator() | |
151 | ||
152 | return thumbnail | |
153 | } |