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, isNewVideo } = 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 isNewVideo = 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, isNewVideo, t)
91 return { videoDatabase, isNewVideo }
94 if (isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
97 async function onVideoFileOptimizerSuccess (videoArg: VideoModel, isNewVideo: boolean) {
98 if (videoArg === undefined) return undefined
100 // Outside the transaction (IO on disk)
101 const { videoFileResolution } = await videoArg.getOriginalFileResolution()
103 const videoDatabase = await sequelizeTypescript.transaction(async t => {
104 // Maybe the video changed in database, refresh it
105 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
106 // Video does not exist anymore
107 if (!videoDatabase) return undefined
109 // Create transcoding jobs if there are enabled resolutions
110 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
112 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
113 { resolutions: resolutionsEnabled }
116 if (resolutionsEnabled.length !== 0) {
117 const tasks: Bluebird<Bull.Job<any>>[] = []
119 for (const resolution of resolutionsEnabled) {
121 videoUUID: videoDatabase.uuid,
125 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
129 await Promise.all(tasks)
131 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
133 // No transcoding to do, it's now published
134 videoDatabase.state = VideoState.PUBLISHED
135 videoDatabase = await videoDatabase.save({ transaction: t })
137 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
140 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
145 if (isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
148 // ---------------------------------------------------------------------------
152 processVideoFileImport