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