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 (downloadUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
31 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
32 const type = ThumbnailType.MINIATURE
34 // Only save the file URL if it is a remote playlist
35 const fileUrl = playlist.isOwned()
39 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
40 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
43 function createVideoMiniatureFromUrl (downloadUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
46 // Only save the file URL if it is a remote video
47 const fileUrl = video.isOwned()
51 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
52 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
55 function createVideoMiniatureFromExisting (options: {
57 video: MVideoThumbnail
59 automaticallyGenerated: boolean
61 keepOriginal?: boolean
63 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal } = options
65 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
66 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
68 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
71 function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
72 const input = getVideoFilePath(video, videoFile)
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 })
79 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
82 function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
83 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
85 const thumbnail = existingThumbnail || new ThumbnailModel()
87 thumbnail.filename = filename
88 thumbnail.height = height
89 thumbnail.width = width
91 thumbnail.fileUrl = fileUrl
96 // ---------------------------------------------------------------------------
99 generateVideoMiniature,
100 createVideoMiniatureFromUrl,
101 createVideoMiniatureFromExisting,
102 createPlaceholderThumbnail,
103 createPlaylistMiniatureFromUrl,
104 createPlaylistMiniatureFromExisting
107 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
108 const filename = playlist.generateThumbnailName()
109 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
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
121 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
122 const existingThumbnail = Array.isArray(video.Thumbnails)
123 ? video.Thumbnails.find(t => t.type === type)
126 if (type === ThumbnailType.MINIATURE) {
127 const filename = video.generateThumbnailName()
128 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
134 outputPath: join(basePath, filename),
135 height: size ? size.height : THUMBNAILS_SIZE.height,
136 width: size ? size.width : THUMBNAILS_SIZE.width
140 if (type === ThumbnailType.PREVIEW) {
141 const filename = video.generatePreviewName()
142 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
148 outputPath: join(basePath, filename),
149 height: size ? size.height : PREVIEWS_SIZE.height,
150 width: size ? size.width : PREVIEWS_SIZE.width
157 async function createThumbnailFromFunction (parameters: {
158 thumbnailCreator: () => Promise<any>
163 automaticallyGenerated?: boolean
165 existingThumbnail?: MThumbnail
167 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
170 if (existingThumbnail) await existingThumbnail.removeThumbnail()
172 const thumbnail = existingThumbnail || new ThumbnailModel()
174 thumbnail.filename = filename
175 thumbnail.height = height
176 thumbnail.width = width
177 thumbnail.type = type
178 thumbnail.fileUrl = fileUrl
179 thumbnail.automaticallyGenerated = automaticallyGenerated
181 await thumbnailCreator()