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
17 async function processVideoFile (job: kue.Job) {
18 const payload = job.data as VideoFilePayload
19 logger.info('Processing video file in job %d.', job.id)
21 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
22 // No video, maybe deleted?
24 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
28 // Transcoding in other resolution
29 if (payload.resolution) {
30 await video.transcodeOriginalVideofile(payload.resolution)
31 await onVideoFileTranscoderSuccess(video)
33 await video.optimizeOriginalVideofile()
34 await onVideoFileOptimizerSuccess(video)
40 async function onVideoFileTranscoderSuccess (video: VideoModel) {
41 if (video === undefined) return undefined
43 // Maybe the video changed in database, refresh it
44 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
45 // Video does not exist anymore
46 if (!videoDatabase) return undefined
48 if (video.privacy !== VideoPrivacy.PRIVATE) {
49 await sendUpdateVideo(video, undefined)
55 async function onVideoFileOptimizerSuccess (video: VideoModel) {
56 if (video === undefined) return undefined
58 // Maybe the video changed in database, refresh it
59 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
60 // Video does not exist anymore
61 if (!videoDatabase) return undefined
63 if (video.privacy !== VideoPrivacy.PRIVATE) {
64 // Now we'll add the video's meta data to our followers
65 await sendCreateVideo(video, undefined)
66 await shareVideoByServerAndChannel(video, undefined)
69 const originalFileHeight = await videoDatabase.getOriginalFileHeight()
71 // Create transcoding jobs if there are enabled resolutions
72 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
74 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight,
75 { resolutions: resolutionsEnabled }
78 if (resolutionsEnabled.length !== 0) {
80 await sequelizeTypescript.transaction(async t => {
81 const tasks: Promise<any>[] = []
83 for (const resolution of resolutionsEnabled) {
85 videoUUID: videoDatabase.uuid,
89 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
93 await Promise.all(tasks)
96 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
98 logger.warn('Cannot transcode the video.', err)
101 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
106 // ---------------------------------------------------------------------------