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 export type VideoImportPayload = {
24 async function processVideoImport (job: kue.Job) {
25 const payload = job.data as VideoImportPayload
26 logger.info('Processing video import in job %d.', job.id)
28 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
29 // No video, maybe deleted?
31 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
35 await video.importVideoFile(payload.filePath)
37 await onVideoFileTranscoderOrImportSuccess(video)
41 async function processVideoFile (job: kue.Job) {
42 const payload = job.data as VideoFilePayload
43 logger.info('Processing video file in job %d.', job.id)
45 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
46 // No video, maybe deleted?
48 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
52 // Transcoding in other resolution
53 if (payload.resolution) {
54 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
55 await onVideoFileTranscoderOrImportSuccess(video)
57 await video.optimizeOriginalVideofile()
58 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
64 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
65 if (video === undefined) return undefined
67 // Maybe the video changed in database, refresh it
68 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
69 // Video does not exist anymore
70 if (!videoDatabase) return undefined
72 if (video.privacy !== VideoPrivacy.PRIVATE) {
73 await sendUpdateVideo(video, undefined)
79 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
80 if (video === undefined) return undefined
82 // Maybe the video changed in database, refresh it
83 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
84 // Video does not exist anymore
85 if (!videoDatabase) return undefined
87 if (video.privacy !== VideoPrivacy.PRIVATE) {
88 if (isNewVideo === true) {
89 // Now we'll add the video's meta data to our followers
90 await sequelizeTypescript.transaction(async t => {
91 await sendCreateVideo(video, t)
92 await shareVideoByServerAndChannel(video, t)
95 await sendUpdateVideo(video, undefined)
99 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
101 // Create transcoding jobs if there are enabled resolutions
102 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
104 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
105 { resolutions: resolutionsEnabled }
108 if (resolutionsEnabled.length !== 0) {
109 const tasks: Promise<any>[] = []
111 for (const resolution of resolutionsEnabled) {
113 videoUUID: videoDatabase.uuid,
118 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
122 await Promise.all(tasks)
124 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
126 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
131 // ---------------------------------------------------------------------------