Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

3.4 KB
Raw Blame
import { pick } from '@peertube/peertube-core-utils'
import {
  RunnerJobUpdatePayload,
  RunnerJobVODWebVideoTranscodingPayload,
  RunnerJobVODWebVideoTranscodingPrivatePayload,
  VideoResolution,
  VODWebVideoTranscodingSuccess
} from '@peertube/peertube-models'
import { buildUUID } from '@peertube/peertube-node-utils'
import { createLogger } from '@server/helpers/logger.js'
import { VideoJobInfoModel } from '@server/models/video/video-job-info.js'
import { MVideoWithFile } from '@server/types/models/index.js'
import { MRunnerJob } from '@server/types/models/runners/index.js'
import { generateRunnerTranscodingInputFileUrl } from '../runner-urls.js'
import { AbstractVODTranscodingJobHandler } from './abstract-vod-transcoding-job-handler.js'
import { loadRunnerVideo, onVODWebVideoOrAudioMergeTranscodingJob } from './shared/utils.js'

const logger = createLogger('vod', 'transcoding')

type CreateOptions = {
  video: MVideoWithFile
  resolution: number
  fps: number
  priority: number
  deleteInputFileId: number | null
  canMoveVideoState: boolean
  dependsOnRunnerJob?: MRunnerJob
}

// oxlint-disable-next-line max-len
export class VODWebVideoTranscodingJobHandler
  extends AbstractVODTranscodingJobHandler<CreateOptions, RunnerJobUpdatePayload, VODWebVideoTranscodingSuccess>
{
  create (options: CreateOptions) {
    const { video, resolution, fps, priority, dependsOnRunnerJob } = options

    const jobUUID = buildUUID()

    return logger.withContext([ jobUUID, video.uuid ], async () => {
      const { separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()

      const payload: RunnerJobVODWebVideoTranscodingPayload = {
        input: {
          videoFileUrl: generateRunnerTranscodingInputFileUrl({
            jobUUID,
            videoUUID: video.uuid,
            type: resolution === VideoResolution.H_NOVIDEO
              ? 'audio'
              : 'video'
          }),

          separatedAudioFileUrl: separatedAudioFile
            ? [ generateRunnerTranscodingInputFileUrl({ jobUUID, videoUUID: video.uuid, type: 'audio' }) ]
            : []
        },
        output: {
          resolution,
          fps
        }
      }

      const privatePayload: RunnerJobVODWebVideoTranscodingPrivatePayload = {
        ...pick(options, [ 'deleteInputFileId', 'canMoveVideoState' ]),

        videoUUID: video.uuid
      }

      const job = await this.createRunnerJob({
        type: 'vod-web-video-transcoding',
        jobUUID,
        payload,
        privatePayload,
        dependsOnRunnerJob,
        priority
      })

      await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingTranscode')

      return job
    })
  }

  // ---------------------------------------------------------------------------

  protected async specificComplete (options: {
    runnerJob: MRunnerJob
    resultPayload: VODWebVideoTranscodingSuccess
  }) {
    const { runnerJob, resultPayload } = options
    const privatePayload = runnerJob.privatePayload as RunnerJobVODWebVideoTranscodingPrivatePayload

    const video = await loadRunnerVideo(runnerJob)
    if (!video) return

    await logger.withContext([ video.uuid ], async () => {
      const videoFilePath = resultPayload.videoFile as string

      await onVODWebVideoOrAudioMergeTranscodingJob({ video, videoFilePath, privatePayload, wasAudioFile: false })

      logger.info('Runner VOD web video transcoding job %s for %s ended.', runnerJob.uuid, video.uuid)
    })
  }
}