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