]>
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 | ||
44e702de C |
52 | logger.info('Manually creating transcoding jobs for %s.', body.transcodingType, { childrenResolutions, maxResolution }) |
53 | ||
b42c2c7e | 54 | const children = await Bluebird.mapSeries(childrenResolutions, resolution => { |
ad5db104 | 55 | if (body.transcodingType === 'hls') { |
b42c2c7e | 56 | return buildHLSJobOption({ |
ad5db104 | 57 | videoUUID: video.uuid, |
b42c2c7e | 58 | hasAudio, |
ad5db104 | 59 | resolution, |
b42c2c7e | 60 | isMaxQuality: false |
ad5db104 | 61 | }) |
b42c2c7e C |
62 | } |
63 | ||
64 | if (body.transcodingType === 'webtorrent') { | |
65 | return buildWebTorrentJobOption({ | |
ad5db104 | 66 | videoUUID: video.uuid, |
b42c2c7e C |
67 | hasAudio, |
68 | resolution | |
ad5db104 C |
69 | }) |
70 | } | |
b42c2c7e C |
71 | }) |
72 | ||
73 | const parent = body.transcodingType === 'hls' | |
74 | ? await buildHLSJobOption({ | |
75 | videoUUID: video.uuid, | |
76 | hasAudio, | |
77 | resolution: maxResolution, | |
78 | isMaxQuality: false | |
79 | }) | |
80 | : await buildWebTorrentJobOption({ | |
81 | videoUUID: video.uuid, | |
82 | hasAudio, | |
83 | resolution: maxResolution | |
84 | }) | |
85 | ||
86 | // Porcess the last resolution after the other ones to prevent concurrency issue | |
87 | // Because low resolutions use the biggest one as ffmpeg input | |
88 | await JobQueue.Instance.createJobWithChildren(parent, children) | |
ad5db104 C |
89 | |
90 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | |
91 | } | |
b42c2c7e C |
92 | |
93 | function buildHLSJobOption (options: { | |
94 | videoUUID: string | |
95 | hasAudio: boolean | |
96 | resolution: number | |
97 | isMaxQuality: boolean | |
98 | }) { | |
99 | const { videoUUID, hasAudio, resolution, isMaxQuality } = options | |
100 | ||
101 | return buildTranscodingJob({ | |
102 | type: 'new-resolution-to-hls', | |
103 | videoUUID, | |
104 | resolution, | |
105 | hasAudio, | |
106 | copyCodecs: false, | |
107 | isNewVideo: false, | |
108 | autoDeleteWebTorrentIfNeeded: false, | |
109 | isMaxQuality | |
110 | }) | |
111 | } | |
112 | ||
113 | function buildWebTorrentJobOption (options: { | |
114 | videoUUID: string | |
115 | hasAudio: boolean | |
116 | resolution: number | |
117 | }) { | |
118 | const { videoUUID, hasAudio, resolution } = options | |
119 | ||
120 | return buildTranscodingJob({ | |
121 | type: 'new-resolution-to-webtorrent', | |
122 | videoUUID, | |
123 | isNewVideo: false, | |
124 | resolution, | |
125 | hasAudio, | |
126 | createHLSIfNeeded: false | |
127 | }) | |
128 | } |