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