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