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