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