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