]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/jobs/transcoding-job-scheduler/video-file-optimizer-handler.ts
Send server announce when users upload a video
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / transcoding-job-scheduler / video-file-optimizer-handler.ts
1 import * as Bluebird from 'bluebird'
2 import { computeResolutionsToTranscode, logger } from '../../../helpers'
3
4 import { database as db } from '../../../initializers/database'
5 import { VideoInstance } from '../../../models'
6 import { sendAddVideo } from '../../activitypub/send-request'
7 import { JobScheduler } from '../job-scheduler'
8 import { TranscodingJobPayload } from './transcoding-job-scheduler'
9 import { shareVideoByServer } from '../../../helpers/activitypub'
10
11 async function process (data: TranscodingJobPayload, jobId: number) {
12 const video = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
13 // No video, maybe deleted?
14 if (!video) {
15 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
16 return undefined
17 }
18
19 await video.optimizeOriginalVideofile()
20
21 return video
22 }
23
24 function onError (err: Error, jobId: number) {
25 logger.error('Error when optimized video file in job %d.', jobId, err)
26 return Promise.resolve()
27 }
28
29 async function onSuccess (jobId: number, video: VideoInstance, jobScheduler: JobScheduler<TranscodingJobPayload, VideoInstance>) {
30 if (video === undefined) return undefined
31
32 logger.info('Job %d is a success.', jobId)
33
34 // Maybe the video changed in database, refresh it
35 const videoDatabase = await db.Video.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
36 // Video does not exist anymore
37 if (!videoDatabase) return undefined
38
39 // Now we'll add the video's meta data to our followers
40 await sendAddVideo(video, undefined)
41 await shareVideoByServer(video, undefined)
42
43 const originalFileHeight = await videoDatabase.getOriginalFileHeight()
44
45 // Create transcoding jobs if there are enabled resolutions
46 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
47 logger.info(
48 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight,
49 { resolutions: resolutionsEnabled }
50 )
51
52 if (resolutionsEnabled.length !== 0) {
53 try {
54 await db.sequelize.transaction(async t => {
55 const tasks: Bluebird<any>[] = []
56
57 for (const resolution of resolutionsEnabled) {
58 const dataInput = {
59 videoUUID: videoDatabase.uuid,
60 resolution
61 }
62
63 const p = jobScheduler.createJob(t, 'videoFileTranscoder', dataInput)
64 tasks.push(p)
65 }
66
67 await Promise.all(tasks)
68 })
69
70 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
71 } catch (err) {
72 logger.warn('Cannot transcode the video.', err)
73 }
74 } else {
75 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
76 return undefined
77 }
78 }
79
80 // ---------------------------------------------------------------------------
81
82 export {
83 process,
84 onError,
85 onSuccess
86 }