]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
94a5ff8a
C
1import * as kue from 'kue'
2import { VideoResolution } from '../../../../shared'
e12a0092 3import { VideoPrivacy } from '../../../../shared/models/videos'
da854ddd
C
4import { logger } from '../../../helpers/logger'
5import { computeResolutionsToTranscode } from '../../../helpers/utils'
3fd3ab2d
C
6import { sequelizeTypescript } from '../../../initializers'
7import { VideoModel } from '../../../models/video/video'
a7d647c4 8import { shareVideoByServerAndChannel } from '../../activitypub'
94a5ff8a
C
9import { sendCreateVideo, sendUpdateVideo } from '../../activitypub/send'
10import { JobQueue } from '../job-queue'
40298b02 11
94a5ff8a
C
12export type VideoFilePayload = {
13 videoUUID: string
0c948c16
C
14 isNewVideo: boolean
15 resolution?: VideoResolution
056aa7f2 16 isPortraitMode?: boolean
94a5ff8a
C
17}
18
0138af92
FF
19export type VideoImportPayload = {
20 videoUUID: string,
21 filePath: string
22}
23
24async 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
94a5ff8a
C
41async 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)
f5028693
C
46 // No video, maybe deleted?
47 if (!video) {
94a5ff8a 48 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
f5028693
C
49 return undefined
50 }
51
94a5ff8a
C
52 // Transcoding in other resolution
53 if (payload.resolution) {
056aa7f2 54 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
0138af92 55 await onVideoFileTranscoderOrImportSuccess(video)
94a5ff8a
C
56 } else {
57 await video.optimizeOriginalVideofile()
0c948c16 58 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
94a5ff8a 59 }
031094f7 60
f5028693 61 return video
40298b02
C
62}
63
0138af92 64async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) {
94a5ff8a
C
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
40298b02
C
77}
78
0c948c16 79async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
031094f7
C
80 if (video === undefined) return undefined
81
4077df72 82 // Maybe the video changed in database, refresh it
3fd3ab2d 83 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
4077df72
C
84 // Video does not exist anymore
85 if (!videoDatabase) return undefined
86
e12a0092 87 if (video.privacy !== VideoPrivacy.PRIVATE) {
0c948c16
C
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 }
e12a0092 97 }
f5028693 98
056aa7f2 99 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
f5028693 100
8e10cf1a 101 // Create transcoding jobs if there are enabled resolutions
056aa7f2 102 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
f5028693 103 logger.info(
056aa7f2 104 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
f5028693
C
105 { resolutions: resolutionsEnabled }
106 )
107
108 if (resolutionsEnabled.length !== 0) {
a7977280
C
109 const tasks: Promise<any>[] = []
110
111 for (const resolution of resolutionsEnabled) {
112 const dataInput = {
113 videoUUID: videoDatabase.uuid,
0c948c16
C
114 resolution,
115 isNewVideo
a7977280
C
116 }
117
118 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
119 tasks.push(p)
f5028693 120 }
a7977280
C
121
122 await Promise.all(tasks)
123
124 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
f5028693
C
125 } else {
126 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
127 return undefined
128 }
40298b02
C
129}
130
131// ---------------------------------------------------------------------------
132
133export {
0138af92
FF
134 processVideoFile,
135 processVideoImport
40298b02 136}