2023-07-31 14:34:36 +02:00
|
|
|
import { pick } from '@peertube/peertube-core-utils'
|
2023-04-21 14:55:10 +02:00
|
|
|
import {
|
|
|
|
RunnerJobUpdatePayload,
|
|
|
|
RunnerJobVODHLSTranscodingPayload,
|
|
|
|
RunnerJobVODHLSTranscodingPrivatePayload,
|
|
|
|
VODHLSTranscodingSuccess
|
2023-07-31 14:34:36 +02:00
|
|
|
} from '@peertube/peertube-models'
|
2024-06-13 09:23:12 +02:00
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { onTranscodingEnded } from '@server/lib/transcoding/ended-transcoding.js'
|
|
|
|
import { onHLSVideoFileTranscoding } from '@server/lib/transcoding/hls-transcoding.js'
|
|
|
|
import { removeAllWebVideoFiles } from '@server/lib/video-file.js'
|
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
|
2024-07-23 16:38:51 +02:00
|
|
|
import { MVideoWithFile } from '@server/types/models/index.js'
|
2024-06-13 09:23:12 +02:00
|
|
|
import { MRunnerJob } from '@server/types/models/runners/index.js'
|
2024-07-23 16:38:51 +02:00
|
|
|
import { generateRunnerTranscodingAudioInputFileUrl, generateRunnerTranscodingVideoInputFileUrl } from '../runner-urls.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { AbstractVODTranscodingJobHandler } from './abstract-vod-transcoding-job-handler.js'
|
2024-06-13 09:23:12 +02:00
|
|
|
import { loadRunnerVideo } from './shared/utils.js'
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
type CreateOptions = {
|
2024-07-23 16:38:51 +02:00
|
|
|
video: MVideoWithFile
|
2023-04-21 14:55:10 +02:00
|
|
|
isNewVideo: boolean
|
|
|
|
deleteWebVideoFiles: boolean
|
|
|
|
resolution: number
|
|
|
|
fps: number
|
|
|
|
priority: number
|
2024-07-23 16:38:51 +02:00
|
|
|
separatedAudio: boolean
|
2023-04-21 14:55:10 +02:00
|
|
|
dependsOnRunnerJob?: MRunnerJob
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
export class VODHLSTranscodingJobHandler extends AbstractVODTranscodingJobHandler<CreateOptions, RunnerJobUpdatePayload, VODHLSTranscodingSuccess> {
|
|
|
|
|
|
|
|
async create (options: CreateOptions) {
|
2024-07-23 16:38:51 +02:00
|
|
|
const { video, resolution, fps, dependsOnRunnerJob, separatedAudio, priority } = options
|
2023-04-21 14:55:10 +02:00
|
|
|
|
|
|
|
const jobUUID = buildUUID()
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
const { separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()
|
|
|
|
|
2023-04-21 14:55:10 +02:00
|
|
|
const payload: RunnerJobVODHLSTranscodingPayload = {
|
|
|
|
input: {
|
2024-07-23 16:38:51 +02:00
|
|
|
videoFileUrl: generateRunnerTranscodingVideoInputFileUrl(jobUUID, video.uuid),
|
|
|
|
|
|
|
|
separatedAudioFileUrl: separatedAudioFile
|
|
|
|
? [ generateRunnerTranscodingAudioInputFileUrl(jobUUID, video.uuid) ]
|
|
|
|
: []
|
2023-04-21 14:55:10 +02:00
|
|
|
},
|
|
|
|
output: {
|
|
|
|
resolution,
|
2024-07-23 16:38:51 +02:00
|
|
|
fps,
|
|
|
|
separatedAudio
|
2023-04-21 14:55:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const privatePayload: RunnerJobVODHLSTranscodingPrivatePayload = {
|
|
|
|
...pick(options, [ 'isNewVideo', 'deleteWebVideoFiles' ]),
|
|
|
|
|
|
|
|
videoUUID: video.uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
const job = await this.createRunnerJob({
|
|
|
|
type: 'vod-hls-transcoding',
|
|
|
|
jobUUID,
|
|
|
|
payload,
|
|
|
|
privatePayload,
|
|
|
|
priority,
|
|
|
|
dependsOnRunnerJob
|
|
|
|
})
|
|
|
|
|
|
|
|
await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingTranscode')
|
|
|
|
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-05-04 15:29:34 +02:00
|
|
|
protected async specificComplete (options: {
|
2023-04-21 14:55:10 +02:00
|
|
|
runnerJob: MRunnerJob
|
|
|
|
resultPayload: VODHLSTranscodingSuccess
|
|
|
|
}) {
|
|
|
|
const { runnerJob, resultPayload } = options
|
|
|
|
const privatePayload = runnerJob.privatePayload as RunnerJobVODHLSTranscodingPrivatePayload
|
|
|
|
|
2024-06-13 09:23:12 +02:00
|
|
|
const video = await loadRunnerVideo(runnerJob, this.lTags)
|
2023-04-21 14:55:10 +02:00
|
|
|
if (!video) return
|
|
|
|
|
|
|
|
const videoFilePath = resultPayload.videoFile as string
|
|
|
|
const resolutionPlaylistFilePath = resultPayload.resolutionPlaylistFile as string
|
|
|
|
|
|
|
|
await onHLSVideoFileTranscoding({
|
|
|
|
video,
|
2024-02-27 11:18:56 +01:00
|
|
|
m3u8OutputPath: resolutionPlaylistFilePath,
|
|
|
|
videoOutputPath: videoFilePath
|
2023-04-21 14:55:10 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
await onTranscodingEnded({ isNewVideo: privatePayload.isNewVideo, moveVideoToNextState: true, video })
|
|
|
|
|
|
|
|
if (privatePayload.deleteWebVideoFiles === true) {
|
|
|
|
logger.info('Removing web video files of %s now we have a HLS version of it.', video.uuid, this.lTags(video.uuid))
|
|
|
|
|
2023-07-11 09:21:13 +02:00
|
|
|
await removeAllWebVideoFiles(video)
|
2023-04-21 14:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Runner VOD HLS job %s for %s ended.', runnerJob.uuid, video.uuid, this.lTags(runnerJob.uuid, video.uuid))
|
|
|
|
}
|
|
|
|
}
|