1 import * as Bull from 'bull'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { VideoModel } from '../../../models/video/video'
5 import { JobQueue } from '../job-queue'
6 import { federateVideoIfNeeded } from '../../activitypub'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { sequelizeTypescript } from '../../../initializers'
9 import * as Bluebird from 'bluebird'
10 import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
11 import { importVideoFile, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding'
12 import { Notifier } from '../../notifier'
14 export type VideoFilePayload = {
17 resolution?: VideoResolution
18 isPortraitMode?: boolean
21 export type VideoFileImportPayload = {
26 async function processVideoFileImport (job: Bull.Job) {
27 const payload = job.data as VideoFileImportPayload
28 logger.info('Processing video file import in job %d.', job.id)
30 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
31 // No video, maybe deleted?
33 logger.info('Do not process job %d, video does not exist.', job.id)
37 await importVideoFile(video, payload.filePath)
39 await onVideoFileTranscoderOrImportSuccess(video)
43 async function processVideoFile (job: Bull.Job) {
44 const payload = job.data as VideoFilePayload
45 logger.info('Processing video file in job %d.', job.id)
47 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
48 // No video, maybe deleted?
50 logger.info('Do not process job %d, video does not exist.', job.id)
54 // Transcoding in other resolution
55 if (payload.resolution) {
56 await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false)
58 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
60 await optimizeVideofile(video)
62 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
68 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
69 if (video === undefined) return undefined
71 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
72 // Maybe the video changed in database, refresh it
73 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
74 // Video does not exist anymore
75 if (!videoDatabase) return undefined
77 let videoPublished = false
79 // We transcoded the video file in another format, now we can publish it
80 if (videoDatabase.state !== VideoState.PUBLISHED) {
83 videoDatabase.state = VideoState.PUBLISHED
84 videoDatabase.publishedAt = new Date()
85 videoDatabase = await videoDatabase.save({ transaction: t })
88 // If the video was not published, we consider it is a new one for other instances
89 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
91 return { videoDatabase, videoPublished }
95 Notifier.Instance.notifyOnNewVideo(videoDatabase)
96 Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
100 async function onVideoFileOptimizerSuccess (videoArg: VideoModel, isNewVideo: boolean) {
101 if (videoArg === undefined) return undefined
103 // Outside the transaction (IO on disk)
104 const { videoFileResolution } = await videoArg.getOriginalFileResolution()
106 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
107 // Maybe the video changed in database, refresh it
108 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
109 // Video does not exist anymore
110 if (!videoDatabase) return undefined
112 // Create transcoding jobs if there are enabled resolutions
113 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
115 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
116 { resolutions: resolutionsEnabled }
119 let videoPublished = false
121 if (resolutionsEnabled.length !== 0) {
122 const tasks: Bluebird<Bull.Job<any>>[] = []
124 for (const resolution of resolutionsEnabled) {
126 videoUUID: videoDatabase.uuid,
130 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
134 await Promise.all(tasks)
136 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
138 videoPublished = true
140 // No transcoding to do, it's now published
141 videoDatabase.state = VideoState.PUBLISHED
142 videoDatabase = await videoDatabase.save({ transaction: t })
144 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
147 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
149 return { videoDatabase, videoPublished }
152 if (isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
153 if (videoPublished) Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
156 // ---------------------------------------------------------------------------
160 processVideoFileImport