PeerTube/packages/peertube-runner/server/process/shared/common.ts

108 lines
3.0 KiB
TypeScript
Raw Normal View History

import { remove } from 'fs-extra'
2023-04-21 08:05:27 -05:00
import { ConfigManager, downloadFile, logger } from 'packages/peertube-runner/shared'
import { join } from 'path'
import { buildUUID } from '@shared/extra-utils'
import { FFmpegEdition, FFmpegLive, FFmpegVOD, getDefaultAvailableEncoders, getDefaultEncodersToTry } from '@shared/ffmpeg'
2023-04-21 08:05:27 -05:00
import { RunnerJob, RunnerJobPayload } from '@shared/models'
import { PeerTubeServer } from '@shared/server-commands'
import { getTranscodingLogger } from './transcoding-logger'
export type JobWithToken <T extends RunnerJobPayload = RunnerJobPayload> = RunnerJob<T> & { jobToken: string }
export type ProcessOptions <T extends RunnerJobPayload = RunnerJobPayload> = {
server: PeerTubeServer
job: JobWithToken<T>
runnerToken: string
}
export async function downloadInputFile (options: {
url: string
job: JobWithToken
runnerToken: string
}) {
const { url, job, runnerToken } = options
const destination = join(ConfigManager.Instance.getTranscodingDirectory(), buildUUID())
try {
await downloadFile({ url, jobToken: job.jobToken, runnerToken, destination })
} catch (err) {
remove(destination)
.catch(err => logger.error({ err }, `Cannot remove ${destination}`))
throw err
}
2023-04-21 08:05:27 -05:00
return destination
}
export async function updateTranscodingProgress (options: {
server: PeerTubeServer
runnerToken: string
job: JobWithToken
progress: number
}) {
const { server, job, runnerToken, progress } = options
return server.runnerJobs.update({ jobToken: job.jobToken, jobUUID: job.uuid, runnerToken, progress })
}
// ---------------------------------------------------------------------------
2023-04-21 08:05:27 -05:00
export function buildFFmpegVOD (options: {
server: PeerTubeServer
runnerToken: string
job: JobWithToken
}) {
const { server, job, runnerToken } = options
const updateInterval = ConfigManager.Instance.isTestInstance()
? 500
: 60000
2023-06-19 06:45:26 -05:00
let progress: number
2023-04-21 08:05:27 -05:00
2023-06-19 06:45:26 -05:00
const interval = setInterval(() => {
2023-04-21 08:05:27 -05:00
updateTranscodingProgress({ server, job, runnerToken, progress })
.catch(err => logger.error({ err }, 'Cannot send job progress'))
2023-06-19 06:45:26 -05:00
}, updateInterval)
2023-04-21 08:05:27 -05:00
return new FFmpegVOD({
...getCommonFFmpegOptions(),
2023-06-19 06:45:26 -05:00
onError: () => clearInterval(interval),
onEnd: () => clearInterval(interval),
updateJobProgress: arg => {
if (arg < 0 || arg > 100) {
progress = undefined
} else {
progress = arg
}
}
2023-04-21 08:05:27 -05:00
})
}
export function buildFFmpegLive () {
return new FFmpegLive(getCommonFFmpegOptions())
}
export function buildFFmpegEdition () {
return new FFmpegEdition(getCommonFFmpegOptions())
}
function getCommonFFmpegOptions () {
2023-04-21 08:05:27 -05:00
const config = ConfigManager.Instance.getConfig()
return {
2023-04-21 08:05:27 -05:00
niceness: config.ffmpeg.nice,
threads: config.ffmpeg.threads,
tmpDirectory: ConfigManager.Instance.getTranscodingDirectory(),
profile: 'default',
availableEncoders: {
available: getDefaultAvailableEncoders(),
encodersToTry: getDefaultEncodersToTry()
2023-04-21 08:05:27 -05:00
},
logger: getTranscodingLogger()
}
2023-04-21 08:05:27 -05:00
}