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