]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-file.ts
Fix resolution for portrait videos
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-file.ts
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 sendCreateVideo(video, undefined)
67 await shareVideoByServerAndChannel(video, undefined)
68 }
69
70 const { videoFileResolution } = await videoDatabase.getOriginalFileResolution()
71
72 // Create transcoding jobs if there are enabled resolutions
73 const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution)
74 logger.info(
75 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, videoFileResolution,
76 { resolutions: resolutionsEnabled }
77 )
78
79 if (resolutionsEnabled.length !== 0) {
80 try {
81 await sequelizeTypescript.transaction(async t => {
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
97 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
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 }
105 }
106
107 // ---------------------------------------------------------------------------
108
109 export {
110 processVideoFile
111 }