2023-05-03 08:17:11 -05:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2022-03-22 10:58:49 -05:00
|
|
|
import { MVideoFullLight } from '@server/types/models'
|
2023-04-21 07:55:10 -05:00
|
|
|
import { getVideoStreamDuration } from '@shared/ffmpeg'
|
2023-05-03 08:17:11 -05:00
|
|
|
import { VideoStudioEditionPayload, VideoStudioTask } from '@shared/models'
|
|
|
|
import { remove } from 'fs-extra'
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
function buildTaskFileFieldname (indice: number, fieldName = 'file') {
|
|
|
|
return `tasks[${indice}][options][${fieldName}]`
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
function getTaskFileFromReq (files: Express.Multer.File[], indice: number, fieldName = 'file') {
|
2022-02-11 03:51:33 -06:00
|
|
|
return files.find(f => f.fieldname === buildTaskFileFieldname(indice, fieldName))
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
async function safeCleanupStudioTMPFiles (payload: VideoStudioEditionPayload) {
|
|
|
|
for (const task of payload.tasks) {
|
|
|
|
try {
|
|
|
|
if (task.name === 'add-intro' || task.name === 'add-outro') {
|
|
|
|
await remove(task.options.file)
|
|
|
|
} else if (task.name === 'add-watermark') {
|
|
|
|
await remove(task.options.file)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot remove studio file', { err })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
async function approximateIntroOutroAdditionalSize (video: MVideoFullLight, tasks: VideoStudioTask[], fileFinder: (i: number) => string) {
|
2022-02-11 03:51:33 -06:00
|
|
|
let additionalDuration = 0
|
|
|
|
|
|
|
|
for (let i = 0; i < tasks.length; i++) {
|
|
|
|
const task = tasks[i]
|
|
|
|
|
|
|
|
if (task.name !== 'add-intro' && task.name !== 'add-outro') continue
|
|
|
|
|
|
|
|
const filePath = fileFinder(i)
|
|
|
|
additionalDuration += await getVideoStreamDuration(filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (video.getMaxQualityFile().size / video.duration) * additionalDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
approximateIntroOutroAdditionalSize,
|
|
|
|
buildTaskFileFieldname,
|
2023-05-03 08:17:11 -05:00
|
|
|
getTaskFileFromReq,
|
|
|
|
safeCleanupStudioTMPFiles
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|