]>
Commit | Line | Data |
---|---|---|
40298b02 C |
1 | import * as Promise from 'bluebird' |
2 | ||
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' | |
8 | ||
031094f7 | 9 | function process (data: { videoUUID: string }, jobId: number) { |
40298b02 | 10 | return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => { |
031094f7 C |
11 | // No video, maybe deleted? |
12 | if (!video) { | |
13 | logger.info('Do not process job %d, video does not exist.', jobId, { videoUUID: video.uuid }) | |
14 | return undefined | |
15 | } | |
16 | ||
40298b02 C |
17 | return video.optimizeOriginalVideofile().then(() => video) |
18 | }) | |
19 | } | |
20 | ||
21 | function onError (err: Error, jobId: number) { | |
22 | logger.error('Error when optimized video file in job %d.', jobId, err) | |
23 | return Promise.resolve() | |
24 | } | |
25 | ||
26 | function onSuccess (jobId: number, video: VideoInstance) { | |
031094f7 C |
27 | if (video === undefined) return undefined |
28 | ||
40298b02 C |
29 | logger.info('Job %d is a success.', jobId) |
30 | ||
31 | video.toAddRemoteJSON() | |
32 | .then(remoteVideo => { | |
33 | // Now we'll add the video's meta data to our friends | |
34 | return addVideoToFriends(remoteVideo, null) | |
35 | }) | |
36 | .then(() => { | |
37 | return video.getOriginalFileHeight() | |
38 | }) | |
39 | .then(originalFileHeight => { | |
40 | // Create transcoding jobs if there are enabled resolutions | |
41 | const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight) | |
42 | logger.info( | |
43 | 'Resolutions computed for video %s and origin file height of %d.', video.uuid, originalFileHeight, | |
44 | { resolutions: resolutionsEnabled } | |
45 | ) | |
46 | ||
47 | if (resolutionsEnabled.length === 0) return undefined | |
48 | ||
49 | return db.sequelize.transaction(t => { | |
50 | const tasks: Promise<any>[] = [] | |
51 | ||
52 | resolutionsEnabled.forEach(resolution => { | |
53 | const dataInput = { | |
54 | videoUUID: video.uuid, | |
55 | resolution | |
56 | } | |
57 | ||
58 | const p = JobScheduler.Instance.createJob(t, 'videoFileTranscoder', dataInput) | |
59 | tasks.push(p) | |
60 | }) | |
61 | ||
62 | return Promise.all(tasks).then(() => resolutionsEnabled) | |
63 | }) | |
64 | }) | |
65 | .then(resolutionsEnabled => { | |
66 | if (resolutionsEnabled === undefined) { | |
67 | logger.info('No transcoding jobs created for video %s (no resolutions enabled).') | |
68 | return undefined | |
69 | } | |
70 | ||
71 | logger.info('Transcoding jobs created for uuid %s.', video.uuid, { resolutionsEnabled }) | |
72 | }) | |
73 | .catch((err: Error) => { | |
74 | logger.debug('Cannot transcode the video.', err) | |
75 | throw err | |
76 | }) | |
77 | ||
78 | } | |
79 | ||
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | export { | |
83 | process, | |
84 | onError, | |
85 | onSuccess | |
86 | } |