]>
Commit | Line | Data |
---|---|---|
e8bafea3 C |
1 | import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils' |
2 | import { CONFIG } from '../initializers/config' | |
453e83ea | 3 | import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants' |
e8bafea3 C |
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' | |
26d6bf65 C |
9 | import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist' |
10 | import { MVideoFile, MVideoThumbnail } from '../types/models' | |
11 | import { MThumbnail } from '../types/models/video/thumbnail' | |
d7a25329 | 12 | import { getVideoFilePath } from './video-paths' |
e8bafea3 C |
13 | |
14 | type ImageSize = { height: number, width: number } | |
15 | ||
65af03a2 C |
16 | function createPlaylistMiniatureFromExisting ( |
17 | inputPath: string, | |
453e83ea | 18 | playlist: MVideoPlaylistThumbnail, |
65af03a2 C |
19 | automaticallyGenerated: boolean, |
20 | keepOriginal = false, | |
21 | size?: ImageSize | |
22 | ) { | |
e8bafea3 | 23 | const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size) |
3acc5084 | 24 | const type = ThumbnailType.MINIATURE |
e8bafea3 | 25 | |
2fb5b3a5 | 26 | const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) |
65af03a2 | 27 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail }) |
e8bafea3 C |
28 | } |
29 | ||
453e83ea | 30 | function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) { |
e8bafea3 | 31 | const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size) |
3acc5084 | 32 | const type = ThumbnailType.MINIATURE |
e8bafea3 | 33 | |
9cc8d43e C |
34 | const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height }) |
35 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl }) | |
e8bafea3 C |
36 | } |
37 | ||
453e83ea | 38 | function createVideoMiniatureFromUrl (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) { |
e8bafea3 | 39 | const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
9cc8d43e | 40 | const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height }) |
e8bafea3 | 41 | |
9cc8d43e | 42 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl }) |
e8bafea3 C |
43 | } |
44 | ||
1ef65f4c C |
45 | function createVideoMiniatureFromExisting (options: { |
46 | inputPath: string | |
47 | video: MVideoThumbnail | |
48 | type: ThumbnailType | |
49 | automaticallyGenerated: boolean | |
65af03a2 | 50 | size?: ImageSize |
1ef65f4c C |
51 | keepOriginal?: boolean |
52 | }) { | |
53 | const { inputPath, video, type, automaticallyGenerated, size, keepOriginal } = options | |
54 | ||
e8bafea3 | 55 | const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
1ef65f4c | 56 | const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) |
e8bafea3 | 57 | |
65af03a2 | 58 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail }) |
e8bafea3 C |
59 | } |
60 | ||
453e83ea | 61 | function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) { |
d7a25329 | 62 | const input = getVideoFilePath(video, videoFile) |
e8bafea3 | 63 | |
536598cf C |
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 }) | |
e8bafea3 | 68 | |
65af03a2 | 69 | return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail }) |
e8bafea3 C |
70 | } |
71 | ||
453e83ea | 72 | function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) { |
e8bafea3 C |
73 | const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) |
74 | ||
a1587156 | 75 | const thumbnail = existingThumbnail || new ThumbnailModel() |
e8bafea3 C |
76 | |
77 | thumbnail.filename = filename | |
78 | thumbnail.height = height | |
79 | thumbnail.width = width | |
80 | thumbnail.type = type | |
9cc8d43e | 81 | thumbnail.fileUrl = fileUrl |
e8bafea3 C |
82 | |
83 | return thumbnail | |
84 | } | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | ||
88 | export { | |
3acc5084 C |
89 | generateVideoMiniature, |
90 | createVideoMiniatureFromUrl, | |
91 | createVideoMiniatureFromExisting, | |
e8bafea3 | 92 | createPlaceholderThumbnail, |
3acc5084 C |
93 | createPlaylistMiniatureFromUrl, |
94 | createPlaylistMiniatureFromExisting | |
e8bafea3 C |
95 | } |
96 | ||
453e83ea | 97 | function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) { |
e8bafea3 C |
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 | ||
453e83ea | 111 | function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) { |
e8bafea3 C |
112 | const existingThumbnail = Array.isArray(video.Thumbnails) |
113 | ? video.Thumbnails.find(t => t.type === type) | |
114 | : undefined | |
115 | ||
3acc5084 | 116 | if (type === ThumbnailType.MINIATURE) { |
e8bafea3 C |
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: { | |
a1587156 C |
148 | thumbnailCreator: () => Promise<any> |
149 | filename: string | |
150 | height: number | |
151 | width: number | |
152 | type: ThumbnailType | |
153 | automaticallyGenerated?: boolean | |
154 | fileUrl?: string | |
453e83ea | 155 | existingThumbnail?: MThumbnail |
e8bafea3 | 156 | }) { |
65af03a2 | 157 | const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters |
e8bafea3 | 158 | |
a1587156 | 159 | const thumbnail = existingThumbnail || new ThumbnailModel() |
e8bafea3 C |
160 | |
161 | thumbnail.filename = filename | |
162 | thumbnail.height = height | |
163 | thumbnail.width = width | |
164 | thumbnail.type = type | |
9cc8d43e | 165 | thumbnail.fileUrl = fileUrl |
65af03a2 | 166 | thumbnail.automaticallyGenerated = automaticallyGenerated |
e8bafea3 C |
167 | |
168 | await thumbnailCreator() | |
169 | ||
170 | return thumbnail | |
171 | } |