]>
Commit | Line | Data |
---|---|---|
94a5ff8a C |
1 | import * as kue from 'kue' |
2 | import { VideoResolution } from '../../../../shared' | |
e12a0092 | 3 | import { VideoPrivacy } from '../../../../shared/models/videos' |
da854ddd C |
4 | import { logger } from '../../../helpers/logger' |
5 | import { computeResolutionsToTranscode } from '../../../helpers/utils' | |
3fd3ab2d C |
6 | import { sequelizeTypescript } from '../../../initializers' |
7 | import { VideoModel } from '../../../models/video/video' | |
a7d647c4 | 8 | import { shareVideoByServerAndChannel } from '../../activitypub' |
94a5ff8a C |
9 | import { sendCreateVideo, sendUpdateVideo } from '../../activitypub/send' |
10 | import { JobQueue } from '../job-queue' | |
40298b02 | 11 | |
94a5ff8a C |
12 | export type VideoFilePayload = { |
13 | videoUUID: string | |
0c948c16 C |
14 | isNewVideo: boolean |
15 | resolution?: VideoResolution | |
056aa7f2 | 16 | isPortraitMode?: boolean |
94a5ff8a C |
17 | } |
18 | ||
28be8916 | 19 | export type VideoFileImportPayload = { |
0138af92 FF |
20 | videoUUID: string, |
21 | filePath: string | |
22 | } | |
23 | ||
28be8916 C |
24 | async function processVideoFileImport (job: kue.Job) { |
25 | const payload = job.data as VideoFileImportPayload | |
26 | logger.info('Processing video file import in job %d.', job.id) | |
0138af92 FF |
27 | |
28 | const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
29 | // No video, maybe deleted? | |
30 | if (!video) { | |
31 | logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid }) | |
32 | return undefined | |
33 | } | |
34 | ||
35 | await video.importVideoFile(payload.filePath) | |
36 | ||
37 | await onVideoFileTranscoderOrImportSuccess(video) | |
38 | return video | |
39 | } | |
40 | ||
94a5ff8a C |
41 | async function processVideoFile (job: kue.Job) { |
42 | const payload = job.data as VideoFilePayload | |
43 | logger.info('Processing video file in job %d.', job.id) | |
44 | ||
45 | const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(payload.videoUUID) | |
f5028693 C |
46 | // No video, maybe deleted? |
47 | if (!video) { | |
94a5ff8a | 48 | logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid }) |
f5028693 C |
49 | return undefined |
50 | } | |
51 | ||
94a5ff8a C |
52 | // Transcoding in other resolution |
53 | if (payload.resolution) { | |
056aa7f2 | 54 | await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode) |
0138af92 | 55 | await onVideoFileTranscoderOrImportSuccess(video) |
94a5ff8a C |
56 | } else { |
57 | await video.optimizeOriginalVideofile() | |
0c948c16 | 58 | await onVideoFileOptimizerSuccess(video, payload.isNewVideo) |
94a5ff8a | 59 | } |
031094f7 | 60 | |
f5028693 | 61 | return video |
40298b02 C |
62 | } |
63 | ||
0138af92 | 64 | async function onVideoFileTranscoderOrImportSuccess (video: VideoModel) { |
94a5ff8a C |
65 | if (video === undefined) return undefined |
66 | ||
67 | // Maybe the video changed in database, refresh it | |
68 | const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) | |
69 | // Video does not exist anymore | |
70 | if (!videoDatabase) return undefined | |
71 | ||
72 | if (video.privacy !== VideoPrivacy.PRIVATE) { | |
73 | await sendUpdateVideo(video, undefined) | |
74 | } | |
75 | ||
76 | return undefined | |
40298b02 C |
77 | } |
78 | ||
0c948c16 | 79 | async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) { |
031094f7 C |
80 | if (video === undefined) return undefined |
81 | ||
4077df72 | 82 | // Maybe the video changed in database, refresh it |
3fd3ab2d | 83 | const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid) |
4077df72 C |
84 | // Video does not exist anymore |
85 | if (!videoDatabase) return undefined | |
86 | ||
e12a0092 | 87 | if (video.privacy !== VideoPrivacy.PRIVATE) { |
8eb8bc20 | 88 | if (isNewVideo !== false) { |
0c948c16 C |
89 | // Now we'll add the video's meta data to our followers |
90 | await sequelizeTypescript.transaction(async t => { | |
91 | await sendCreateVideo(video, t) | |
92 | await shareVideoByServerAndChannel(video, t) | |
93 | }) | |
94 | } else { | |
95 | await sendUpdateVideo(video, undefined) | |
96 | } | |
e12a0092 | 97 | } |
f5028693 | 98 | |
056aa7f2 | 99 | const { videoFileResolution } = await videoDatabase.getOriginalFileResolution() |
f5028693 | 100 | |
8e10cf1a | 101 | // Create transcoding jobs if there are enabled resolutions |
056aa7f2 | 102 | const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution) |
f5028693 | 103 | logger.info( |
056aa7f2 | 104 | 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution, |
f5028693 C |
105 | { resolutions: resolutionsEnabled } |
106 | ) | |
107 | ||
108 | if (resolutionsEnabled.length !== 0) { | |
a7977280 C |
109 | const tasks: Promise<any>[] = [] |
110 | ||
111 | for (const resolution of resolutionsEnabled) { | |
112 | const dataInput = { | |
113 | videoUUID: videoDatabase.uuid, | |
0c948c16 C |
114 | resolution, |
115 | isNewVideo | |
a7977280 C |
116 | } |
117 | ||
118 | const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
119 | tasks.push(p) | |
f5028693 | 120 | } |
a7977280 C |
121 | |
122 | await Promise.all(tasks) | |
123 | ||
124 | logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled }) | |
f5028693 C |
125 | } else { |
126 | logger.info('No transcoding jobs created for video %s (no resolutions enabled).') | |
127 | return undefined | |
128 | } | |
40298b02 C |
129 | } |
130 | ||
131 | // --------------------------------------------------------------------------- | |
132 | ||
133 | export { | |
0138af92 | 134 | processVideoFile, |
28be8916 | 135 | processVideoFileImport |
40298b02 | 136 | } |