1 import * as Bluebird from 'bluebird'
2 import { computeResolutionsToTranscode, logger } from '../../../helpers'
3 import { database as db } from '../../../initializers/database'
4 import { VideoInstance } from '../../../models'
5 import { sendAddVideo } from '../../activitypub/send/send-add'
6 import { JobScheduler } from '../job-scheduler'
7 import { TranscodingJobPayload } from './transcoding-job-scheduler'
8 import { shareVideoByServer } from '../../activitypub/share'
10 async function process (data: TranscodingJobPayload, jobId: number) {
11 const video = await db.Video.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: VideoInstance, jobScheduler: JobScheduler<TranscodingJobPayload, VideoInstance>) {
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 db.Video.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 sendAddVideo(video, undefined)
40 await shareVideoByServer(video, undefined)
42 const originalFileHeight = await videoDatabase.getOriginalFileHeight()
44 // Create transcoding jobs if there are enabled resolutions
45 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
47 'Resolutions computed for video %s and origin file height of %d.', videoDatabase.uuid, originalFileHeight,
48 { resolutions: resolutionsEnabled }
51 if (resolutionsEnabled.length !== 0) {
53 await db.sequelize.transaction(async t => {
54 const tasks: Bluebird<any>[] = []
56 for (const resolution of resolutionsEnabled) {
58 videoUUID: videoDatabase.uuid,
62 const p = jobScheduler.createJob(t, 'videoFileTranscoder', dataInput)
66 await Promise.all(tasks)
69 logger.info('Transcoding jobs created for uuid %s.', videoDatabase.uuid, { resolutionsEnabled })
71 logger.warn('Cannot transcode the video.', err)
74 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
79 // ---------------------------------------------------------------------------