local-video-creator.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { ffprobePromise } from '@peertube/peertube-ffmpeg'
  2. import {
  3. LiveVideoCreate,
  4. LiveVideoLatencyMode,
  5. ThumbnailType,
  6. ThumbnailType_Type,
  7. VideoCreate,
  8. VideoPrivacy,
  9. VideoStateType
  10. } from '@peertube/peertube-models'
  11. import { buildUUID } from '@peertube/peertube-node-utils'
  12. import { sequelizeTypescript } from '@server/initializers/database.js'
  13. import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting.js'
  14. import { VideoLiveModel } from '@server/models/video/video-live.js'
  15. import { VideoPasswordModel } from '@server/models/video/video-password.js'
  16. import { VideoSourceModel } from '@server/models/video/video-source.js'
  17. import { VideoModel } from '@server/models/video/video.js'
  18. import { MVideoFullLight, MThumbnail, MChannel, MChannelAccountLight, MVideoFile, MUser } from '@server/types/models/index.js'
  19. import { move } from 'fs-extra/esm'
  20. import { getLocalVideoActivityPubUrl } from './activitypub/url.js'
  21. import { generateLocalVideoMiniature, updateLocalVideoMiniatureFromExisting } from './thumbnail.js'
  22. import { autoBlacklistVideoIfNeeded } from './video-blacklist.js'
  23. import { buildNewFile } from './video-file.js'
  24. import { addVideoJobsAfterCreation } from './video-jobs.js'
  25. import { VideoPathManager } from './video-path-manager.js'
  26. import { setVideoTags } from './video.js'
  27. import { FilteredModelAttributes } from '@server/types/sequelize.js'
  28. import { CONFIG } from '@server/initializers/config.js'
  29. import { Hooks } from './plugins/hooks.js'
  30. import Ffmpeg from 'fluent-ffmpeg'
  31. import { ScheduleVideoUpdateModel } from '@server/models/video/schedule-video-update.js'
  32. import { replaceChapters, replaceChaptersFromDescriptionIfNeeded } from './video-chapters.js'
  33. import { LoggerTagsFn, logger } from '@server/helpers/logger.js'
  34. import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
  35. import { federateVideoIfNeeded } from './activitypub/videos/federate.js'
  36. import { buildAspectRatio } from '@peertube/peertube-core-utils'
  37. type VideoAttributes = Omit<VideoCreate, 'channelId'> & {
  38. duration: number
  39. isLive: boolean
  40. state: VideoStateType
  41. filename: string
  42. }
  43. type LiveAttributes = Pick<LiveVideoCreate, 'permanentLive' | 'latencyMode' | 'saveReplay' | 'replaySettings'> & {
  44. streamKey?: string
  45. }
  46. export type ThumbnailOptions = {
  47. path: string
  48. type: ThumbnailType_Type
  49. automaticallyGenerated: boolean
  50. keepOriginal: boolean
  51. }[]
  52. type ChaptersOption = { timecode: number, title: string }[]
  53. type VideoAttributeHookFilter =
  54. 'filter:api.video.user-import.video-attribute.result' |
  55. 'filter:api.video.upload.video-attribute.result' |
  56. 'filter:api.video.live.video-attribute.result'
  57. export class LocalVideoCreator {
  58. private readonly lTags: LoggerTagsFn
  59. private readonly videoFilePath: string | undefined
  60. private readonly videoAttributes: VideoAttributes
  61. private readonly liveAttributes: LiveAttributes | undefined
  62. private readonly channel: MChannelAccountLight
  63. private readonly videoAttributeResultHook: VideoAttributeHookFilter
  64. private video: MVideoFullLight
  65. private videoFile: MVideoFile
  66. private ffprobe: Ffmpeg.FfprobeData
  67. constructor (private readonly options: {
  68. lTags: LoggerTagsFn
  69. videoFilePath: string
  70. videoAttributes: VideoAttributes
  71. liveAttributes: LiveAttributes
  72. channel: MChannelAccountLight
  73. user: MUser
  74. videoAttributeResultHook: VideoAttributeHookFilter
  75. thumbnails: ThumbnailOptions
  76. chapters: ChaptersOption | undefined
  77. fallbackChapters: {
  78. fromDescription: boolean
  79. finalFallback: ChaptersOption | undefined
  80. }
  81. }) {
  82. this.videoFilePath = options.videoFilePath
  83. this.videoAttributes = options.videoAttributes
  84. this.liveAttributes = options.liveAttributes
  85. this.channel = options.channel
  86. this.videoAttributeResultHook = options.videoAttributeResultHook
  87. }
  88. async create () {
  89. this.video = new VideoModel(
  90. await Hooks.wrapObject(this.buildVideo(this.videoAttributes, this.channel), this.videoAttributeResultHook)
  91. ) as MVideoFullLight
  92. this.video.VideoChannel = this.channel
  93. this.video.url = getLocalVideoActivityPubUrl(this.video)
  94. if (this.videoFilePath) {
  95. this.ffprobe = await ffprobePromise(this.videoFilePath)
  96. this.videoFile = await buildNewFile({ path: this.videoFilePath, mode: 'web-video', ffprobe: this.ffprobe })
  97. const destination = VideoPathManager.Instance.getFSVideoFileOutputPath(this.video, this.videoFile)
  98. await move(this.videoFilePath, destination)
  99. this.video.aspectRatio = buildAspectRatio({ width: this.videoFile.width, height: this.videoFile.height })
  100. }
  101. const thumbnails = await this.createThumbnails()
  102. await retryTransactionWrapper(() => {
  103. return sequelizeTypescript.transaction(async transaction => {
  104. await this.video.save({ transaction })
  105. for (const thumbnail of thumbnails) {
  106. await this.video.addAndSaveThumbnail(thumbnail, transaction)
  107. }
  108. if (this.videoFile) {
  109. this.videoFile.videoId = this.video.id
  110. await this.videoFile.save({ transaction })
  111. this.video.VideoFiles = [ this.videoFile ]
  112. }
  113. await setVideoTags({ video: this.video, tags: this.videoAttributes.tags, transaction })
  114. // Schedule an update in the future?
  115. if (this.videoAttributes.scheduleUpdate) {
  116. await ScheduleVideoUpdateModel.create({
  117. videoId: this.video.id,
  118. updateAt: new Date(this.videoAttributes.scheduleUpdate.updateAt),
  119. privacy: this.videoAttributes.scheduleUpdate.privacy || null
  120. }, { transaction })
  121. }
  122. if (this.options.chapters) {
  123. await replaceChapters({ video: this.video, chapters: this.options.chapters, transaction })
  124. } else if (this.options.fallbackChapters.fromDescription) {
  125. if (!await replaceChaptersFromDescriptionIfNeeded({ newDescription: this.video.description, video: this.video, transaction })) {
  126. await replaceChapters({ video: this.video, chapters: this.options.fallbackChapters.finalFallback, transaction })
  127. }
  128. }
  129. await autoBlacklistVideoIfNeeded({
  130. video: this.video,
  131. user: this.options.user,
  132. isRemote: false,
  133. isNew: true,
  134. isNewFile: true,
  135. transaction
  136. })
  137. if (this.videoAttributes.filename) {
  138. await VideoSourceModel.create({
  139. filename: this.videoAttributes.filename,
  140. videoId: this.video.id
  141. }, { transaction })
  142. }
  143. if (this.videoAttributes.privacy === VideoPrivacy.PASSWORD_PROTECTED) {
  144. await VideoPasswordModel.addPasswords(this.videoAttributes.videoPasswords, this.video.id, transaction)
  145. }
  146. if (this.videoAttributes.isLive) {
  147. const videoLive = new VideoLiveModel({
  148. saveReplay: this.liveAttributes.saveReplay || false,
  149. permanentLive: this.liveAttributes.permanentLive || false,
  150. latencyMode: this.liveAttributes.latencyMode || LiveVideoLatencyMode.DEFAULT,
  151. streamKey: this.liveAttributes.streamKey || buildUUID()
  152. })
  153. if (videoLive.saveReplay) {
  154. const replaySettings = new VideoLiveReplaySettingModel({
  155. privacy: this.liveAttributes.replaySettings?.privacy ?? this.video.privacy
  156. })
  157. await replaySettings.save({ transaction })
  158. videoLive.replaySettingId = replaySettings.id
  159. }
  160. videoLive.videoId = this.video.id
  161. this.video.VideoLive = await videoLive.save({ transaction })
  162. }
  163. if (this.videoFile) {
  164. transaction.afterCommit(() => {
  165. addVideoJobsAfterCreation({ video: this.video, videoFile: this.videoFile })
  166. .catch(err => logger.error('Cannot build new video jobs of %s.', this.video.uuid, { err, ...this.lTags(this.video.uuid) }))
  167. })
  168. } else {
  169. await federateVideoIfNeeded(this.video, true, transaction)
  170. }
  171. })
  172. })
  173. // Channel has a new content, set as updated
  174. await this.channel.setAsUpdated()
  175. return { video: this.video, videoFile: this.videoFile }
  176. }
  177. private async createThumbnails () {
  178. const promises: Promise<MThumbnail>[] = []
  179. let toGenerate = [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]
  180. for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
  181. const thumbnail = this.options.thumbnails.find(t => t.type === type)
  182. if (!thumbnail) continue
  183. promises.push(
  184. updateLocalVideoMiniatureFromExisting({
  185. inputPath: thumbnail.path,
  186. video: this.video,
  187. type,
  188. automaticallyGenerated: thumbnail.automaticallyGenerated || false,
  189. keepOriginal: thumbnail.keepOriginal
  190. })
  191. )
  192. toGenerate = toGenerate.filter(t => t !== thumbnail.type)
  193. }
  194. return [
  195. ...await Promise.all(promises),
  196. ...await generateLocalVideoMiniature({ video: this.video, videoFile: this.videoFile, types: toGenerate, ffprobe: this.ffprobe })
  197. ]
  198. }
  199. private buildVideo (videoInfo: VideoAttributes, channel: MChannel): FilteredModelAttributes<VideoModel> {
  200. return {
  201. name: videoInfo.name,
  202. state: videoInfo.state,
  203. remote: false,
  204. category: videoInfo.category,
  205. licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE,
  206. language: videoInfo.language,
  207. commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
  208. downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED,
  209. waitTranscoding: videoInfo.waitTranscoding || false,
  210. nsfw: videoInfo.nsfw || false,
  211. description: videoInfo.description,
  212. support: videoInfo.support,
  213. privacy: videoInfo.privacy || VideoPrivacy.PRIVATE,
  214. isLive: videoInfo.isLive,
  215. channelId: channel.id,
  216. originallyPublishedAt: videoInfo.originallyPublishedAt
  217. ? new Date(videoInfo.originallyPublishedAt)
  218. : null,
  219. uuid: buildUUID(),
  220. duration: videoInfo.duration
  221. }
  222. }
  223. }