1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 18:29:27 +02:00
Peertube/server/core/lib/runners/job-handlers/vod-hls-transcoding-job-handler.ts
Chocobozzz 816f346a60 Separate HLS audio and video streams
Allows:
  * The HLS player to propose an "Audio only" resolution
  * The live to output an "Audio only" resolution
  * The live to ingest and output an "Audio only" stream

 This feature is under a config for VOD videos and is enabled by default for lives

 In the future we can imagine:
  * To propose multiple audio streams for a specific video
  * To ingest an audio only VOD and just output an audio only "video"
    (the player would play the audio file and PeerTube would not
    generate additional resolutions)

This commit introduce a new way to download videos:
 * Add "/download/videos/generate/:videoId" endpoint where PeerTube can
   mux an audio only and a video only file to a mp4 container
 * The download client modal introduces a new default panel where the
   user can choose resolutions it wants to download
2024-08-05 08:31:59 +02:00

107 lines
3.7 KiB
TypeScript

import { pick } from '@peertube/peertube-core-utils'
import {
RunnerJobUpdatePayload,
RunnerJobVODHLSTranscodingPayload,
RunnerJobVODHLSTranscodingPrivatePayload,
VODHLSTranscodingSuccess
} from '@peertube/peertube-models'
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'
import { MVideoWithFile } from '@server/types/models/index.js'
import { MRunnerJob } from '@server/types/models/runners/index.js'
import { generateRunnerTranscodingAudioInputFileUrl, generateRunnerTranscodingVideoInputFileUrl } from '../runner-urls.js'
import { AbstractVODTranscodingJobHandler } from './abstract-vod-transcoding-job-handler.js'
import { loadRunnerVideo } from './shared/utils.js'
type CreateOptions = {
video: MVideoWithFile
isNewVideo: boolean
deleteWebVideoFiles: boolean
resolution: number
fps: number
priority: number
separatedAudio: boolean
dependsOnRunnerJob?: MRunnerJob
}
// eslint-disable-next-line max-len
export class VODHLSTranscodingJobHandler extends AbstractVODTranscodingJobHandler<CreateOptions, RunnerJobUpdatePayload, VODHLSTranscodingSuccess> {
async create (options: CreateOptions) {
const { video, resolution, fps, dependsOnRunnerJob, separatedAudio, priority } = options
const jobUUID = buildUUID()
const { separatedAudioFile } = video.getMaxQualityAudioAndVideoFiles()
const payload: RunnerJobVODHLSTranscodingPayload = {
input: {
videoFileUrl: generateRunnerTranscodingVideoInputFileUrl(jobUUID, video.uuid),
separatedAudioFileUrl: separatedAudioFile
? [ generateRunnerTranscodingAudioInputFileUrl(jobUUID, video.uuid) ]
: []
},
output: {
resolution,
fps,
separatedAudio
}
}
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
}
// ---------------------------------------------------------------------------
protected async specificComplete (options: {
runnerJob: MRunnerJob
resultPayload: VODHLSTranscodingSuccess
}) {
const { runnerJob, resultPayload } = options
const privatePayload = runnerJob.privatePayload as RunnerJobVODHLSTranscodingPrivatePayload
const video = await loadRunnerVideo(runnerJob, this.lTags)
if (!video) return
const videoFilePath = resultPayload.videoFile as string
const resolutionPlaylistFilePath = resultPayload.resolutionPlaylistFile as string
await onHLSVideoFileTranscoding({
video,
m3u8OutputPath: resolutionPlaylistFilePath,
videoOutputPath: videoFilePath
})
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))
await removeAllWebVideoFiles(video)
}
logger.info('Runner VOD HLS job %s for %s ended.', runnerJob.uuid, video.uuid, this.lTags(runnerJob.uuid, video.uuid))
}
}