1 import * as Bluebird from 'bluebird'
2 import { computeResolutionsToTranscode, logger } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { VideoModel } from '../../../models/video/video'
5 import { shareVideoByServer } from '../../activitypub'
6 import { sendCreateVideo } from '../../activitypub/send'
7 import { JobScheduler } from '../job-scheduler'
8 import { TranscodingJobPayload } from './transcoding-job-scheduler'
10 async function process (data: TranscodingJobPayload, jobId: number) {
11 const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(data.videoUUID)
12 // No video, maybe deleted?
14 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
18 await video.optimizeOriginalVideofile()
23 function onError (err: Error, jobId: number) {
24 logger.error('Error when optimized video file in job %d.', jobId, err)
25 return Promise.resolve()
28 async function onSuccess (jobId: number, video: VideoModel, jobScheduler: JobScheduler<TranscodingJobPayload, VideoModel>) {
29 if (video === undefined) return undefined
31 logger.info('Job %d is a success.', jobId)
33 // Maybe the video changed in database, refresh it
34 const videoDatabase = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(video.uuid)
35 // Video does not exist anymore
36 if (!videoDatabase) return undefined
38 // Now we'll add the video's meta data to our followers
39 await sendCreateVideo(video, undefined)
40 // TODO: share by channel
41 await shareVideoByServer(video, undefined)
43 const originalFileHeight = await videoDatabase.getOriginalFileHeight()
45 // Create transcoding jobs if there are enabled resolutions
46 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
48 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight,
49 { resolutions: resolutionsEnabled }
52 if (resolutionsEnabled.length !== 0) {
54 await sequelizeTypescript.transaction(async t => {
55 const tasks: Bluebird<any>[] = []
57 for (const resolution of resolutionsEnabled) {
59 videoUUID: videoDatabase.uuid,
63 const p = jobScheduler.createJob(t, 'videoFileTranscoder', dataInput)
67 await Promise.all(tasks)
70 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
72 logger.warn('Cannot transcode the video.', err)
75 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
80 // ---------------------------------------------------------------------------