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, transcodeOriginalVideofile, optimizeVideofile } from '../../video-transcoding'
13 export type VideoFilePayload = {
16 resolution?: VideoResolution
17 isPortraitMode?: boolean
20 export type VideoFileImportPayload = {
25 async function processVideoFileImport (job: Bull.Job) {
26 const payload = job.data as VideoFileImportPayload
27 logger.info('Processing video file import in job %d.', job.id)
29 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
30 // No video, maybe deleted?
32 logger.info('Do not process job %d, video does not exist.', job.id)
36 await importVideoFile(video, payload.filePath)
38 await onVideoFileTranscoderOrImportSuccess(video)
42 async function processVideoFile (job: Bull.Job) {
43 const payload = job.data as VideoFilePayload
44 logger.info('Processing video file in job %d.', job.id)
46 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
47 // No video, maybe deleted?
49 logger.info('Do not process job %d, video does not exist.', job.id)
53 // Transcoding in other resolution
54 if (payload.resolution) {
55 await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false)
57 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
59 await optimizeVideofile(video)
61 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
67 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
68 if (video === undefined) return undefined
70 return sequelizeTypescript.transaction(async t => {
71 // Maybe the video changed in database, refresh it
72 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
73 // Video does not exist anymore
74 if (!videoDatabase) return undefined
76 let isNewVideo = false
78 // We transcoded the video file in another format, now we can publish it
79 if (videoDatabase.state !== VideoState.PUBLISHED) {
82 videoDatabase.state = VideoState.PUBLISHED
83 videoDatabase.publishedAt = new Date()
84 videoDatabase = await videoDatabase.save({ transaction: t })
87 // If the video was not published, we consider it is a new one for other instances
88 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
94 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
95 if (video === undefined) return undefined
97 // Outside the transaction (IO on disk)
98 const { videoFileResolution } = await video.getOriginalFileResolution()
100 return sequelizeTypescript.transaction(async t => {
101 // Maybe the video changed in database, refresh it
102 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
103 // Video does not exist anymore
104 if (!videoDatabase) return undefined
106 // Create transcoding jobs if there are enabled resolutions
107 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
109 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
110 { resolutions: resolutionsEnabled }
113 if (resolutionsEnabled.length !== 0) {
114 const tasks: Bluebird<any>[] = []
116 for (const resolution of resolutionsEnabled) {
118 videoUUID: videoDatabase.uuid,
122 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
126 await Promise.all(tasks)
128 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
130 // No transcoding to do, it's now published
131 video.state = VideoState.PUBLISHED
132 video = await video.save({ transaction: t })
134 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
137 return federateVideoIfNeeded(video, isNewVideo, t)
141 // ---------------------------------------------------------------------------
145 processVideoFileImport