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