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