diff options
author | Chocobozzz <me@florianbigard.com> | 2022-02-11 10:51:33 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-02-28 10:42:19 +0100 |
commit | c729caf6cc34630877a0e5a1bda1719384cd0c8a (patch) | |
tree | 1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/controllers/api | |
parent | a24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff) | |
download | PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip |
Add basic video editor support
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/config.ts | 3 | ||||
-rw-r--r-- | server/controllers/api/videos/editor.ts | 120 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/videos/transcoding.ts | 4 | ||||
-rw-r--r-- | server/controllers/api/videos/upload.ts | 8 |
5 files changed, 131 insertions, 6 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 4e3dd4d80..821ed4ad3 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts | |||
@@ -256,6 +256,9 @@ function customConfig (): CustomConfig { | |||
256 | } | 256 | } |
257 | } | 257 | } |
258 | }, | 258 | }, |
259 | videoEditor: { | ||
260 | enabled: CONFIG.VIDEO_EDITOR.ENABLED | ||
261 | }, | ||
259 | import: { | 262 | import: { |
260 | videos: { | 263 | videos: { |
261 | concurrency: CONFIG.IMPORT.VIDEOS.CONCURRENCY, | 264 | concurrency: CONFIG.IMPORT.VIDEOS.CONCURRENCY, |
diff --git a/server/controllers/api/videos/editor.ts b/server/controllers/api/videos/editor.ts new file mode 100644 index 000000000..61e2eb5da --- /dev/null +++ b/server/controllers/api/videos/editor.ts | |||
@@ -0,0 +1,120 @@ | |||
1 | import express from 'express' | ||
2 | import { createAnyReqFiles } from '@server/helpers/express-utils' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
4 | import { MIMETYPES } from '@server/initializers/constants' | ||
5 | import { JobQueue } from '@server/lib/job-queue' | ||
6 | import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor' | ||
7 | import { | ||
8 | HttpStatusCode, | ||
9 | VideoEditionTaskPayload, | ||
10 | VideoEditorCreateEdition, | ||
11 | VideoEditorTask, | ||
12 | VideoEditorTaskCut, | ||
13 | VideoEditorTaskIntro, | ||
14 | VideoEditorTaskOutro, | ||
15 | VideoEditorTaskWatermark, | ||
16 | VideoState | ||
17 | } from '@shared/models' | ||
18 | import { asyncMiddleware, authenticate, videosEditorAddEditionValidator } from '../../../middlewares' | ||
19 | |||
20 | const editorRouter = express.Router() | ||
21 | |||
22 | const tasksFiles = createAnyReqFiles( | ||
23 | MIMETYPES.VIDEO.MIMETYPE_EXT, | ||
24 | CONFIG.STORAGE.TMP_DIR, | ||
25 | (req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => { | ||
26 | const body = req.body as VideoEditorCreateEdition | ||
27 | |||
28 | // Fetch array element | ||
29 | const matches = file.fieldname.match(/tasks\[(\d+)\]/) | ||
30 | if (!matches) return cb(new Error('Cannot find array element indice for ' + file.fieldname)) | ||
31 | |||
32 | const indice = parseInt(matches[1]) | ||
33 | const task = body.tasks[indice] | ||
34 | |||
35 | if (!task) return cb(new Error('Cannot find array element of indice ' + indice + ' for ' + file.fieldname)) | ||
36 | |||
37 | if ( | ||
38 | [ 'add-intro', 'add-outro', 'add-watermark' ].includes(task.name) && | ||
39 | file.fieldname === buildTaskFileFieldname(indice) | ||
40 | ) { | ||
41 | return cb(null, true) | ||
42 | } | ||
43 | |||
44 | return cb(null, false) | ||
45 | } | ||
46 | ) | ||
47 | |||
48 | editorRouter.post('/:videoId/editor/edit', | ||
49 | authenticate, | ||
50 | tasksFiles, | ||
51 | asyncMiddleware(videosEditorAddEditionValidator), | ||
52 | asyncMiddleware(createEditionTasks) | ||
53 | ) | ||
54 | |||
55 | // --------------------------------------------------------------------------- | ||
56 | |||
57 | export { | ||
58 | editorRouter | ||
59 | } | ||
60 | |||
61 | // --------------------------------------------------------------------------- | ||
62 | |||
63 | async function createEditionTasks (req: express.Request, res: express.Response) { | ||
64 | const files = req.files as Express.Multer.File[] | ||
65 | const body = req.body as VideoEditorCreateEdition | ||
66 | const video = res.locals.videoAll | ||
67 | |||
68 | video.state = VideoState.TO_EDIT | ||
69 | await video.save() | ||
70 | |||
71 | const payload = { | ||
72 | videoUUID: video.uuid, | ||
73 | tasks: body.tasks.map((t, i) => buildTaskPayload(t, i, files)) | ||
74 | } | ||
75 | |||
76 | JobQueue.Instance.createJob({ type: 'video-edition', payload }) | ||
77 | |||
78 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
79 | } | ||
80 | |||
81 | const taskPayloadBuilders: { | ||
82 | [id in VideoEditorTask['name']]: (task: VideoEditorTask, indice?: number, files?: Express.Multer.File[]) => VideoEditionTaskPayload | ||
83 | } = { | ||
84 | 'add-intro': buildIntroOutroTask, | ||
85 | 'add-outro': buildIntroOutroTask, | ||
86 | 'cut': buildCutTask, | ||
87 | 'add-watermark': buildWatermarkTask | ||
88 | } | ||
89 | |||
90 | function buildTaskPayload (task: VideoEditorTask, indice: number, files: Express.Multer.File[]): VideoEditionTaskPayload { | ||
91 | return taskPayloadBuilders[task.name](task, indice, files) | ||
92 | } | ||
93 | |||
94 | function buildIntroOutroTask (task: VideoEditorTaskIntro | VideoEditorTaskOutro, indice: number, files: Express.Multer.File[]) { | ||
95 | return { | ||
96 | name: task.name, | ||
97 | options: { | ||
98 | file: getTaskFile(files, indice).path | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | function buildCutTask (task: VideoEditorTaskCut) { | ||
104 | return { | ||
105 | name: task.name, | ||
106 | options: { | ||
107 | start: task.options.start, | ||
108 | end: task.options.end | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | function buildWatermarkTask (task: VideoEditorTaskWatermark, indice: number, files: Express.Multer.File[]) { | ||
114 | return { | ||
115 | name: task.name, | ||
116 | options: { | ||
117 | file: getTaskFile(files, indice).path | ||
118 | } | ||
119 | } | ||
120 | } | ||
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 61a030ba1..a5ae07d95 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts | |||
@@ -35,6 +35,7 @@ import { VideoModel } from '../../../models/video/video' | |||
35 | import { blacklistRouter } from './blacklist' | 35 | import { blacklistRouter } from './blacklist' |
36 | import { videoCaptionsRouter } from './captions' | 36 | import { videoCaptionsRouter } from './captions' |
37 | import { videoCommentRouter } from './comment' | 37 | import { videoCommentRouter } from './comment' |
38 | import { editorRouter } from './editor' | ||
38 | import { filesRouter } from './files' | 39 | import { filesRouter } from './files' |
39 | import { videoImportsRouter } from './import' | 40 | import { videoImportsRouter } from './import' |
40 | import { liveRouter } from './live' | 41 | import { liveRouter } from './live' |
@@ -51,6 +52,7 @@ const videosRouter = express.Router() | |||
51 | videosRouter.use('/', blacklistRouter) | 52 | videosRouter.use('/', blacklistRouter) |
52 | videosRouter.use('/', rateVideoRouter) | 53 | videosRouter.use('/', rateVideoRouter) |
53 | videosRouter.use('/', videoCommentRouter) | 54 | videosRouter.use('/', videoCommentRouter) |
55 | videosRouter.use('/', editorRouter) | ||
54 | videosRouter.use('/', videoCaptionsRouter) | 56 | videosRouter.use('/', videoCaptionsRouter) |
55 | videosRouter.use('/', videoImportsRouter) | 57 | videosRouter.use('/', videoImportsRouter) |
56 | videosRouter.use('/', ownershipVideoRouter) | 58 | videosRouter.use('/', ownershipVideoRouter) |
diff --git a/server/controllers/api/videos/transcoding.ts b/server/controllers/api/videos/transcoding.ts index fba4545c2..da3ea3c9c 100644 --- a/server/controllers/api/videos/transcoding.ts +++ b/server/controllers/api/videos/transcoding.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { computeLowerResolutionsToTranscode } from '@server/helpers/ffprobe-utils' | 2 | import { computeLowerResolutionsToTranscode } from '@server/helpers/ffmpeg' |
3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | 3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
4 | import { addTranscodingJob } from '@server/lib/video' | 4 | import { addTranscodingJob } from '@server/lib/video' |
5 | import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models' | 5 | import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models' |
@@ -29,7 +29,7 @@ async function createTranscoding (req: express.Request, res: express.Response) { | |||
29 | 29 | ||
30 | const body: VideoTranscodingCreate = req.body | 30 | const body: VideoTranscodingCreate = req.body |
31 | 31 | ||
32 | const { resolution: maxResolution, isPortraitMode, audioStream } = await video.getMaxQualityFileInfo() | 32 | const { resolution: maxResolution, isPortraitMode, audioStream } = await video.probeMaxQualityFile() |
33 | const resolutions = computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ]) | 33 | const resolutions = computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ]) |
34 | 34 | ||
35 | video.state = VideoState.TO_TRANSCODE | 35 | video.state = VideoState.TO_TRANSCODE |
diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts index fd90d9915..3c026ad1f 100644 --- a/server/controllers/api/videos/upload.ts +++ b/server/controllers/api/videos/upload.ts | |||
@@ -24,7 +24,7 @@ import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@share | |||
24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 24 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 25 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
26 | import { createReqFiles } from '../../../helpers/express-utils' | 26 | import { createReqFiles } from '../../../helpers/express-utils' |
27 | import { ffprobePromise, getMetadataFromFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' | 27 | import { ffprobePromise, buildFileMetadata, getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg' |
28 | import { logger, loggerTagsFactory } from '../../../helpers/logger' | 28 | import { logger, loggerTagsFactory } from '../../../helpers/logger' |
29 | import { CONFIG } from '../../../initializers/config' | 29 | import { CONFIG } from '../../../initializers/config' |
30 | import { MIMETYPES } from '../../../initializers/constants' | 30 | import { MIMETYPES } from '../../../initializers/constants' |
@@ -246,7 +246,7 @@ async function buildNewFile (videoPhysicalFile: express.VideoUploadFile) { | |||
246 | extname: getLowercaseExtension(videoPhysicalFile.filename), | 246 | extname: getLowercaseExtension(videoPhysicalFile.filename), |
247 | size: videoPhysicalFile.size, | 247 | size: videoPhysicalFile.size, |
248 | videoStreamingPlaylistId: null, | 248 | videoStreamingPlaylistId: null, |
249 | metadata: await getMetadataFromFile(videoPhysicalFile.path) | 249 | metadata: await buildFileMetadata(videoPhysicalFile.path) |
250 | }) | 250 | }) |
251 | 251 | ||
252 | const probe = await ffprobePromise(videoPhysicalFile.path) | 252 | const probe = await ffprobePromise(videoPhysicalFile.path) |
@@ -254,8 +254,8 @@ async function buildNewFile (videoPhysicalFile: express.VideoUploadFile) { | |||
254 | if (await isAudioFile(videoPhysicalFile.path, probe)) { | 254 | if (await isAudioFile(videoPhysicalFile.path, probe)) { |
255 | videoFile.resolution = VideoResolution.H_NOVIDEO | 255 | videoFile.resolution = VideoResolution.H_NOVIDEO |
256 | } else { | 256 | } else { |
257 | videoFile.fps = await getVideoFileFPS(videoPhysicalFile.path, probe) | 257 | videoFile.fps = await getVideoStreamFPS(videoPhysicalFile.path, probe) |
258 | videoFile.resolution = (await getVideoFileResolution(videoPhysicalFile.path, probe)).resolution | 258 | videoFile.resolution = (await getVideoStreamDimensionsInfo(videoPhysicalFile.path, probe)).resolution |
259 | } | 259 | } |
260 | 260 | ||
261 | videoFile.filename = generateWebTorrentVideoFilename(videoFile.resolution, videoFile.extname) | 261 | videoFile.filename = generateWebTorrentVideoFilename(videoFile.resolution, videoFile.extname) |