]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-transcoding.ts
Begin auth plugin support
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-transcoding.ts
CommitLineData
94831479 1import * as Bull from 'bull'
d7a25329 2import { VideoResolution } from '../../../../shared'
da854ddd 3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { VideoModel } from '../../../models/video/video'
94a5ff8a 5import { JobQueue } from '../job-queue'
2186386c
C
6import { federateVideoIfNeeded } from '../../activitypub'
7import { retryTransactionWrapper } from '../../../helpers/database-utils'
6dd9de95 8import { sequelizeTypescript } from '../../../initializers'
06215f15 9import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
d7a25329 10import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding'
cef534ed 11import { Notifier } from '../../notifier'
6dd9de95 12import { CONFIG } from '../../../initializers/config'
d7a25329 13import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/typings/models'
40298b02 14
536598cf 15interface BaseTranscodingPayload {
94a5ff8a 16 videoUUID: string
09209296 17 isNewVideo?: boolean
536598cf
C
18}
19
20interface HLSTranscodingPayload extends BaseTranscodingPayload {
21 type: 'hls'
22 isPortraitMode?: boolean
23 resolution: VideoResolution
d7a25329 24 copyCodecs: boolean
536598cf
C
25}
26
27interface NewResolutionTranscodingPayload extends BaseTranscodingPayload {
28 type: 'new-resolution'
056aa7f2 29 isPortraitMode?: boolean
536598cf
C
30 resolution: VideoResolution
31}
32
33interface MergeAudioTranscodingPayload extends BaseTranscodingPayload {
34 type: 'merge-audio'
35 resolution: VideoResolution
36}
37
38interface OptimizeTranscodingPayload extends BaseTranscodingPayload {
39 type: 'optimize'
94a5ff8a
C
40}
41
a1587156
C
42export type VideoTranscodingPayload =
43 HLSTranscodingPayload
44 | NewResolutionTranscodingPayload
45 | OptimizeTranscodingPayload
46 | MergeAudioTranscodingPayload
536598cf 47
a0327eed
C
48async function processVideoTranscoding (job: Bull.Job) {
49 const payload = job.data as VideoTranscodingPayload
94a5ff8a
C
50 logger.info('Processing video file in job %d.', job.id)
51
627621c1 52 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
f5028693
C
53 // No video, maybe deleted?
54 if (!video) {
c1e791ba 55 logger.info('Do not process job %d, video does not exist.', job.id)
f5028693
C
56 return undefined
57 }
58
536598cf 59 if (payload.type === 'hls') {
d7a25329 60 await generateHlsPlaylist(video, payload.resolution, payload.copyCodecs, payload.isPortraitMode || false)
09209296
C
61
62 await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video)
536598cf 63 } else if (payload.type === 'new-resolution') {
d7a25329 64 await transcodeNewResolution(video, payload.resolution, payload.isPortraitMode || false)
2186386c 65
536598cf
C
66 await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload)
67 } else if (payload.type === 'merge-audio') {
68 await mergeAudioVideofile(video, payload.resolution)
69
70 await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload)
94a5ff8a 71 } else {
d7a25329 72 await optimizeOriginalVideofile(video)
2186386c 73
09209296 74 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload)
94a5ff8a 75 }
031094f7 76
f5028693 77 return video
40298b02
C
78}
79
d7a25329 80async function onHlsPlaylistGenerationSuccess (video: MVideoFullLight) {
09209296
C
81 if (video === undefined) return undefined
82
d7a25329
C
83 // We generated the HLS playlist, we don't need the webtorrent files anymore if the admin disabled it
84 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
85 for (const file of video.VideoFiles) {
86 await video.removeFile(file)
87 await file.destroy()
1b952dd4 88 }
2186386c 89
d7a25329
C
90 video.VideoFiles = []
91 }
94a5ff8a 92
d7a25329
C
93 return publishAndFederateIfNeeded(video)
94}
e8d246d5 95
d7a25329
C
96async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) {
97 await publishAndFederateIfNeeded(video)
09209296
C
98
99 await createHlsJobIfEnabled(payload)
40298b02
C
100}
101
453e83ea 102async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) {
56b13bd1 103 if (videoArg === undefined) return undefined
031094f7 104
2186386c 105 // Outside the transaction (IO on disk)
d7a25329 106 const { videoFileResolution } = await videoArg.getMaxQualityResolution()
2186386c 107
dc133480 108 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
2186386c 109 // Maybe the video changed in database, refresh it
a1587156 110 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
2186386c
C
111 // Video does not exist anymore
112 if (!videoDatabase) return undefined
113
114 // Create transcoding jobs if there are enabled resolutions
115 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
116 logger.info(
117 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
118 { resolutions: resolutionsEnabled }
119 )
120
dc133480
C
121 let videoPublished = false
122
d7a25329
C
123 const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getMaxQualityFile().resolution })
124 await createHlsJobIfEnabled(hlsPayload)
125
2186386c 126 if (resolutionsEnabled.length !== 0) {
2186386c 127 for (const resolution of resolutionsEnabled) {
d7a25329
C
128 let dataInput: VideoTranscodingPayload
129
130 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) {
131 dataInput = {
132 type: 'new-resolution' as 'new-resolution',
133 videoUUID: videoDatabase.uuid,
134 resolution
135 }
136 } else if (CONFIG.TRANSCODING.HLS.ENABLED) {
137 dataInput = {
138 type: 'hls',
139 videoUUID: videoDatabase.uuid,
140 resolution,
141 isPortraitMode: false,
142 copyCodecs: false
143 }
2186386c
C
144 }
145
a1587156 146 JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
2186386c 147 }
f5028693 148
2186386c
C
149 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
150 } else {
151 // No transcoding to do, it's now published
d7a25329 152 videoPublished = await videoDatabase.publishIfNeededAndSave(t)
a7977280 153
56b13bd1 154 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
f5028693 155 }
a7977280 156
09209296 157 await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t)
e8d246d5 158
dc133480 159 return { videoDatabase, videoPublished }
2186386c 160 })
e8d246d5 161
5b77537c 162 if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
7ccddd7b 163 if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
40298b02
C
164}
165
166// ---------------------------------------------------------------------------
167
168export {
a0327eed 169 processVideoTranscoding,
536598cf 170 publishNewResolutionIfNeeded
40298b02 171}
09209296
C
172
173// ---------------------------------------------------------------------------
174
536598cf 175function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) {
09209296
C
176 // Generate HLS playlist?
177 if (payload && CONFIG.TRANSCODING.HLS.ENABLED) {
178 const hlsTranscodingPayload = {
536598cf 179 type: 'hls' as 'hls',
09209296
C
180 videoUUID: payload.videoUUID,
181 resolution: payload.resolution,
d7a25329
C
182 isPortraitMode: payload.isPortraitMode,
183 copyCodecs: true
09209296
C
184 }
185
a0327eed 186 return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload })
09209296
C
187 }
188}
d7a25329
C
189
190async function publishAndFederateIfNeeded (video: MVideoUUID) {
191 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
192 // Maybe the video changed in database, refresh it
193 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
194 // Video does not exist anymore
195 if (!videoDatabase) return undefined
196
197 // We transcoded the video file in another format, now we can publish it
198 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
199
200 // If the video was not published, we consider it is a new one for other instances
201 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
202
203 return { videoDatabase, videoPublished }
204 })
205
206 if (videoPublished) {
207 Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
208 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
209 }
210}