]>
Commit | Line | Data |
---|---|---|
ad5db104 | 1 | import express from 'express' |
84cae54e | 2 | import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg' |
ad5db104 C |
3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
4 | import { addTranscodingJob } from '@server/lib/video' | |
5 | import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models' | |
6 | import { asyncMiddleware, authenticate, createTranscodingValidator, ensureUserHasRight } from '../../../middlewares' | |
ebb9e53a | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
ad5db104 C |
8 | |
9 | const lTags = loggerTagsFactory('api', 'video') | |
10 | const transcodingRouter = express.Router() | |
11 | ||
12 | transcodingRouter.post('/:videoId/transcoding', | |
13 | authenticate, | |
14 | ensureUserHasRight(UserRight.RUN_VIDEO_TRANSCODING), | |
15 | asyncMiddleware(createTranscodingValidator), | |
16 | asyncMiddleware(createTranscoding) | |
17 | ) | |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
21 | export { | |
22 | transcodingRouter | |
23 | } | |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
27 | async function createTranscoding (req: express.Request, res: express.Response) { | |
28 | const video = res.locals.videoAll | |
0f11ec8d | 29 | logger.info('Creating %s transcoding job for %s.', req.body.transcodingType, video.url, lTags()) |
ad5db104 C |
30 | |
31 | const body: VideoTranscodingCreate = req.body | |
32 | ||
84cae54e | 33 | const { resolution: maxResolution, audioStream } = await video.probeMaxQualityFile() |
ebb9e53a | 34 | const resolutions = await Hooks.wrapObject( |
5e2afe42 | 35 | computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false }), |
64fd6158 | 36 | 'filter:transcoding.manual.resolutions-to-transcode.result', |
ebb9e53a C |
37 | body |
38 | ) | |
39 | ||
40 | if (resolutions.length === 0) { | |
41 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
42 | } | |
ad5db104 C |
43 | |
44 | video.state = VideoState.TO_TRANSCODE | |
45 | await video.save() | |
46 | ||
47 | for (const resolution of resolutions) { | |
48 | if (body.transcodingType === 'hls') { | |
49 | await addTranscodingJob({ | |
50 | type: 'new-resolution-to-hls', | |
51 | videoUUID: video.uuid, | |
52 | resolution, | |
cbe2f36d | 53 | hasAudio: !!audioStream, |
ad5db104 C |
54 | copyCodecs: false, |
55 | isNewVideo: false, | |
56 | autoDeleteWebTorrentIfNeeded: false, | |
57 | isMaxQuality: maxResolution === resolution | |
58 | }) | |
59 | } else if (body.transcodingType === 'webtorrent') { | |
60 | await addTranscodingJob({ | |
61 | type: 'new-resolution-to-webtorrent', | |
62 | videoUUID: video.uuid, | |
63 | isNewVideo: false, | |
0f11ec8d | 64 | resolution, |
cbe2f36d | 65 | hasAudio: !!audioStream, |
84cae54e | 66 | createHLSIfNeeded: false |
ad5db104 C |
67 | }) |
68 | } | |
69 | } | |
70 | ||
71 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
72 | } |