]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Generate a name for caption files
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
CommitLineData
e8bafea3
C
1import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
2import { CONFIG } from '../initializers/config'
453e83ea 3import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
e8bafea3
C
4import { ThumbnailModel } from '../models/video/thumbnail'
5import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
6import { processImage } from '../helpers/image-utils'
7import { join } from 'path'
8import { downloadImage } from '../helpers/requests'
26d6bf65
C
9import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
10import { MVideoFile, MVideoThumbnail } from '../types/models'
11import { MThumbnail } from '../types/models/video/thumbnail'
d7a25329 12import { getVideoFilePath } from './video-paths'
e8bafea3
C
13
14type ImageSize = { height: number, width: number }
15
65af03a2
C
16function 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
a8b1b404 30function createPlaylistMiniatureFromUrl (downloadUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
e8bafea3 31 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 32 const type = ThumbnailType.MINIATURE
e8bafea3 33
a8b1b404
C
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 })
9cc8d43e 40 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
41}
42
a8b1b404 43function createVideoMiniatureFromUrl (downloadUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3 44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
e8bafea3 45
a8b1b404
C
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 })
9cc8d43e 52 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
53}
54
1ef65f4c
C
55function createVideoMiniatureFromExisting (options: {
56 inputPath: string
57 video: MVideoThumbnail
58 type: ThumbnailType
59 automaticallyGenerated: boolean
65af03a2 60 size?: ImageSize
1ef65f4c
C
61 keepOriginal?: boolean
62}) {
63 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal } = options
64
e8bafea3 65 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
1ef65f4c 66 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
e8bafea3 67
65af03a2 68 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
e8bafea3
C
69}
70
453e83ea 71function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
d7a25329 72 const input = getVideoFilePath(video, videoFile)
e8bafea3 73
536598cf
C
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 })
e8bafea3 78
65af03a2 79 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
e8bafea3
C
80}
81
453e83ea 82function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
e8bafea3
C
83 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
84
a1587156 85 const thumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3
C
86
87 thumbnail.filename = filename
88 thumbnail.height = height
89 thumbnail.width = width
90 thumbnail.type = type
9cc8d43e 91 thumbnail.fileUrl = fileUrl
e8bafea3
C
92
93 return thumbnail
94}
95
96// ---------------------------------------------------------------------------
97
98export {
3acc5084
C
99 generateVideoMiniature,
100 createVideoMiniatureFromUrl,
101 createVideoMiniatureFromExisting,
e8bafea3 102 createPlaceholderThumbnail,
3acc5084
C
103 createPlaylistMiniatureFromUrl,
104 createPlaylistMiniatureFromExisting
e8bafea3
C
105}
106
453e83ea 107function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
e8bafea3
C
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
453e83ea 121function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3
C
122 const existingThumbnail = Array.isArray(video.Thumbnails)
123 ? video.Thumbnails.find(t => t.type === type)
124 : undefined
125
3acc5084 126 if (type === ThumbnailType.MINIATURE) {
e8bafea3
C
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
157async function createThumbnailFromFunction (parameters: {
a1587156
C
158 thumbnailCreator: () => Promise<any>
159 filename: string
160 height: number
161 width: number
162 type: ThumbnailType
163 automaticallyGenerated?: boolean
164 fileUrl?: string
453e83ea 165 existingThumbnail?: MThumbnail
e8bafea3 166}) {
65af03a2 167 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
e8bafea3 168
6302d599
C
169 // Remove old file
170 if (existingThumbnail) await existingThumbnail.removeThumbnail()
171
a1587156 172 const thumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3
C
173
174 thumbnail.filename = filename
175 thumbnail.height = height
176 thumbnail.width = width
177 thumbnail.type = type
9cc8d43e 178 thumbnail.fileUrl = fileUrl
65af03a2 179 thumbnail.automaticallyGenerated = automaticallyGenerated
e8bafea3
C
180
181 await thumbnailCreator()
182
183 return thumbnail
184}