aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
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
parentf0adb2701c1cf404ff63095f71e542bfe6d025ae (diff)
downloadPeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.tar.gz
PeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.tar.zst
PeerTube-40298b02546e8225dd21bf6048fe7f224aefc32a.zip
Implement video transcoding on server side
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/jobs/handlers/index.ts6
-rw-r--r--server/lib/jobs/handlers/video-file-optimizer.ts78
-rw-r--r--server/lib/jobs/handlers/video-file-transcoder.ts (renamed from server/lib/jobs/handlers/video-transcoder.ts)17
3 files changed, 90 insertions, 11 deletions
diff --git a/server/lib/jobs/handlers/index.ts b/server/lib/jobs/handlers/index.ts
index 8abddae35..5941427a1 100644
--- a/server/lib/jobs/handlers/index.ts
+++ b/server/lib/jobs/handlers/index.ts
@@ -1,4 +1,5 @@
1import * as videoTranscoder from './video-transcoder' 1import * as videoFileOptimizer from './video-file-optimizer'
2import * as videoFileTranscoder from './video-file-transcoder'
2 3
3export interface JobHandler<T> { 4export interface JobHandler<T> {
4 process (data: object): T 5 process (data: object): T
@@ -7,7 +8,8 @@ export interface JobHandler<T> {
7} 8}
8 9
9const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = { 10const jobHandlers: { [ handlerName: string ]: JobHandler<any> } = {
10 videoTranscoder 11 videoFileOptimizer,
12 videoFileTranscoder
11} 13}
12 14
13export { 15export {
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}
diff --git a/server/lib/jobs/handlers/video-transcoder.ts b/server/lib/jobs/handlers/video-file-transcoder.ts
index 87d8ffa6a..0e45b4dca 100644
--- a/server/lib/jobs/handlers/video-transcoder.ts
+++ b/server/lib/jobs/handlers/video-file-transcoder.ts
@@ -1,13 +1,12 @@
1import { database as db } from '../../../initializers/database' 1import { database as db } from '../../../initializers/database'
2import { updateVideoToFriends } from '../../friends'
2import { logger } from '../../../helpers' 3import { logger } from '../../../helpers'
3import { addVideoToFriends } from '../../../lib'
4import { VideoInstance } from '../../../models' 4import { VideoInstance } from '../../../models'
5import { VideoResolution } from '../../../../shared'
5 6
6function process (data: { videoUUID: string }) { 7function process (data: { videoUUID: string, resolution: VideoResolution }) {
7 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => { 8 return db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(data.videoUUID).then(video => {
8 // TODO: handle multiple resolutions 9 return video.transcodeOriginalVideofile(data.resolution).then(() => video)
9 const videoFile = video.VideoFiles[0]
10 return video.transcodeVideofile(videoFile).then(() => video)
11 }) 10 })
12} 11}
13 12
@@ -19,10 +18,10 @@ function onError (err: Error, jobId: number) {
19function onSuccess (jobId: number, video: VideoInstance) { 18function onSuccess (jobId: number, video: VideoInstance) {
20 logger.info('Job %d is a success.', jobId) 19 logger.info('Job %d is a success.', jobId)
21 20
22 video.toAddRemoteJSON().then(remoteVideo => { 21 const remoteVideo = video.toUpdateRemoteJSON()
23 // Now we'll add the video's meta data to our friends 22
24 return addVideoToFriends(remoteVideo, null) 23 // Now we'll add the video's meta data to our friends
25 }) 24 return updateVideoToFriends(remoteVideo, null)
26} 25}
27 26
28// --------------------------------------------------------------------------- 27// ---------------------------------------------------------------------------