thumbnail.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { VideoFileModel } from '../models/video/video-file'
  2. import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
  3. import { CONFIG } from '../initializers/config'
  4. import { PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
  5. import { VideoModel } from '../models/video/video'
  6. import { ThumbnailModel } from '../models/video/thumbnail'
  7. import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
  8. import { processImage } from '../helpers/image-utils'
  9. import { join } from 'path'
  10. import { downloadImage } from '../helpers/requests'
  11. import { VideoPlaylistModel } from '../models/video/video-playlist'
  12. type ImageSize = { height: number, width: number }
  13. function createPlaylistMiniatureFromExisting (inputPath: string, playlist: VideoPlaylistModel, keepOriginal = false, size?: ImageSize) {
  14. const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
  15. const type = ThumbnailType.MINIATURE
  16. const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
  17. return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail })
  18. }
  19. function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: VideoPlaylistModel, size?: ImageSize) {
  20. const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
  21. const type = ThumbnailType.MINIATURE
  22. const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
  23. return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
  24. }
  25. function createVideoMiniatureFromUrl (fileUrl: string, video: VideoModel, type: ThumbnailType, size?: ImageSize) {
  26. const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  27. const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
  28. return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
  29. }
  30. function createVideoMiniatureFromExisting (inputPath: string, video: VideoModel, type: ThumbnailType, size?: ImageSize) {
  31. const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  32. const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height })
  33. return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail })
  34. }
  35. function generateVideoMiniature (video: VideoModel, videoFile: VideoFileModel, type: ThumbnailType) {
  36. const input = video.getVideoFilePath(videoFile)
  37. const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type)
  38. const thumbnailCreator = () => generateImageFromVideoFile(input, basePath, filename, { height, width })
  39. return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail })
  40. }
  41. function createPlaceholderThumbnail (fileUrl: string, video: VideoModel, type: ThumbnailType, size: ImageSize) {
  42. const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
  43. const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel()
  44. thumbnail.filename = filename
  45. thumbnail.height = height
  46. thumbnail.width = width
  47. thumbnail.type = type
  48. thumbnail.fileUrl = fileUrl
  49. return thumbnail
  50. }
  51. // ---------------------------------------------------------------------------
  52. export {
  53. generateVideoMiniature,
  54. createVideoMiniatureFromUrl,
  55. createVideoMiniatureFromExisting,
  56. createPlaceholderThumbnail,
  57. createPlaylistMiniatureFromUrl,
  58. createPlaylistMiniatureFromExisting
  59. }
  60. function buildMetadataFromPlaylist (playlist: VideoPlaylistModel, size: ImageSize) {
  61. const filename = playlist.generateThumbnailName()
  62. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  63. return {
  64. filename,
  65. basePath,
  66. existingThumbnail: playlist.Thumbnail,
  67. outputPath: join(basePath, filename),
  68. height: size ? size.height : THUMBNAILS_SIZE.height,
  69. width: size ? size.width : THUMBNAILS_SIZE.width
  70. }
  71. }
  72. function buildMetadataFromVideo (video: VideoModel, type: ThumbnailType, size?: ImageSize) {
  73. const existingThumbnail = Array.isArray(video.Thumbnails)
  74. ? video.Thumbnails.find(t => t.type === type)
  75. : undefined
  76. if (type === ThumbnailType.MINIATURE) {
  77. const filename = video.generateThumbnailName()
  78. const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
  79. return {
  80. filename,
  81. basePath,
  82. existingThumbnail,
  83. outputPath: join(basePath, filename),
  84. height: size ? size.height : THUMBNAILS_SIZE.height,
  85. width: size ? size.width : THUMBNAILS_SIZE.width
  86. }
  87. }
  88. if (type === ThumbnailType.PREVIEW) {
  89. const filename = video.generatePreviewName()
  90. const basePath = CONFIG.STORAGE.PREVIEWS_DIR
  91. return {
  92. filename,
  93. basePath,
  94. existingThumbnail,
  95. outputPath: join(basePath, filename),
  96. height: size ? size.height : PREVIEWS_SIZE.height,
  97. width: size ? size.width : PREVIEWS_SIZE.width
  98. }
  99. }
  100. return undefined
  101. }
  102. async function createThumbnailFromFunction (parameters: {
  103. thumbnailCreator: () => Promise<any>,
  104. filename: string,
  105. height: number,
  106. width: number,
  107. type: ThumbnailType,
  108. fileUrl?: string,
  109. existingThumbnail?: ThumbnailModel
  110. }) {
  111. const { thumbnailCreator, filename, width, height, type, existingThumbnail, fileUrl = null } = parameters
  112. const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel()
  113. thumbnail.filename = filename
  114. thumbnail.height = height
  115. thumbnail.width = width
  116. thumbnail.type = type
  117. thumbnail.fileUrl = fileUrl
  118. await thumbnailCreator()
  119. return thumbnail
  120. }