thumbnail.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { join } from 'path'
  2. import { ThumbnailType, ThumbnailType_Type } from '@peertube/peertube-models'
  3. import { generateImageFilename } from '../helpers/image-utils.js'
  4. import { CONFIG } from '../initializers/config.js'
  5. import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants.js'
  6. import { ThumbnailModel } from '../models/video/thumbnail.js'
  7. import { MVideoFile, MVideoThumbnail, MVideoUUID, MVideoWithAllFiles } from '../types/models/index.js'
  8. import { MThumbnail } from '../types/models/video/thumbnail.js'
  9. import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist.js'
  10. import { VideoPathManager } from './video-path-manager.js'
  11. import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process.js'
  12. import { generateThumbnailFromVideo } from '@server/helpers/ffmpeg/ffmpeg-image.js'
  13. import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
  14. import { remove } from 'fs-extra/esm'
  15. import { FfprobeData } from 'fluent-ffmpeg'
  16. import Bluebird from 'bluebird'
  17. const lTags = loggerTagsFactory('thumbnail')
  18. type ImageSize = { height?: number, width?: number }
  19. function updateLocalPlaylistMiniatureFromExisting (options: {
  20. inputPath: string
  21. playlist: MVideoPlaylistThumbnail
  22. automaticallyGenerated: boolean
  23. keepOriginal?: boolean // default to false
  24. size?: ImageSize
  25. }) {
  26. const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
  27. const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
  28. const type = ThumbnailType.MINIATURE
  29. const thumbnailCreator = () => {
  30. return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
  31. }
  32. return updateThumbnailFromFunction({
  33. thumbnailCreator,
  34. filename,
  35. height,
  36. width,
  37. type,
  38. automaticallyGenerated,
  39. onDisk: true,
  40. existingThumbnail
  41. })
  42. }
  43. function updateRemotePlaylistMiniatureFromUrl (options: {
  44. downloadUrl: string
  45. playlist: MVideoPlaylistThumbnail
  46. size?: ImageSize
  47. }) {
  48. const { downloadUrl, playlist, size } = options
  49. const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
  50. const type = ThumbnailType.MINIATURE
  51. // Only save the file URL if it is a remote playlist
  52. const fileUrl = playlist.isOwned()
  53. ? null
  54. : downloadUrl
  55. const thumbnailCreator = () => {
  56. return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
  57. }
  58. return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl, onDisk: true })
  59. }
  60. function updateLocalVideoMiniatureFromExisting (options: {
  61. inputPath: string
  62. video: MVideoThumbnail
  63. type: ThumbnailType_Type
  64. automaticallyGenerated: boolean
  65. size?: ImageSize
  66. keepOriginal?: boolean // default to false
  67. }) {
  68. const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
  69. const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  70. const thumbnailCreator = () => {
  71. return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
  72. }
  73. return updateThumbnailFromFunction({
  74. thumbnailCreator,
  75. filename,
  76. height,
  77. width,
  78. type,
  79. automaticallyGenerated,
  80. existingThumbnail,
  81. onDisk: true
  82. })
  83. }
  84. // Returns thumbnail models sorted by their size (height) in descendent order (biggest first)
  85. function generateLocalVideoMiniature (options: {
  86. video: MVideoThumbnail
  87. videoFile: MVideoFile
  88. types: ThumbnailType_Type[]
  89. ffprobe: FfprobeData
  90. }): Promise<MThumbnail[]> {
  91. const { video, videoFile, types, ffprobe } = options
  92. if (types.length === 0) return Promise.resolve([])
  93. return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
  94. // Get bigger images to generate first
  95. const metadatas = types.map(type => buildMetadataFromVideo(video, type))
  96. .sort((a, b) => {
  97. if (a.height < b.height) return 1
  98. if (a.height === b.height) return 0
  99. return -1
  100. })
  101. let biggestImagePath: string
  102. return Bluebird.mapSeries(metadatas, metadata => {
  103. const { filename, basePath, height, width, existingThumbnail, outputPath, type } = metadata
  104. let thumbnailCreator: () => Promise<any>
  105. if (videoFile.isAudio()) {
  106. thumbnailCreator = () => processImageFromWorker({
  107. path: ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
  108. destination: outputPath,
  109. newSize: { width, height },
  110. keepOriginal: true
  111. })
  112. } else if (biggestImagePath) {
  113. thumbnailCreator = () => processImageFromWorker({
  114. path: biggestImagePath,
  115. destination: outputPath,
  116. newSize: { width, height },
  117. keepOriginal: true
  118. })
  119. } else {
  120. thumbnailCreator = () => generateImageFromVideoFile({
  121. fromPath: input,
  122. folder: basePath,
  123. imageName: filename,
  124. size: { height, width },
  125. ffprobe
  126. })
  127. }
  128. if (!biggestImagePath) biggestImagePath = outputPath
  129. return updateThumbnailFromFunction({
  130. thumbnailCreator,
  131. filename,
  132. height,
  133. width,
  134. type,
  135. automaticallyGenerated: true,
  136. onDisk: true,
  137. existingThumbnail
  138. })
  139. })
  140. })
  141. }
  142. // ---------------------------------------------------------------------------
  143. function updateLocalVideoMiniatureFromUrl (options: {
  144. downloadUrl: string
  145. video: MVideoThumbnail
  146. type: ThumbnailType_Type
  147. size?: ImageSize
  148. }) {
  149. const { downloadUrl, video, type, size } = options
  150. const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  151. // Only save the file URL if it is a remote video
  152. const fileUrl = video.isOwned()
  153. ? null
  154. : downloadUrl
  155. const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
  156. // Do not change the thumbnail filename if the file did not change
  157. const filename = thumbnailUrlChanged
  158. ? updatedFilename
  159. : existingThumbnail.filename
  160. const thumbnailCreator = () => {
  161. if (thumbnailUrlChanged) {
  162. return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
  163. }
  164. return Promise.resolve()
  165. }
  166. return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl, onDisk: true })
  167. }
  168. function updateRemoteVideoThumbnail (options: {
  169. fileUrl: string
  170. video: MVideoThumbnail
  171. type: ThumbnailType_Type
  172. size: ImageSize
  173. onDisk: boolean
  174. }) {
  175. const { fileUrl, video, type, size, onDisk } = options
  176. const { filename: generatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  177. const thumbnail = existingThumbnail || new ThumbnailModel()
  178. // Do not change the thumbnail filename if the file did not change
  179. if (hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)) {
  180. thumbnail.filename = generatedFilename
  181. }
  182. thumbnail.height = height
  183. thumbnail.width = width
  184. thumbnail.type = type
  185. thumbnail.fileUrl = fileUrl
  186. thumbnail.onDisk = onDisk
  187. return thumbnail
  188. }
  189. // ---------------------------------------------------------------------------
  190. async function regenerateMiniaturesIfNeeded (video: MVideoWithAllFiles, ffprobe: FfprobeData) {
  191. const thumbnailsToGenerate: ThumbnailType_Type[] = []
  192. if (video.getMiniature().automaticallyGenerated === true) {
  193. thumbnailsToGenerate.push(ThumbnailType.MINIATURE)
  194. }
  195. if (video.getPreview().automaticallyGenerated === true) {
  196. thumbnailsToGenerate.push(ThumbnailType.PREVIEW)
  197. }
  198. const models = await generateLocalVideoMiniature({
  199. video,
  200. videoFile: video.getMaxQualityFile(),
  201. ffprobe,
  202. types: thumbnailsToGenerate
  203. })
  204. for (const model of models) {
  205. await video.addAndSaveThumbnail(model)
  206. }
  207. }
  208. // ---------------------------------------------------------------------------
  209. export {
  210. generateLocalVideoMiniature,
  211. regenerateMiniaturesIfNeeded,
  212. updateLocalVideoMiniatureFromUrl,
  213. updateLocalVideoMiniatureFromExisting,
  214. updateRemoteVideoThumbnail,
  215. updateRemotePlaylistMiniatureFromUrl,
  216. updateLocalPlaylistMiniatureFromExisting
  217. }
  218. // ---------------------------------------------------------------------------
  219. // Private
  220. // ---------------------------------------------------------------------------
  221. function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
  222. const existingUrl = existingThumbnail
  223. ? existingThumbnail.fileUrl
  224. : null
  225. // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
  226. return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
  227. }
  228. function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
  229. const filename = playlist.generateThumbnailName()
  230. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  231. return {
  232. filename,
  233. basePath,
  234. existingThumbnail: playlist.Thumbnail,
  235. outputPath: join(basePath, filename),
  236. height: size ? size.height : THUMBNAILS_SIZE.height,
  237. width: size ? size.width : THUMBNAILS_SIZE.width
  238. }
  239. }
  240. function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType_Type, size?: ImageSize) {
  241. const existingThumbnail = Array.isArray(video.Thumbnails)
  242. ? video.Thumbnails.find(t => t.type === type)
  243. : undefined
  244. if (type === ThumbnailType.MINIATURE) {
  245. const filename = generateImageFilename()
  246. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  247. return {
  248. type,
  249. filename,
  250. basePath,
  251. existingThumbnail,
  252. outputPath: join(basePath, filename),
  253. height: size ? size.height : THUMBNAILS_SIZE.height,
  254. width: size ? size.width : THUMBNAILS_SIZE.width
  255. }
  256. }
  257. if (type === ThumbnailType.PREVIEW) {
  258. const filename = generateImageFilename()
  259. const basePath = CONFIG.STORAGE.PREVIEWS_DIR
  260. return {
  261. type,
  262. filename,
  263. basePath,
  264. existingThumbnail,
  265. outputPath: join(basePath, filename),
  266. height: size ? size.height : PREVIEWS_SIZE.height,
  267. width: size ? size.width : PREVIEWS_SIZE.width
  268. }
  269. }
  270. return undefined
  271. }
  272. async function updateThumbnailFromFunction (parameters: {
  273. thumbnailCreator: () => Promise<any>
  274. filename: string
  275. height: number
  276. width: number
  277. type: ThumbnailType_Type
  278. onDisk: boolean
  279. automaticallyGenerated?: boolean
  280. fileUrl?: string
  281. existingThumbnail?: MThumbnail
  282. }) {
  283. const {
  284. thumbnailCreator,
  285. filename,
  286. width,
  287. height,
  288. type,
  289. existingThumbnail,
  290. onDisk,
  291. automaticallyGenerated = null,
  292. fileUrl = null
  293. } = parameters
  294. const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
  295. ? existingThumbnail.filename
  296. : undefined
  297. const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
  298. thumbnail.filename = filename
  299. thumbnail.height = height
  300. thumbnail.width = width
  301. thumbnail.type = type
  302. thumbnail.fileUrl = fileUrl
  303. thumbnail.automaticallyGenerated = automaticallyGenerated
  304. thumbnail.onDisk = onDisk
  305. if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
  306. await thumbnailCreator()
  307. return thumbnail
  308. }
  309. async function generateImageFromVideoFile (options: {
  310. fromPath: string
  311. folder: string
  312. imageName: string
  313. size: { width: number, height: number }
  314. ffprobe?: FfprobeData
  315. }) {
  316. const { fromPath, folder, imageName, size, ffprobe } = options
  317. const pendingImageName = 'pending-' + imageName
  318. const pendingImagePath = join(folder, pendingImageName)
  319. try {
  320. const framesToAnalyze = CONFIG.THUMBNAILS.GENERATION_FROM_VIDEO.FRAMES_TO_ANALYZE
  321. await generateThumbnailFromVideo({ fromPath, output: pendingImagePath, framesToAnalyze, ffprobe, scale: size })
  322. const destination = join(folder, imageName)
  323. await processImageFromWorker({ path: pendingImagePath, destination, newSize: size })
  324. return destination
  325. } catch (err) {
  326. logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
  327. try {
  328. await remove(pendingImagePath)
  329. } catch (err) {
  330. logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
  331. }
  332. throw err
  333. }
  334. }