]>
Commit | Line | Data |
---|---|---|
b42c2c7e | 1 | import Bluebird from 'bluebird' |
ad5db104 | 2 | import express from 'express' |
84cae54e | 3 | import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg' |
ad5db104 | 4 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
b42c2c7e C |
5 | import { JobQueue } from '@server/lib/job-queue' |
6 | import { Hooks } from '@server/lib/plugins/hooks' | |
7 | import { buildTranscodingJob } from '@server/lib/video' | |
ad5db104 C |
8 | import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models' |
9 | import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares' | |
10 | ||
11 | const lTags = loggerTagsFactory('api', 'video') | |
12 | const transcodingRouter = express.Router() | |
13 | ||
14 | transcodingRouter.post('/:videoId/transcoding', | |
15 | authenticate, | |
16 | ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING), | |
17 | asyncMiddleware(createTranscodingValidator), | |
18 | asyncMiddleware(createTranscoding) | |
19 | ) | |
20 | ||
21 | // --------------------------------------------------------------------------- | |
22 | ||
23 | export { | |
24 | transcodingRouter | |
25 | } | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | async function createTranscoding (req: express.Request, res: express.Response) { | |
30 | const video = res.locals.videoAll | |
0f11ec8d | 31 | logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags()) |
ad5db104 C |
32 | |
33 | const body: VideoTranscodingCreate = req.body | |
34 | ||
a32bf8cd C |
35 | const { resolution: maxResolution, hasAudio } = await video.probeMaxQualityFile() |
36 | ||
ebb9e53a | 37 | const resolutions = await Hooks.wrapObject( |
a32bf8cd | 38 | computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false, hasAudio }), |
64fd6158 | 39 | 'filter:transcoding.manual.resolutions-to-transcode.result', |
ebb9e53a C |
40 | body |
41 | ) | |
42 | ||
43 | if (resolutions.length === 0) { | |
44 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
45 | } | |
ad5db104 C |
46 | |
47 | video.state = VideoState.TO_TRANSCODE | |
48 | await video.save() | |
49 | ||
b42c2c7e C |
50 | const childrenResolutions = resolutions.filter(r => r !== maxResolution) |
51 | ||
52 | const children = await Bluebird.mapSeries(childrenResolutions, resolution => { | |
ad5db104 | 53 | if (body.transcodingType === 'hls') { |
b42c2c7e | 54 | return buildHLSJobOption({ |
ad5db104 | 55 | videoUUID: video.uuid, |
b42c2c7e | 56 | hasAudio, |
ad5db104 | 57 | resolution, |
b42c2c7e | 58 | isMaxQuality: false |
ad5db104 | 59 | }) |
b42c2c7e C |
60 | } |
61 | ||
62 | if (body.transcodingType === 'webtorrent') { | |
63 | return buildWebTorrentJobOption({ | |
ad5db104 | 64 | videoUUID: video.uuid, |
b42c2c7e C |
65 | hasAudio, |
66 | resolution | |
ad5db104 C |
67 | }) |
68 | } | |
b42c2c7e C |
69 | }) |
70 | ||
71 | const parent = body.transcodingType === 'hls' | |
72 | ? await buildHLSJobOption({ | |
73 | videoUUID: video.uuid, | |
74 | hasAudio, | |
75 | resolution: maxResolution, | |
76 | isMaxQuality: false | |
77 | }) | |
78 | : await buildWebTorrentJobOption({ | |
79 | videoUUID: video.uuid, | |
80 | hasAudio, | |
81 | resolution: maxResolution | |
82 | }) | |
83 | ||
84 | // Porcess the last resolution after the other ones to prevent concurrency issue | |
85 | // Because low resolutions use the biggest one as ffmpeg input | |
86 | await JobQueue.Instance.createJobWithChildren(parent, children) | |
ad5db104 C |
87 | |
88 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
89 | } | |
b42c2c7e C |
90 | |
91 | function buildHLSJobOption (options: { | |
92 | videoUUID: string | |
93 | hasAudio: boolean | |
94 | resolution: number | |
95 | isMaxQuality: boolean | |
96 | }) { | |
97 | const { videoUUID, hasAudio, resolution, isMaxQuality } = options | |
98 | ||
99 | return buildTranscodingJob({ | |
100 | type: 'new-resolution-to-hls', | |
101 | videoUUID, | |
102 | resolution, | |
103 | hasAudio, | |
104 | copyCodecs: false, | |
105 | isNewVideo: false, | |
106 | autoDeleteWebTorrentIfNeeded: false, | |
107 | isMaxQuality | |
108 | }) | |
109 | } | |
110 | ||
111 | function buildWebTorrentJobOption (options: { | |
112 | videoUUID: string | |
113 | hasAudio: boolean | |
114 | resolution: number | |
115 | }) { | |
116 | const { videoUUID, hasAudio, resolution } = options | |
117 | ||
118 | return buildTranscodingJob({ | |
119 | type: 'new-resolution-to-webtorrent', | |
120 | videoUUID, | |
121 | isNewVideo: false, | |
122 | resolution, | |
123 | hasAudio, | |
124 | createHLSIfNeeded: false | |
125 | }) | |
126 | } |