]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file.ts
Add create-import-video-file-job command
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
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'
11
12 export type VideoFilePayload = {
13 videoUUID: string
14 isNewVideo: boolean
15 resolution?: VideoResolution
16 isPortraitMode?: boolean
17 }
18
19 export type VideoImportPayload = {
20 videoUUID: string,
21 filePath: string
22 }
23
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)
27
28 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
29 // No video, maybe deleted?
30 if (!video) {
31 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
32 return undefined
33 }
34
35 await video.importVideoFile(payload.filePath)
36
37 await onVideoFileTranscoderOrImportSuccess(video)
38 return video
39 }
40
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)
44
45 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID)
46 // No video, maybe deleted?
47 if (!video) {
48 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
49 return undefined
50 }
51
52 // Transcoding in other resolution
53 if (payload.resolution) {
54 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
55 await onVideoFileTranscoderOrImportSuccess(video)
56 } else {
57 await video.optimizeOriginalVideofile()
58 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
59 }
60
61 return video
62 }
63
64 async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
65 if (video === undefined) return undefined
66
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
71
72 if (video.privacy !== VideoPrivacy.PRIVATE) {
73 await sendUpdateVideo(video, undefined)
74 }
75
76 return undefined
77 }
78
79 async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
80 if (video === undefined) return undefined
81
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
86
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)
93 })
94 } else {
95 await sendUpdateVideo(video, undefined)
96 }
97 }
98
99 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
100
101 // Create transcoding jobs if there are enabled resolutions
102 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
103 logger.info(
104 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
105 { resolutions: resolutionsEnabled }
106 )
107
108 if (resolutionsEnabled.length !== 0) {
109 const tasks: Promise<any>[] = []
110
111 for (const resolution of resolutionsEnabled) {
112 const dataInput = {
113 videoUUID: videoDatabase.uuid,
114 resolution,
115 isNewVideo
116 }
117
118 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
119 tasks.push(p)
120 }
121
122 await Promise.all(tasks)
123
124 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
125 } else {
126 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
127 return undefined
128 }
129 }
130
131 // ---------------------------------------------------------------------------
132
133 export {
134 processVideoFile,
135 processVideoImport
136 }