]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-transcoding.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-transcoding.ts
1 import * as Bull from 'bull'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { VideoModel } from '../../../models/video/video'
5 import { JobQueue } from '../job-queue'
6 import { federateVideoIfNeeded } from '../../activitypub'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { sequelizeTypescript } from '../../../initializers'
9 import * as Bluebird from 'bluebird'
10 import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
11 import { generateHlsPlaylist, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding'
12 import { Notifier } from '../../notifier'
13 import { CONFIG } from '../../../initializers/config'
14
15 export type VideoTranscodingPayload = {
16 videoUUID: string
17 resolution?: VideoResolution
18 isNewVideo?: boolean
19 isPortraitMode?: boolean
20 generateHlsPlaylist?: boolean
21 }
22
23 async function processVideoTranscoding (job: Bull.Job) {
24 const payload = job.data as VideoTranscodingPayload
25 logger.info('Processing video file in job %d.', job.id)
26
27 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
28 // No video, maybe deleted?
29 if (!video) {
30 logger.info('Do not process job %d, video does not exist.', job.id)
31 return undefined
32 }
33
34 if (payload.generateHlsPlaylist) {
35 await generateHlsPlaylist(video, payload.resolution, payload.isPortraitMode || false)
36
37 await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video)
38 } else if (payload.resolution) { // Transcoding in other resolution
39 await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false)
40
41 await retryTransactionWrapper(publishVideoIfNeeded, video, payload)
42 } else {
43 await optimizeVideofile(video)
44
45 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload)
46 }
47
48 return video
49 }
50
51 async function onHlsPlaylistGenerationSuccess (video: VideoModel) {
52 if (video === undefined) return undefined
53
54 await sequelizeTypescript.transaction(async t => {
55 // Maybe the video changed in database, refresh it
56 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
57 // Video does not exist anymore
58 if (!videoDatabase) return undefined
59
60 // If the video was not published, we consider it is a new one for other instances
61 await federateVideoIfNeeded(videoDatabase, false, t)
62 })
63 }
64
65 async function publishVideoIfNeeded (video: VideoModel, payload?: VideoTranscodingPayload) {
66 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
67 // Maybe the video changed in database, refresh it
68 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
69 // Video does not exist anymore
70 if (!videoDatabase) return undefined
71
72 let videoPublished = false
73
74 // We transcoded the video file in another format, now we can publish it
75 if (videoDatabase.state !== VideoState.PUBLISHED) {
76 videoPublished = true
77
78 videoDatabase.state = VideoState.PUBLISHED
79 videoDatabase.publishedAt = new Date()
80 videoDatabase = await videoDatabase.save({ transaction: t })
81 }
82
83 // If the video was not published, we consider it is a new one for other instances
84 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
85
86 return { videoDatabase, videoPublished }
87 })
88
89 if (videoPublished) {
90 Notifier.Instance.notifyOnNewVideo(videoDatabase)
91 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
92 }
93
94 await createHlsJobIfEnabled(payload)
95 }
96
97 async function onVideoFileOptimizerSuccess (videoArg: VideoModel, payload: VideoTranscodingPayload) {
98 if (videoArg === undefined) return undefined
99
100 // Outside the transaction (IO on disk)
101 const { videoFileResolution } = await videoArg.getOriginalFileResolution()
102
103 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
104 // Maybe the video changed in database, refresh it
105 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
106 // Video does not exist anymore
107 if (!videoDatabase) return undefined
108
109 // Create transcoding jobs if there are enabled resolutions
110 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
111 logger.info(
112 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
113 { resolutions: resolutionsEnabled }
114 )
115
116 let videoPublished = false
117
118 if (resolutionsEnabled.length !== 0) {
119 const tasks: (Bluebird<Bull.Job<any>> | Promise<Bull.Job<any>>)[] = []
120
121 for (const resolution of resolutionsEnabled) {
122 const dataInput = {
123 videoUUID: videoDatabase.uuid,
124 resolution
125 }
126
127 const p = JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
128 tasks.push(p)
129 }
130
131 await Promise.all(tasks)
132
133 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
134 } else {
135 videoPublished = true
136
137 // No transcoding to do, it's now published
138 videoDatabase.state = VideoState.PUBLISHED
139 videoDatabase = await videoDatabase.save({ transaction: t })
140
141 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
142 }
143
144 await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t)
145
146 return { videoDatabase, videoPublished }
147 })
148
149 if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
150 if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
151
152 await createHlsJobIfEnabled(Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution }))
153 }
154
155 // ---------------------------------------------------------------------------
156
157 export {
158 processVideoTranscoding,
159 publishVideoIfNeeded
160 }
161
162 // ---------------------------------------------------------------------------
163
164 function createHlsJobIfEnabled (payload?: VideoTranscodingPayload) {
165 // Generate HLS playlist?
166 if (payload && CONFIG.TRANSCODING.HLS.ENABLED) {
167 const hlsTranscodingPayload = {
168 videoUUID: payload.videoUUID,
169 resolution: payload.resolution,
170 isPortraitMode: payload.isPortraitMode,
171
172 generateHlsPlaylist: true
173 }
174
175 return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload })
176 }
177 }