thumbnail.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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'
  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) {
  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. types: thumbnailsToGenerate
  202. })
  203. for (const model of models) {
  204. await video.addAndSaveThumbnail(model)
  205. }
  206. }
  207. // ---------------------------------------------------------------------------
  208. export {
  209. generateLocalVideoMiniature,
  210. regenerateMiniaturesIfNeeded,
  211. updateLocalVideoMiniatureFromUrl,
  212. updateLocalVideoMiniatureFromExisting,
  213. updateRemoteVideoThumbnail,
  214. updateRemotePlaylistMiniatureFromUrl,
  215. updateLocalPlaylistMiniatureFromExisting
  216. }
  217. // ---------------------------------------------------------------------------
  218. // Private
  219. // ---------------------------------------------------------------------------
  220. function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
  221. const existingUrl = existingThumbnail
  222. ? existingThumbnail.fileUrl
  223. : null
  224. // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
  225. return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
  226. }
  227. function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
  228. const filename = playlist.generateThumbnailName()
  229. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  230. return {
  231. filename,
  232. basePath,
  233. existingThumbnail: playlist.Thumbnail,
  234. outputPath: join(basePath, filename),
  235. height: size ? size.height : THUMBNAILS_SIZE.height,
  236. width: size ? size.width : THUMBNAILS_SIZE.width
  237. }
  238. }
  239. function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType_Type, size?: ImageSize) {
  240. const existingThumbnail = Array.isArray(video.Thumbnails)
  241. ? video.Thumbnails.find(t => t.type === type)
  242. : undefined
  243. if (type === ThumbnailType.MINIATURE) {
  244. const filename = generateImageFilename()
  245. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  246. return {
  247. type,
  248. filename,
  249. basePath,
  250. existingThumbnail,
  251. outputPath: join(basePath, filename),
  252. height: size ? size.height : THUMBNAILS_SIZE.height,
  253. width: size ? size.width : THUMBNAILS_SIZE.width
  254. }
  255. }
  256. if (type === ThumbnailType.PREVIEW) {
  257. const filename = generateImageFilename()
  258. const basePath = CONFIG.STORAGE.PREVIEWS_DIR
  259. return {
  260. type,
  261. filename,
  262. basePath,
  263. existingThumbnail,
  264. outputPath: join(basePath, filename),
  265. height: size ? size.height : PREVIEWS_SIZE.height,
  266. width: size ? size.width : PREVIEWS_SIZE.width
  267. }
  268. }
  269. return undefined
  270. }
  271. async function updateThumbnailFromFunction (parameters: {
  272. thumbnailCreator: () => Promise<any>
  273. filename: string
  274. height: number
  275. width: number
  276. type: ThumbnailType_Type
  277. onDisk: boolean
  278. automaticallyGenerated?: boolean
  279. fileUrl?: string
  280. existingThumbnail?: MThumbnail
  281. }) {
  282. const {
  283. thumbnailCreator,
  284. filename,
  285. width,
  286. height,
  287. type,
  288. existingThumbnail,
  289. onDisk,
  290. automaticallyGenerated = null,
  291. fileUrl = null
  292. } = parameters
  293. const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
  294. ? existingThumbnail.filename
  295. : undefined
  296. const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
  297. thumbnail.filename = filename
  298. thumbnail.height = height
  299. thumbnail.width = width
  300. thumbnail.type = type
  301. thumbnail.fileUrl = fileUrl
  302. thumbnail.automaticallyGenerated = automaticallyGenerated
  303. thumbnail.onDisk = onDisk
  304. if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
  305. await thumbnailCreator()
  306. return thumbnail
  307. }
  308. async function generateImageFromVideoFile (options: {
  309. fromPath: string
  310. folder: string
  311. imageName: string
  312. size: { width: number, height: number }
  313. ffprobe?: FfprobeData
  314. }) {
  315. const { fromPath, folder, imageName, size, ffprobe } = options
  316. const pendingImageName = 'pending-' + imageName
  317. const pendingImagePath = join(folder, pendingImageName)
  318. try {
  319. await generateThumbnailFromVideo({ fromPath, output: pendingImagePath, ffprobe })
  320. const destination = join(folder, imageName)
  321. await processImageFromWorker({ path: pendingImagePath, destination, newSize: size })
  322. return destination
  323. } catch (err) {
  324. logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() })
  325. try {
  326. await remove(pendingImagePath)
  327. } catch (err) {
  328. logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() })
  329. }
  330. throw err
  331. }
  332. }