aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-02-11 10:51:33 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-02-28 10:42:19 +0100
commitc729caf6cc34630877a0e5a1bda1719384cd0c8a (patch)
tree1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/controllers/api/videos
parenta24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff)
downloadPeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz
PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst
PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip
Add basic video editor support
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/editor.ts120
-rw-r--r--server/controllers/api/videos/index.ts2
-rw-r--r--server/controllers/api/videos/transcoding.ts4
-rw-r--r--server/controllers/api/videos/upload.ts8
4 files changed, 128 insertions, 6 deletions
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 @@
1import express from 'express'
2import { createAnyReqFiles } from '@server/helpers/express-utils'
3import { CONFIG } from '@server/initializers/config'
4import { MIMETYPES } from '@server/initializers/constants'
5import { JobQueue } from '@server/lib/job-queue'
6import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor'
7import {
8 HttpStatusCode,
9 VideoEditionTaskPayload,
10 VideoEditorCreateEdition,
11 VideoEditorTask,
12 VideoEditorTaskCut,
13 VideoEditorTaskIntro,
14 VideoEditorTaskOutro,
15 VideoEditorTaskWatermark,
16 VideoState
17} from '@shared/models'
18import { asyncMiddleware, authenticate, videosEditorAddEditionValidator } from '../../../middlewares'
19
20const editorRouter = express.Router()
21
22const 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
48editorRouter.post('/:videoId/editor/edit',
49 authenticate,
50 tasksFiles,
51 asyncMiddleware(videosEditorAddEditionValidator),
52 asyncMiddleware(createEditionTasks)
53)
54
55// ---------------------------------------------------------------------------
56
57export {
58 editorRouter
59}
60
61// ---------------------------------------------------------------------------
62
63async 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
81const 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
90function buildTaskPayload (task: VideoEditorTask, indice: number, files: Express.Multer.File[]): VideoEditionTaskPayload {
91 return taskPayloadBuilders[task.name](task, indice, files)
92}
93
94function 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
103function 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
113function 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'
35import { blacklistRouter } from './blacklist' 35import { blacklistRouter } from './blacklist'
36import { videoCaptionsRouter } from './captions' 36import { videoCaptionsRouter } from './captions'
37import { videoCommentRouter } from './comment' 37import { videoCommentRouter } from './comment'
38import { editorRouter } from './editor'
38import { filesRouter } from './files' 39import { filesRouter } from './files'
39import { videoImportsRouter } from './import' 40import { videoImportsRouter } from './import'
40import { liveRouter } from './live' 41import { liveRouter } from './live'
@@ -51,6 +52,7 @@ const videosRouter = express.Router()
51videosRouter.use('/', blacklistRouter) 52videosRouter.use('/', blacklistRouter)
52videosRouter.use('/', rateVideoRouter) 53videosRouter.use('/', rateVideoRouter)
53videosRouter.use('/', videoCommentRouter) 54videosRouter.use('/', videoCommentRouter)
55videosRouter.use('/', editorRouter)
54videosRouter.use('/', videoCaptionsRouter) 56videosRouter.use('/', videoCaptionsRouter)
55videosRouter.use('/', videoImportsRouter) 57videosRouter.use('/', videoImportsRouter)
56videosRouter.use('/', ownershipVideoRouter) 58videosRouter.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 @@
1import express from 'express' 1import express from 'express'
2import { computeLowerResolutionsToTranscode } from '@server/helpers/ffprobe-utils' 2import { computeLowerResolutionsToTranscode } from '@server/helpers/ffmpeg'
3import { logger, loggerTagsFactory } from '@server/helpers/logger' 3import { logger, loggerTagsFactory } from '@server/helpers/logger'
4import { addTranscodingJob } from '@server/lib/video' 4import { addTranscodingJob } from '@server/lib/video'
5import { HttpStatusCode, UserRight, VideoState, VideoTranscodingCreate } from '@shared/models' 5import { 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
24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
25import { retryTransactionWrapper } from '../../../helpers/database-utils' 25import { retryTransactionWrapper } from '../../../helpers/database-utils'
26import { createReqFiles } from '../../../helpers/express-utils' 26import { createReqFiles } from '../../../helpers/express-utils'
27import { ffprobePromise, getMetadataFromFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' 27import { ffprobePromise, buildFileMetadata, getVideoStreamFPS, getVideoStreamDimensionsInfo } from '../../../helpers/ffmpeg'
28import { logger, loggerTagsFactory } from '../../../helpers/logger' 28import { logger, loggerTagsFactory } from '../../../helpers/logger'
29import { CONFIG } from '../../../initializers/config' 29import { CONFIG } from '../../../initializers/config'
30import { MIMETYPES } from '../../../initializers/constants' 30import { 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)