]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-file.ts
Add dev doc about localization
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
CommitLineData
94a5ff8a
C
1import * as kue from 'kue'
2import { VideoResolution } from '../../../../shared'
e12a0092 3import { VideoPrivacy } from '../../../../shared/models/videos'
da854ddd
C
4import { logger } from '../../../helpers/logger'
5import { computeResolutionsToTranscode } from '../../../helpers/utils'
3fd3ab2d
C
6import { sequelizeTypescript } from '../../../initializers'
7import { VideoModel } from '../../../models/video/video'
a7d647c4 8import { shareVideoByServerAndChannel } from '../../activitypub'
94a5ff8a
C
9import { sendCreateVideo, sendUpdateVideo } from '../../activitypub/send'
10import { JobQueue } from '../job-queue'
40298b02 11
94a5ff8a
C
12export type VideoFilePayload = {
13 videoUUID: string
0c948c16
C
14 isNewVideo: boolean
15 resolution?: VideoResolution
056aa7f2 16 isPortraitMode?: boolean
94a5ff8a
C
17}
18
19async 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)
f5028693
C
24 // No video, maybe deleted?
25 if (!video) {
94a5ff8a 26 logger.info('Do not process job %d, video does not exist.', job.id, { videoUUID: video.uuid })
f5028693
C
27 return undefined
28 }
29
94a5ff8a
C
30 // Transcoding in other resolution
31 if (payload.resolution) {
056aa7f2 32 await video.transcodeOriginalVideofile(payload.resolution, payload.isPortraitMode)
94a5ff8a
C
33 await onVideoFileTranscoderSuccess(video)
34 } else {
35 await video.optimizeOriginalVideofile()
0c948c16 36 await onVideoFileOptimizerSuccess(video, payload.isNewVideo)
94a5ff8a 37 }
031094f7 38
f5028693 39 return video
40298b02
C
40}
41
94a5ff8a
C
42async 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
40298b02
C
55}
56
0c948c16 57async function onVideoFileOptimizerSuccess (video: VideoModel, isNewVideo: boolean) {
031094f7
C
58 if (video === undefined) return undefined
59
4077df72 60 // Maybe the video changed in database, refresh it
3fd3ab2d 61 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
4077df72
C
62 // Video does not exist anymore
63 if (!videoDatabase) return undefined
64
e12a0092 65 if (video.privacy !== VideoPrivacy.PRIVATE) {
0c948c16
C
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 }
e12a0092 75 }
f5028693 76
056aa7f2 77 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
f5028693 78
8e10cf1a 79 // Create transcoding jobs if there are enabled resolutions
056aa7f2 80 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
f5028693 81 logger.info(
056aa7f2 82 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
f5028693
C
83 { resolutions: resolutionsEnabled }
84 )
85
86 if (resolutionsEnabled.length !== 0) {
a7977280
C
87 const tasks: Promise<any>[] = []
88
89 for (const resolution of resolutionsEnabled) {
90 const dataInput = {
91 videoUUID: videoDatabase.uuid,
0c948c16
C
92 resolution,
93 isNewVideo
a7977280
C
94 }
95
96 const p = JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
97 tasks.push(p)
f5028693 98 }
a7977280
C
99
100 await Promise.all(tasks)
101
102 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
f5028693
C
103 } else {
104 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
105 return undefined
106 }
40298b02
C
107}
108
109// ---------------------------------------------------------------------------
110
111export {
94a5ff8a 112 processVideoFile
40298b02 113}