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'
14 type ImageSize = { height: number, width: number }
16 function createPlaylistMiniatureFromExisting (
18 playlist: MVideoPlaylistThumbnail,
19 automaticallyGenerated: boolean,
23 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
24 const type = ThumbnailType.MINIATURE
26 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
27 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
30 function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
31 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
32 const type = ThumbnailType.MINIATURE
34 const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
35 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
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 })
42 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
45 function createVideoMiniatureFromExisting (options: {
47 video: MVideoThumbnail
49 automaticallyGenerated: boolean
51 keepOriginal?: boolean
53 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal } = options
55 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
56 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
58 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
61 function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
62 const input = getVideoFilePath(video, videoFile)
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 })
69 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
72 function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
73 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
75 const thumbnail = existingThumbnail || new ThumbnailModel()
77 thumbnail.filename = filename
78 thumbnail.height = height
79 thumbnail.width = width
81 thumbnail.fileUrl = fileUrl
86 // ---------------------------------------------------------------------------
89 generateVideoMiniature,
90 createVideoMiniatureFromUrl,
91 createVideoMiniatureFromExisting,
92 createPlaceholderThumbnail,
93 createPlaylistMiniatureFromUrl,
94 createPlaylistMiniatureFromExisting
97 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
98 const filename = playlist.generateThumbnailName()
99 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
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
111 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
112 const existingThumbnail = Array.isArray(video.Thumbnails)
113 ? video.Thumbnails.find(t => t.type === type)
116 if (type === ThumbnailType.MINIATURE) {
117 const filename = video.generateThumbnailName()
118 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
124 outputPath: join(basePath, filename),
125 height: size ? size.height : THUMBNAILS_SIZE.height,
126 width: size ? size.width : THUMBNAILS_SIZE.width
130 if (type === ThumbnailType.PREVIEW) {
131 const filename = video.generatePreviewName()
132 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
138 outputPath: join(basePath, filename),
139 height: size ? size.height : PREVIEWS_SIZE.height,
140 width: size ? size.width : PREVIEWS_SIZE.width
147 async function createThumbnailFromFunction (parameters: {
148 thumbnailCreator: () => Promise<any>
153 automaticallyGenerated?: boolean
155 existingThumbnail?: MThumbnail
157 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
159 const thumbnail = existingThumbnail || new ThumbnailModel()
161 thumbnail.filename = filename
162 thumbnail.height = height
163 thumbnail.width = width
164 thumbnail.type = type
165 thumbnail.fileUrl = fileUrl
166 thumbnail.automaticallyGenerated = automaticallyGenerated
168 await thumbnailCreator()