]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-studio.ts
Add TMP persistent directory
[github/Chocobozzz/PeerTube.git] / server / lib / video-studio.ts
1 import { logger } from '@server/helpers/logger'
2 import { MVideoFullLight } from '@server/types/models'
3 import { getVideoStreamDuration } from '@shared/ffmpeg'
4 import { VideoStudioEditionPayload, VideoStudioTask } from '@shared/models'
5 import { remove } from 'fs-extra'
6
7 function buildTaskFileFieldname (indice: number, fieldName = 'file') {
8 return `tasks[${indice}][options][${fieldName}]`
9 }
10
11 function getTaskFileFromReq (files: Express.Multer.File[], indice: number, fieldName = 'file') {
12 return files.find(f => f.fieldname === buildTaskFileFieldname(indice, fieldName))
13 }
14
15 async function safeCleanupStudioTMPFiles (payload: VideoStudioEditionPayload) {
16 for (const task of payload.tasks) {
17 try {
18 if (task.name === 'add-intro' || task.name === 'add-outro') {
19 await remove(task.options.file)
20 } else if (task.name === 'add-watermark') {
21 await remove(task.options.file)
22 }
23 } catch (err) {
24 logger.error('Cannot remove studio file', { err })
25 }
26 }
27 }
28
29 async function approximateIntroOutroAdditionalSize (video: MVideoFullLight, tasks: VideoStudioTask[], fileFinder: (i: number) => string) {
30 let additionalDuration = 0
31
32 for (let i = 0; i < tasks.length; i++) {
33 const task = tasks[i]
34
35 if (task.name !== 'add-intro' && task.name !== 'add-outro') continue
36
37 const filePath = fileFinder(i)
38 additionalDuration += await getVideoStreamDuration(filePath)
39 }
40
41 return (video.getMaxQualityFile().size / video.duration) * additionalDuration
42 }
43
44 export {
45 approximateIntroOutroAdditionalSize,
46 buildTaskFileFieldname,
47 getTaskFileFromReq,
48 safeCleanupStudioTMPFiles
49 }