1 import * as kue from 'kue'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { computeResolutionsToTranscode } from '../../../helpers/utils'
5 import { VideoModel } from '../../../models/video/video'
6 import { JobQueue } from '../job-queue'
7 import { federateVideoIfNeeded } from '../../activitypub'
8 import { retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { sequelizeTypescript } from '../../../initializers'
11 export type VideoFilePayload = {
14 resolution?: VideoResolution
15 isPortraitMode?: boolean
18 export type VideoFileImportPayload = {
23 async function processVideoFileImport (job: kue.Job) {
24 const payload = job.data as VideoFileImportPayload
25 logger.info('Processing video file import in job %d.', job.id)
27 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
28 // No video, maybe deleted?
30 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
34 await video.importVideoFile(payload.filePath)
36 await onVideoFileTranscoderOrImportSuccess(video)
40 async function processVideoFile (job: kue.Job) {
41 const payload = job.data as VideoFilePayload
42 logger.info('Processing video file in job %d.', job.id)
44 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
45 // No video, maybe deleted?
47 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
51 // Transcoding in other resolution
52 if (payload.resolution) {
53 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
55 await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video)
57 await video.optimizeOriginalVideofile()
59 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo)
65 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
66 if (video === undefined) return undefined
68 return sequelizeTypescript.transaction(async t => {
69 // Maybe the video changed in database, refresh it
70 let videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
71 // Video does not exist anymore
72 if (!videoDatabase) return undefined
74 // We transcoded the video file in another format, now we can publish it
75 const oldState = videoDatabase.state
76 videoDatabase.state = VideoState.PUBLISHED
77 videoDatabase = await videoDatabase.save({ transaction: t })
79 // If the video was not published, we consider it is a new one for other instances
80 const isNewVideo = oldState !== VideoState.PUBLISHED
81 await federateVideoIfNeeded(videoDatabase, isNewVideo, t)
87 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
88 if (video === undefined) return undefined
90 // Outside the transaction (IO on disk)
91 const { videoFileResolution } = await video.getOriginalFileResolution()
93 return sequelizeTypescript.transaction(async t => {
94 // Maybe the video changed in database, refresh it
95 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid, t)
96 // Video does not exist anymore
97 if (!videoDatabase) return undefined
99 // Create transcoding jobs if there are enabled resolutions
100 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
102 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
103 { resolutions: resolutionsEnabled }
106 if (resolutionsEnabled.length !== 0) {
107 const tasks: Promise<any>[] = []
109 for (const resolution of resolutionsEnabled) {
111 videoUUID: videoDatabase.uuid,
115 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
119 await Promise.all(tasks)
121 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
123 // No transcoding to do, it's now published
124 video.state = VideoState.PUBLISHED
125 video = await video.save({ transaction: t })
127 logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid)
130 return federateVideoIfNeeded(video, isNewVideo, t)
134 // ---------------------------------------------------------------------------
138 processVideoFileImport