1 import * as Bull from 'bull'
2 import { getVideoFilePath } from '@server/lib/video-paths'
3 import { MVideoFullLight, MVideoUUID, MVideoWithFile } from '@server/types/models'
5 MergeAudioTranscodingPayload,
6 NewResolutionTranscodingPayload,
7 OptimizeTranscodingPayload,
8 VideoTranscodingPayload
9 } from '../../../../shared'
10 import { retryTransactionWrapper } from '../../../helpers/database-utils'
11 import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils'
12 import { logger } from '../../../helpers/logger'
13 import { CONFIG } from '../../../initializers/config'
14 import { sequelizeTypescript } from '../../../initializers/database'
15 import { VideoModel } from '../../../models/video/video'
16 import { federateVideoIfNeeded } from '../../activitypub/videos'
17 import { Notifier } from '../../notifier'
18 import { generateHlsPlaylist, mergeAudioVideofile, optimizeOriginalVideofile, transcodeNewResolution } from '../../video-transcoding'
19 import { JobQueue } from '../job-queue'
21 async function processVideoTranscoding (job: Bull.Job) {
22 const payload = job.data as VideoTranscodingPayload
23 logger.info('Processing video file in job %d.', job.id)
25 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
26 // No video, maybe deleted?
28 logger.info('Do not process job %d, video does not exist.', job.id)
32 if (payload.type === 'hls') {
33 const videoFileInput = payload.copyCodecs
34 ? video.getWebTorrentFile(payload.resolution)
35 : video.getMaxQualityFile()
37 const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
38 const videoInputPath = getVideoFilePath(videoOrStreamingPlaylist, videoFileInput)
40 await generateHlsPlaylist({
43 resolution: payload.resolution,
44 copyCodecs: payload.copyCodecs,
45 isPortraitMode: payload.isPortraitMode || false
48 await retryTransactionWrapper(onHlsPlaylistGenerationSuccess, video)
49 } else if (payload.type === 'new-resolution') {
50 await transcodeNewResolution(video, payload.resolution, payload.isPortraitMode || false)
52 await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload)
53 } else if (payload.type === 'merge-audio') {
54 await mergeAudioVideofile(video, payload.resolution)
56 await retryTransactionWrapper(publishNewResolutionIfNeeded, video, payload)
58 await optimizeOriginalVideofile(video)
60 await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload)
66 async function onHlsPlaylistGenerationSuccess (video: MVideoFullLight) {
67 if (video === undefined) return undefined
69 // We generated the HLS playlist, we don't need the webtorrent files anymore if the admin disabled it
70 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
71 for (const file of video.VideoFiles) {
72 await video.removeFile(file)
79 return publishAndFederateIfNeeded(video)
82 async function publishNewResolutionIfNeeded (video: MVideoUUID, payload?: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload) {
83 await publishAndFederateIfNeeded(video)
85 await createHlsJobIfEnabled(payload)
88 async function onVideoFileOptimizerSuccess (videoArg: MVideoWithFile, payload: OptimizeTranscodingPayload) {
89 if (videoArg === undefined) return undefined
91 // Outside the transaction (IO on disk)
92 const { videoFileResolution, isPortraitMode } = await videoArg.getMaxQualityResolution()
94 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
95 // Maybe the video changed in database, refresh it
96 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid, t)
97 // Video does not exist anymore
98 if (!videoDatabase) return undefined
100 // Create transcoding jobs if there are enabled resolutions
101 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod')
103 'Resolutions computed for video %s and origin file resolution of %d.', videoDatabase.uuid, videoFileResolution,
104 { resolutions: resolutionsEnabled }
107 let videoPublished = false
109 // Generate HLS version of the max quality file
110 const hlsPayload = Object.assign({}, payload, { resolution: videoDatabase.getMaxQualityFile().resolution })
111 await createHlsJobIfEnabled(hlsPayload)
113 if (resolutionsEnabled.length !== 0) {
114 for (const resolution of resolutionsEnabled) {
115 let dataInput: VideoTranscodingPayload
117 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED) {
119 type: 'new-resolution' as 'new-resolution',
120 videoUUID: videoDatabase.uuid,
124 } else if (CONFIG.TRANSCODING.HLS.ENABLED) {
127 videoUUID: videoDatabase.uuid,
134 JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
137 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
139 // No transcoding to do, it's now published
140 videoPublished = await videoDatabase.publishIfNeededAndSave(t)
142 logger.info('No transcoding jobs created for video %s (no resolutions).', videoDatabase.uuid, { privacy: videoDatabase.privacy })
145 await federateVideoIfNeeded(videoDatabase, payload.isNewVideo, t)
147 return { videoDatabase, videoPublished }
150 if (payload.isNewVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
151 if (videoPublished) Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)
154 // ---------------------------------------------------------------------------
157 processVideoTranscoding,
158 publishNewResolutionIfNeeded
161 // ---------------------------------------------------------------------------
163 function createHlsJobIfEnabled (payload?: { videoUUID: string, resolution: number, isPortraitMode?: boolean }) {
164 // Generate HLS playlist?
165 if (payload && CONFIG.TRANSCODING.HLS.ENABLED) {
166 const hlsTranscodingPayload = {
167 type: 'hls' as 'hls',
168 videoUUID: payload.videoUUID,
169 resolution: payload.resolution,
170 isPortraitMode: payload.isPortraitMode,
174 return JobQueue.Instance.createJob({ type: 'video-transcoding', payload: hlsTranscodingPayload })
178 async function publishAndFederateIfNeeded (video: MVideoUUID) {
179 const { videoDatabase, videoPublished } = await sequelizeTypescript.transaction(async t => {
180 // Maybe the video changed in database, refresh it
181 const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t)
182 // Video does not exist anymore
183 if (!videoDatabase) return undefined
185 // We transcoded the video file in another format, now we can publish it
186 const videoPublished = await videoDatabase.publishIfNeededAndSave(t)
188 // If the video was not published, we consider it is a new one for other instances
189 await federateVideoIfNeeded(videoDatabase, videoPublished, t)
191 return { videoDatabase, videoPublished }
194 if (videoPublished) {
195 Notifier.Instance.notifyOnNewVideoIfNeeded(videoDatabase)
196 Notifier.Instance.notifyOnVideoPublishedAfterTranscoding(videoDatabase)