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 '../typings/models/video/video-playlist'
10 import { MVideoFile, MVideoThumbnail } from '../typings/models'
11 import { MThumbnail } from '../typings/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 (
47 video: MVideoThumbnail,
49 automaticallyGenerated: boolean,
52 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
53 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height })
55 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
58 function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
59 const input = getVideoFilePath(video, videoFile)
61 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
62 const thumbnailCreator = videoFile.isAudio()
63 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
64 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
66 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
69 function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
70 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
72 const thumbnail = existingThumbnail || new ThumbnailModel()
74 thumbnail.filename = filename
75 thumbnail.height = height
76 thumbnail.width = width
78 thumbnail.fileUrl = fileUrl
83 // ---------------------------------------------------------------------------
86 generateVideoMiniature,
87 createVideoMiniatureFromUrl,
88 createVideoMiniatureFromExisting,
89 createPlaceholderThumbnail,
90 createPlaylistMiniatureFromUrl,
91 createPlaylistMiniatureFromExisting
94 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
95 const filename = playlist.generateThumbnailName()
96 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
101 existingThumbnail: playlist.Thumbnail,
102 outputPath: join(basePath, filename),
103 height: size ? size.height : THUMBNAILS_SIZE.height,
104 width: size ? size.width : THUMBNAILS_SIZE.width
108 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
109 const existingThumbnail = Array.isArray(video.Thumbnails)
110 ? video.Thumbnails.find(t => t.type === type)
113 if (type === ThumbnailType.MINIATURE) {
114 const filename = video.generateThumbnailName()
115 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
121 outputPath: join(basePath, filename),
122 height: size ? size.height : THUMBNAILS_SIZE.height,
123 width: size ? size.width : THUMBNAILS_SIZE.width
127 if (type === ThumbnailType.PREVIEW) {
128 const filename = video.generatePreviewName()
129 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
135 outputPath: join(basePath, filename),
136 height: size ? size.height : PREVIEWS_SIZE.height,
137 width: size ? size.width : PREVIEWS_SIZE.width
144 async function createThumbnailFromFunction (parameters: {
145 thumbnailCreator: () => Promise<any>
150 automaticallyGenerated?: boolean
152 existingThumbnail?: MThumbnail
154 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
156 const thumbnail = existingThumbnail || new ThumbnailModel()
158 thumbnail.filename = filename
159 thumbnail.height = height
160 thumbnail.width = width
161 thumbnail.type = type
162 thumbnail.fileUrl = fileUrl
163 thumbnail.automaticallyGenerated = automaticallyGenerated
165 await thumbnailCreator()