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