]>
Commit | Line | Data |
---|---|---|
94831479 | 1 | import * as Bull from 'bull' |
f9a971c6 | 2 | import { VideoResolution, VideoState, Job } from '../../../../shared' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
3fd3ab2d | 4 | import { VideoModel } from '../../../models/video/video' |
94a5ff8a | 5 | import { JobQueue } from '../job-queue' |
2186386c C |
6 | import { federateVideoIfNeeded } from '../../activitypub' |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
8 | import { sequelizeTypescript } from '../../../initializers' | |
94831479 | 9 | import * as Bluebird from 'bluebird' |
06215f15 | 10 | import { computeResolutionsToTranscode } from '../../../helpers/ffmpeg-utils' |
edb4ffc7 | 11 | import { importVideoFile, transcodeOriginalVideofile, optimizeVideofile } from '../../video-transcoding' |
40298b02 | 12 | |
94a5ff8a C |
13 | export type VideoFilePayload = { |
14 | videoUUID: string | |
2186386c | 15 | isNewVideo?: boolean |
0c948c16 | 16 | resolution?: VideoResolution |
056aa7f2 | 17 | isPortraitMode?: boolean |
94a5ff8a C |
18 | } |
19 | ||
28be8916 | 20 | export type VideoFileImportPayload = { |
0138af92 FF |
21 | videoUUID: string, |
22 | filePath: string | |
23 | } | |
24 | ||
94831479 | 25 | async function processVideoFileImport (job: Bull.Job) { |
28be8916 C |
26 | const payload = job.data as VideoFileImportPayload |
27 | logger.info('Processing video file import in job %d.', job.id) | |
0138af92 | 28 | |
627621c1 | 29 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) |
0138af92 FF |
30 | // No video, maybe deleted? |
31 | if (!video) { | |
c1e791ba | 32 | logger.info('Do not process job %d, video does not exist.', job.id) |
0138af92 FF |
33 | return undefined |
34 | } | |
35 | ||
098eb377 | 36 | await importVideoFile(video, payload.filePath) |
0138af92 FF |
37 | |
38 | await onVideoFileTranscoderOrImportSuccess(video) | |
39 | return video | |
40 | } | |
41 | ||
94831479 | 42 | async function processVideoFile (job: Bull.Job) { |
94a5ff8a C |
43 | const payload = job.data as VideoFilePayload |
44 | logger.info('Processing video file in job %d.', job.id) | |
45 | ||
627621c1 | 46 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID) |
f5028693 C |
47 | // No video, maybe deleted? |
48 | if (!video) { | |
c1e791ba | 49 | logger.info('Do not process job %d, video does not exist.', job.id) |
f5028693 C |
50 | return undefined |
51 | } | |
52 | ||
94a5ff8a C |
53 | // Transcoding in other resolution |
54 | if (payload.resolution) { | |
098eb377 | 55 | await transcodeOriginalVideofile(video, payload.resolution, payload.isPortraitMode || false) |
2186386c | 56 | |
90d4bb81 | 57 | await retryTransactionWrapper(onVideoFileTranscoderOrImportSuccess, video) |
94a5ff8a | 58 | } else { |
edb4ffc7 | 59 | await optimizeVideofile(video) |
2186386c | 60 | |
90d4bb81 | 61 | await retryTransactionWrapper(onVideoFileOptimizerSuccess, video, payload.isNewVideo) |
94a5ff8a | 62 | } |
031094f7 | 63 | |
f5028693 | 64 | return video |
40298b02 C |
65 | } |
66 | ||
0138af92 | 67 | async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) { |
94a5ff8a C |
68 | if (video === undefined) return undefined |
69 | ||
2186386c C |
70 | return sequelizeTypescript.transaction(async t => { |
71 | // Maybe the video changed in database, refresh it | |
627621c1 | 72 | let videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
2186386c C |
73 | // Video does not exist anymore |
74 | if (!videoDatabase) return undefined | |
94a5ff8a | 75 | |
1b952dd4 C |
76 | let isNewVideo = false |
77 | ||
2186386c | 78 | // We transcoded the video file in another format, now we can publish it |
1b952dd4 C |
79 | if (videoDatabase.state !== VideoState.PUBLISHED) { |
80 | isNewVideo = true | |
81 | ||
82 | videoDatabase.state = VideoState.PUBLISHED | |
83 | videoDatabase.publishedAt = new Date() | |
84 | videoDatabase = await videoDatabase.save({ transaction: t }) | |
85 | } | |
2186386c C |
86 | |
87 | // If the video was not published, we consider it is a new one for other instances | |
2186386c | 88 | await federateVideoIfNeeded(videoDatabase, isNewVideo, t) |
94a5ff8a | 89 | |
2186386c C |
90 | return undefined |
91 | }) | |
40298b02 C |
92 | } |
93 | ||
0c948c16 | 94 | async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) { |
031094f7 C |
95 | if (video === undefined) return undefined |
96 | ||
2186386c C |
97 | // Outside the transaction (IO on disk) |
98 | const { videoFileResolution } = await video.getOriginalFileResolution() | |
99 | ||
100 | return sequelizeTypescript.transaction(async t => { | |
101 | // Maybe the video changed in database, refresh it | |
627621c1 | 102 | const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.uuid, t) |
2186386c C |
103 | // Video does not exist anymore |
104 | if (!videoDatabase) return undefined | |
105 | ||
106 | // Create transcoding jobs if there are enabled resolutions | |
107 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution) | |
108 | logger.info( | |
109 | 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution, | |
110 | { resolutions: resolutionsEnabled } | |
111 | ) | |
112 | ||
113 | if (resolutionsEnabled.length !== 0) { | |
f9a971c6 | 114 | const tasks: Bluebird<Bull.Job<any>>[] = [] |
2186386c C |
115 | |
116 | for (const resolution of resolutionsEnabled) { | |
117 | const dataInput = { | |
118 | videoUUID: videoDatabase.uuid, | |
119 | resolution | |
120 | } | |
121 | ||
122 | const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
123 | tasks.push(p) | |
124 | } | |
f5028693 | 125 | |
2186386c | 126 | await Promise.all(tasks) |
a7977280 | 127 | |
2186386c C |
128 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) |
129 | } else { | |
130 | // No transcoding to do, it's now published | |
131 | video.state = VideoState.PUBLISHED | |
132 | video = await video.save({ transaction: t }) | |
a7977280 | 133 | |
2186386c | 134 | logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid) |
f5028693 | 135 | } |
a7977280 | 136 | |
2186386c C |
137 | return federateVideoIfNeeded(video, isNewVideo, t) |
138 | }) | |
40298b02 C |
139 | } |
140 | ||
141 | // --------------------------------------------------------------------------- | |
142 | ||
143 | export { | |
0138af92 | 144 | processVideoFile, |
28be8916 | 145 | processVideoFileImport |
40298b02 | 146 | } |