1 import * as Bluebird from 'bluebird'
3 import { database as db } from '../../../initializers/database'
4 import { logger, computeResolutionsToTranscode } from '../../../helpers'
5 import { VideoInstance } from '../../../models'
6 import { addVideoToFriends } from '../../friends'
7 import { JobScheduler } from '../job-scheduler'
9 async function process (data: { videoUUID: string }, jobId: number) {
10 const video = await db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID)
11 // No video, maybe deleted?
13 logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid })
17 await video.optimizeOriginalVideofile()
22 function onError (err: Error, jobId: number) {
23 logger.error('Error when optimized video file in job %d.', jobId, err)
24 return Promise.resolve()
27 async function onSuccess (jobId: number, video: VideoInstance) {
28 if (video === undefined) return undefined
30 logger.info('Job %d is a success.', jobId)
32 const remoteVideo = await video.toAddRemoteJSON()
34 // Now we'll add the video's meta data to our friends
35 await addVideoToFriends(remoteVideo, null)
37 const originalFileHeight = await video.getOriginalFileHeight()
38 // Create transcoding jobs if there are enabled resolutions
40 const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight)
42 'Resolutions computed for video %s and origin file height of %d.', video.uuid, originalFileHeight,
43 { resolutions: resolutionsEnabled }
46 if (resolutionsEnabled.length !== 0) {
48 await db.sequelize.transaction(async t => {
49 const tasks: Bluebird<any>[] = []
51 for (const resolution of resolutionsEnabled) {
53 videoUUID: video.uuid,
57 const p = JobScheduler.Instance.createJob(t, 'videoFileTranscoder', dataInput)
61 await Promise.all(tasks)
64 logger.info('Transcoding jobs created for uuid %s.', video.uuid, { resolutionsEnabled })
66 logger.warn('Cannot transcode the video.', err)
69 logger.info('No transcoding jobs created for video %s (no resolutions enabled).')
74 // ---------------------------------------------------------------------------