1 import * as kue from 'kue'
2 import { VideoResolution } from '../../../../shared'
3 import { VideoPrivacy } from '../../../../shared/models/videos'
4 import { logger } from '../../../helpers/logger'
5 import { computeResolutionsToTranscode } from '../../../helpers/utils'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { VideoModel } from '../../../models/video/video'
8 import { shareVideoByServerAndChannel } from '../../activitypub'
9 import { sendCreateVideo, sendUpdateVideo } from '../../activitypub/send'
10 import { JobQueue } from '../job-queue'
12 export type VideoFilePayload = {
15 resolution?: VideoResolution
16 isPortraitMode?: boolean
19 async function processVideoFile (job: kue.Job) {
20 const payload = job.data as VideoFilePayload
21 logger.info('Processing video file in job %d.', job.id)
23 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
24 // No video, maybe deleted?
26 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
30 // Transcoding in other resolution
31 if (payload.resolution) {
32 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
33 await onVideoFileTranscoderSuccess(video)
35 await video.optimizeOriginalVideofile()
36 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
42 async function onVideoFileTranscoderSuccess (video: VideoModel) {
43 if (video === undefined) return undefined
45 // Maybe the video changed in database, refresh it
46 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
47 // Video does not exist anymore
48 if (!videoDatabase) return undefined
50 if (video.privacy !== VideoPrivacy.PRIVATE) {
51 await sendUpdateVideo(video, undefined)
57 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
58 if (video === undefined) return undefined
60 // Maybe the video changed in database, refresh it
61 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
62 // Video does not exist anymore
63 if (!videoDatabase) return undefined
65 if (video.privacy !== VideoPrivacy.PRIVATE) {
66 if (isNewVideo === true) {
67 // Now we'll add the video's meta data to our followers
68 await sequelizeTypescript.transaction(async t => {
69 await sendCreateVideo(video, t)
70 await shareVideoByServerAndChannel(video, t)
73 await sendUpdateVideo(video, undefined)
77 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
79 // Create transcoding jobs if there are enabled resolutions
80 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
82 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
83 { resolutions: resolutionsEnabled }
86 if (resolutionsEnabled.length !== 0) {
87 const tasks: Promise<any>[] = []
89 for (const resolution of resolutionsEnabled) {
91 videoUUID: videoDatabase.uuid,
96 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
100 await Promise.all(tasks)
102 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
104 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
109 // ---------------------------------------------------------------------------