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'
12 export type VideoFilePayload = {
15 resolution?: VideoResolution
16 isPortraitMode?: boolean
19 export type VideoFileImportPayload = {
24 async function processVideoFileImport (job: Bull.Job) {
25 const payload = job.data as VideoFileImportPayload
26 logger.info('Processing video file import in job %d.', job.id)
28 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
29 // No video, maybe deleted?
31 logger.info('Do not process job %d, video does not exist.', job.id)
35 await video.importVideoFile(payload.filePath)
37 await onVideoFileTranscoderOrImportSuccess(video)
41 async function processVideoFile (job: Bull.Job) {
42 const payload = job.data as VideoFilePayload
43 logger.info('Processing video file in job %d.', job.id)
45 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
46 // No video, maybe deleted?
48 logger.info('Do not process job %d, video does not exist.', job.id)
52 // Transcoding in other resolution
53 if (payload.resolution) {
54 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode || false)
56 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
58 await video.optimizeOriginalVideofile()
60 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
66 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
67 if (video === undefined) return undefined
69 return sequelizeTypescript.transaction(async t => {
70 // Maybe the video changed in database, refresh it
71 let videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
72 // Video does not exist anymore
73 if (!videoDatabase) return undefined
75 let isNewVideo = false
77 // We transcoded the video file in another format, now we can publish it
78 if (videoDatabase.state !== VideoState.PUBLISHED) {
81 videoDatabase.state = VideoState.PUBLISHED
82 videoDatabase.publishedAt = new Date()
83 videoDatabase = await videoDatabase.save({ transaction: t })
86 // If the video was not published, we consider it is a new one for other instances
87 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
93 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
94 if (video === undefined) return undefined
96 // Outside the transaction (IO on disk)
97 const { videoFileResolution } = await video.getOriginalFileResolution()
99 return sequelizeTypescript.transaction(async t => {
100 // Maybe the video changed in database, refresh it
101 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
102 // Video does not exist anymore
103 if (!videoDatabase) return undefined
105 // Create transcoding jobs if there are enabled resolutions
106 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
108 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
109 { resolutions: resolutionsEnabled }
112 if (resolutionsEnabled.length !== 0) {
113 const tasks: Bluebird<any>[] = []
115 for (const resolution of resolutionsEnabled) {
117 videoUUID: videoDatabase.uuid,
121 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
125 await Promise.all(tasks)
127 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
129 // No transcoding to do, it's now published
130 video.state = VideoState.PUBLISHED
131 video = await video.save({ transaction: t })
133 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
136 return federateVideoIfNeeded(video, isNewVideo, t)
140 // ---------------------------------------------------------------------------
144 processVideoFileImport