]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/thumbnail.ts
Generate a name for caption files
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
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 (downloadUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
31 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
32 const type = ThumbnailType.MINIATURE
33
34 // Only save the file URL if it is a remote playlist
35 const fileUrl = playlist.isOwned()
36 ? null
37 : downloadUrl
38
39 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
40 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
41 }
42
43 function createVideoMiniatureFromUrl (downloadUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
45
46 // Only save the file URL if it is a remote video
47 const fileUrl = video.isOwned()
48 ? null
49 : downloadUrl
50
51 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
52 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
53 }
54
55 function createVideoMiniatureFromExisting (options: {
56 inputPath: string
57 video: MVideoThumbnail
58 type: ThumbnailType
59 automaticallyGenerated: boolean
60 size?: ImageSize
61 keepOriginal?: boolean
62 }) {
63 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal } = options
64
65 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
66 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
67
68 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
69 }
70
71 function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
72 const input = getVideoFilePath(video, videoFile)
73
74 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
75 const thumbnailCreator = videoFile.isAudio()
76 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
77 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
78
79 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
80 }
81
82 function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
83 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
84
85 const thumbnail = existingThumbnail || new ThumbnailModel()
86
87 thumbnail.filename = filename
88 thumbnail.height = height
89 thumbnail.width = width
90 thumbnail.type = type
91 thumbnail.fileUrl = fileUrl
92
93 return thumbnail
94 }
95
96 // ---------------------------------------------------------------------------
97
98 export {
99 generateVideoMiniature,
100 createVideoMiniatureFromUrl,
101 createVideoMiniatureFromExisting,
102 createPlaceholderThumbnail,
103 createPlaylistMiniatureFromUrl,
104 createPlaylistMiniatureFromExisting
105 }
106
107 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
108 const filename = playlist.generateThumbnailName()
109 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
110
111 return {
112 filename,
113 basePath,
114 existingThumbnail: playlist.Thumbnail,
115 outputPath: join(basePath, filename),
116 height: size ? size.height : THUMBNAILS_SIZE.height,
117 width: size ? size.width : THUMBNAILS_SIZE.width
118 }
119 }
120
121 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
122 const existingThumbnail = Array.isArray(video.Thumbnails)
123 ? video.Thumbnails.find(t => t.type === type)
124 : undefined
125
126 if (type === ThumbnailType.MINIATURE) {
127 const filename = video.generateThumbnailName()
128 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
129
130 return {
131 filename,
132 basePath,
133 existingThumbnail,
134 outputPath: join(basePath, filename),
135 height: size ? size.height : THUMBNAILS_SIZE.height,
136 width: size ? size.width : THUMBNAILS_SIZE.width
137 }
138 }
139
140 if (type === ThumbnailType.PREVIEW) {
141 const filename = video.generatePreviewName()
142 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
143
144 return {
145 filename,
146 basePath,
147 existingThumbnail,
148 outputPath: join(basePath, filename),
149 height: size ? size.height : PREVIEWS_SIZE.height,
150 width: size ? size.width : PREVIEWS_SIZE.width
151 }
152 }
153
154 return undefined
155 }
156
157 async function createThumbnailFromFunction (parameters: {
158 thumbnailCreator: () => Promise<any>
159 filename: string
160 height: number
161 width: number
162 type: ThumbnailType
163 automaticallyGenerated?: boolean
164 fileUrl?: string
165 existingThumbnail?: MThumbnail
166 }) {
167 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
168
169 // Remove old file
170 if (existingThumbnail) await existingThumbnail.removeThumbnail()
171
172 const thumbnail = existingThumbnail || new ThumbnailModel()
173
174 thumbnail.filename = filename
175 thumbnail.height = height
176 thumbnail.width = width
177 thumbnail.type = type
178 thumbnail.fileUrl = fileUrl
179 thumbnail.automaticallyGenerated = automaticallyGenerated
180
181 await thumbnailCreator()
182
183 return thumbnail
184 }