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 = {
14 resolution?: VideoResolution,
15 isPortraitMode?: boolean
18 async function processVideoFile (job: kue.Job) {
19 const payload = job.data as VideoFilePayload
20 logger.info('Processing video file in job %d.', job.id)
22 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
23 // No video, maybe deleted?
25 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
29 // Transcoding in other resolution
30 if (payload.resolution) {
31 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
32 await onVideoFileTranscoderSuccess(video)
34 await video.optimizeOriginalVideofile()
35 await onVideoFileOptimizerSuccess(video)
41 async function onVideoFileTranscoderSuccess (video: VideoModel) {
42 if (video === undefined) return undefined
44 // Maybe the video changed in database, refresh it
45 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
46 // Video does not exist anymore
47 if (!videoDatabase) return undefined
49 if (video.privacy !== VideoPrivacy.PRIVATE) {
50 await sendUpdateVideo(video, undefined)
56 async function onVideoFileOptimizerSuccess (video: VideoModel) {
57 if (video === undefined) return undefined
59 // Maybe the video changed in database, refresh it
60 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
61 // Video does not exist anymore
62 if (!videoDatabase) return undefined
64 if (video.privacy !== VideoPrivacy.PRIVATE) {
65 // Now we'll add the video's meta data to our followers
66 await sequelizeTypescript.transaction(async t => {
67 await sendCreateVideo(video, t)
68 await shareVideoByServerAndChannel(video, t)
72 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
74 // Create transcoding jobs if there are enabled resolutions
75 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
77 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
78 { resolutions: resolutionsEnabled }
81 if (resolutionsEnabled.length !== 0) {
82 const tasks: Promise<any>[] = []
84 for (const resolution of resolutionsEnabled) {
86 videoUUID: videoDatabase.uuid,
90 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
94 await Promise.all(tasks)
96 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
98 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
103 // ---------------------------------------------------------------------------