1 import * as Bull from 'bull'
2 import { VideoResolution, VideoState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { VideoModel } from '../../../models/video/video'
5 import { JobQueue } from '../job-queue'
6 import { federateVideoIfNeeded } from '../../activitypub'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { CONFIG, sequelizeTypescript } from '../../../initializers'
9 import * as Bluebird from 'bluebird'
10 import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
11 import { generateHlsPlaylist, optimizeVideofile, transcodeOriginalVideofile } from '../../video-transcoding'
12 import { Notifier } from '../../notifier'
14 export type VideoTranscodingPayload = {
16 resolution?: VideoResolution
18 isPortraitMode?: boolean
19 generateHlsPlaylist?: boolean
22 async function processVideoTranscoding (job: Bull.Job) {
23 const payload = job.data as VideoTranscodingPayload
24 logger.info('Processing video file in job %d.', job.id)
26 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
27 // No video, maybe deleted?
29 logger.info('Do not process job %d, video does not exist.', job.id)
33 if (payload.generateHlsPlaylist) {
34 await generateHlsPlaylist(video, payload.resolution, payload.isPortraitMode || false)
36 await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video)
37 } else if (payload.resolution) { // Transcoding in other resolution
38 await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false)
40 await retryTransactionWrapper(publishVideoIfNeeded, video, payload)
42 await optimizeVideofile(video)
44 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload)
50 async function onHlsPlaylistGenerationSuccess (video: VideoModel) {
51 if (video === undefined) return undefined
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
59 // If the video was not published, we consider it is a new one for other instances
60 await federateVideoIfNeeded(videoDatabase, false, t)
64 async function publishVideoIfNeeded (video: VideoModel, payload?: VideoTranscodingPayload) {
65 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
66 // Maybe the video changed in database, refresh it
67 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
68 // Video does not exist anymore
69 if (!videoDatabase) return undefined
71 let videoPublished = false
73 // We transcoded the video file in another format, now we can publish it
74 if (videoDatabase.state !== VideoState.PUBLISHED) {
77 videoDatabase.state = VideoState.PUBLISHED
78 videoDatabase.publishedAt = new Date()
79 videoDatabase = await videoDatabase.save({ transaction: t })
82 // If the video was not published, we consider it is a new one for other instances
83 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
85 return { videoDatabase, videoPublished }
88 // don't notify prior to scheduled video update
89 if (videoPublished && !videoDatabase.ScheduleVideoUpdate) {
90 Notifier.Instance.notifyOnNewVideo(videoDatabase)
91 Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
94 await createHlsJobIfEnabled(payload)
97 async function onVideoFileOptimizerSuccess (videoArg: VideoModel, payload: VideoTranscodingPayload) {
98 if (videoArg === undefined) return undefined
100 // Outside the transaction (IO on disk)
101 const { videoFileResolution } = await videoArg.getOriginalFileResolution()
103 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
104 // Maybe the video changed in database, refresh it
105 let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
106 // Video does not exist anymore
107 if (!videoDatabase) return undefined
109 // Create transcoding jobs if there are enabled resolutions
110 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
112 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
113 { resolutions: resolutionsEnabled }
116 let videoPublished = false
118 if (resolutionsEnabled.length !== 0) {
119 const tasks: (Bluebird<Bull.Job<any>> | Promise<Bull.Job<any>>)[] = []
121 for (const resolution of resolutionsEnabled) {
123 videoUUID: videoDatabase.uuid,
127 const p = JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
131 await Promise.all(tasks)
133 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
135 videoPublished = true
137 // No transcoding to do, it's now published
138 videoDatabase.state = VideoState.PUBLISHED
139 videoDatabase = await videoDatabase.save({ transaction: t })
141 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
144 await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t)
146 return { videoDatabase, videoPublished }
149 // don't notify prior to scheduled video update
150 if (!videoDatabase.ScheduleVideoUpdate) {
151 if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideo(videoDatabase)
152 if (videoPublished) Notifier.Instance.notifyOnPendingVideoPublished(videoDatabase)
155 await createHlsJobIfEnabled(Object.assign({}, payload, { resolution: videoDatabase.getOriginalFile().resolution }))
158 // ---------------------------------------------------------------------------
161 processVideoTranscoding,
165 // ---------------------------------------------------------------------------
167 function createHlsJobIfEnabled (payload?: VideoTranscodingPayload) {
168 // Generate HLS playlist?
169 if (payload && CONFIG.TRANSCODING.HLS.ENABLED) {
170 const hlsTranscodingPayload = {
171 videoUUID: payload.videoUUID,
172 resolution: payload.resolution,
173 isPortraitMode: payload.isPortraitMode,
175 generateHlsPlaylist: true
178 return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload })