2023-05-04 08:55:51 -05:00
|
|
|
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'
|
2023-05-04 08:55:51 -05:00
|
|
|
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())
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-22 08:25:39 -05:00
|
|
|
export function scheduleTranscodingProgress (options: {
|
2023-04-21 08:05:27 -05:00
|
|
|
server: PeerTubeServer
|
|
|
|
runnerToken: string
|
|
|
|
job: JobWithToken
|
2023-06-22 08:25:39 -05:00
|
|
|
progressGetter: () => number
|
2023-04-21 08:05:27 -05:00
|
|
|
}) {
|
2023-06-22 08:25:39 -05:00
|
|
|
const { job, server, progressGetter, runnerToken } = options
|
2023-04-21 08:05:27 -05:00
|
|
|
|
|
|
|
const updateInterval = ConfigManager.Instance.isTestInstance()
|
|
|
|
? 500
|
|
|
|
: 60000
|
|
|
|
|
2023-06-22 08:25:39 -05:00
|
|
|
const update = () => {
|
|
|
|
server.runnerJobs.update({ jobToken: job.jobToken, jobUUID: job.uuid, runnerToken, progress: progressGetter() })
|
|
|
|
.catch(err => logger.error({ err }, 'Cannot send job progress'))
|
|
|
|
}
|
2023-04-21 08:05:27 -05:00
|
|
|
|
2023-06-19 06:45:26 -05:00
|
|
|
const interval = setInterval(() => {
|
2023-06-22 08:25:39 -05:00
|
|
|
update()
|
2023-06-19 06:45:26 -05:00
|
|
|
}, updateInterval)
|
2023-04-21 08:05:27 -05:00
|
|
|
|
2023-06-22 08:25:39 -05:00
|
|
|
update()
|
|
|
|
|
|
|
|
return interval
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export function buildFFmpegVOD (options: {
|
|
|
|
onJobProgress: (progress: number) => void
|
|
|
|
}) {
|
|
|
|
const { onJobProgress } = options
|
|
|
|
|
2023-04-21 08:05:27 -05:00
|
|
|
return new FFmpegVOD({
|
2023-05-04 08:29:34 -05:00
|
|
|
...getCommonFFmpegOptions(),
|
|
|
|
|
2023-06-19 06:45:26 -05:00
|
|
|
updateJobProgress: arg => {
|
2023-06-22 08:25:39 -05:00
|
|
|
const progress = arg < 0 || arg > 100
|
|
|
|
? undefined
|
|
|
|
: arg
|
|
|
|
|
|
|
|
onJobProgress(progress)
|
2023-06-19 06:45:26 -05:00
|
|
|
}
|
2023-04-21 08:05:27 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildFFmpegLive () {
|
2023-05-04 08:29:34 -05:00
|
|
|
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()
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
return {
|
2023-04-21 08:05:27 -05:00
|
|
|
niceness: config.ffmpeg.nice,
|
|
|
|
threads: config.ffmpeg.threads,
|
|
|
|
tmpDirectory: ConfigManager.Instance.getTranscodingDirectory(),
|
|
|
|
profile: 'default',
|
|
|
|
availableEncoders: {
|
2023-05-04 08:55:51 -05:00
|
|
|
available: getDefaultAvailableEncoders(),
|
|
|
|
encodersToTry: getDefaultEncodersToTry()
|
2023-04-21 08:05:27 -05:00
|
|
|
},
|
|
|
|
logger: getTranscodingLogger()
|
2023-05-04 08:29:34 -05:00
|
|
|
}
|
2023-04-21 08:05:27 -05:00
|
|
|
}
|