diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-02 12:20:26 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-03 15:31:26 +0200 |
commit | 40298b02546e8225dd21bf6048fe7f224aefc32a (patch) | |
tree | 0a0b981dbeb2af47810adff6553a0df995a03734 /server/lib/jobs/handlers/video-file-optimizer.ts | |
parent | f0adb2701c1cf404ff63095f71e542bfe6d025ae (diff) | |
download | PeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.tar.gz PeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.tar.zst PeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.zip |
Implement video transcoding on server side
Diffstat (limited to 'server/lib/jobs/handlers/video-file-optimizer.ts')
-rw-r--r-- | server/lib/jobs/handlers/video-file-optimizer.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/server/lib/jobs/handlers/video-file-optimizer.ts b/server/lib/jobs/handlers/video-file-optimizer.ts new file mode 100644 index 000000000..a87ce52dc --- /dev/null +++ b/server/lib/jobs/handlers/video-file-optimizer.ts | |||
@@ -0,0 +1,78 @@ | |||
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 | |||
9 | function process (data: { videoUUID: string }) { | ||
10 | return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => { | ||
11 | return video.optimizeOriginalVideofile().then(() => video) | ||
12 | }) | ||
13 | } | ||
14 | |||
15 | function onError (err: Error, jobId: number) { | ||
16 | logger.error('Error when optimized video file in job %d.', jobId, err) | ||
17 | return Promise.resolve() | ||
18 | } | ||
19 | |||
20 | function onSuccess (jobId: number, video: VideoInstance) { | ||
21 | logger.info('Job %d is a success.', jobId) | ||
22 | |||
23 | video.toAddRemoteJSON() | ||
24 | .then(remoteVideo => { | ||
25 | // Now we'll add the video's meta data to our friends | ||
26 | return addVideoToFriends(remoteVideo, null) | ||
27 | }) | ||
28 | .then(() => { | ||
29 | return video.getOriginalFileHeight() | ||
30 | }) | ||
31 | .then(originalFileHeight => { | ||
32 | // Create transcoding jobs if there are enabled resolutions | ||
33 | const resolutionsEnabled = computeResolutionsToTranscode(originalFileHeight) | ||
34 | logger.info( | ||
35 | 'Resolutions computed for video %s and origin file height of %d.', video.uuid, originalFileHeight, | ||
36 | { resolutions: resolutionsEnabled } | ||
37 | ) | ||
38 | |||
39 | if (resolutionsEnabled.length === 0) return undefined | ||
40 | |||
41 | return db.sequelize.transaction(t => { | ||
42 | const tasks: Promise<any>[] = [] | ||
43 | |||
44 | resolutionsEnabled.forEach(resolution => { | ||
45 | const dataInput = { | ||
46 | videoUUID: video.uuid, | ||
47 | resolution | ||
48 | } | ||
49 | |||
50 | const p = JobScheduler.Instance.createJob(t, 'videoFileTranscoder', dataInput) | ||
51 | tasks.push(p) | ||
52 | }) | ||
53 | |||
54 | return Promise.all(tasks).then(() => resolutionsEnabled) | ||
55 | }) | ||
56 | }) | ||
57 | .then(resolutionsEnabled => { | ||
58 | if (resolutionsEnabled === undefined) { | ||
59 | logger.info('No transcoding jobs created for video %s (no resolutions enabled).') | ||
60 | return undefined | ||
61 | } | ||
62 | |||
63 | logger.info('Transcoding jobs created for uuid %s.', video.uuid, { resolutionsEnabled }) | ||
64 | }) | ||
65 | .catch((err: Error) => { | ||
66 | logger.debug('Cannot transcode the video.', err) | ||
67 | throw err | ||
68 | }) | ||
69 | |||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | export { | ||
75 | process, | ||
76 | onError, | ||
77 | onSuccess | ||
78 | } | ||