aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/jobs/handlers/video-file-optimizer.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-02 12:20:26 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-03 15:31:26 +0200
commit40298b02546e8225dd21bf6048fe7f224aefc32a (patch)
tree0a0b981dbeb2af47810adff6553a0df995a03734 /server/lib/jobs/handlers/video-file-optimizer.ts
parentf0adb2701c1cf404ff63095f71e542bfe6d025ae (diff)
downloadPeerTube-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.ts78
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 @@
1import * as Promise from 'bluebird'
2
3import { database as db } from '../../../initializers/database'
4import { logger, computeResolutionsToTranscode } from '../../../helpers'
5import { VideoInstance } from '../../../models'
6import { addVideoToFriends } from '../../friends'
7import { JobScheduler } from '../job-scheduler'
8
9function process (data: { videoUUID: string }) {
10 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => {
11 return video.optimizeOriginalVideofile().then(() => video)
12 })
13}
14
15function onError (err: Error, jobId: number) {
16 logger.error('Error when optimized video file in job %d.', jobId, err)
17 return Promise.resolve()
18}
19
20function 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
74export {
75 process,
76 onError,
77 onSuccess
78}