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 }
94 // don't notify prior to scheduled video update
95 if (videoPublished && !videoDatabase.ScheduleVideoUpdate) {
96 Notifier.Instance.notifyOnNewVideo(videoDatabase)
97 Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
101 async function onVideoFileOptimizerSuccess (videoArg: VideoModel, isNewVideo: boolean) {
102 if (videoArg === undefined) return undefined
104 // Outside the transaction (IO on disk)
105 const { videoFileResolution } = await videoArg.getOriginalFileResolution()
107 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
108 // Maybe the video changed in database, refresh it
109 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
110 // Video does not exist anymore
111 if (!videoDatabase) return undefined
113 // Create transcoding jobs if there are enabled resolutions
114 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
116 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
117 { resolutions: resolutionsEnabled }
120 let videoPublished = false
122 if (resolutionsEnabled.length !== 0) {
123 const tasks: Bluebird<Bull.Job<any>>[] = []
125 for (const resolution of resolutionsEnabled) {
127 videoUUID: videoDatabase.uuid,
131 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
135 await Promise.all(tasks)
137 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
139 videoPublished = true
141 // No transcoding to do, it's now published
142 videoDatabase.state = VideoState.PUBLISHED
143 videoDatabase = await videoDatabase.save({ transaction: t })
145 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
148 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
150 return { videoDatabase, videoPublished }
153 // don't notify prior to scheduled video update
154 if (!videoDatabase.ScheduleVideoUpdate) {
155 if (isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
156 if (videoPublished) Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
160 // ---------------------------------------------------------------------------
164 processVideoFileImport