1 import { move } from 'fs-extra'
2 import { dirname, join } from 'path'
3 import { logger } from '@server/helpers/logger'
4 import { renameVideoFileInPlaylist } from '@server/lib/hls'
5 import { getHlsResolutionPlaylistFilename } from '@server/lib/paths'
6 import { onTranscodingEnded } from '@server/lib/transcoding/ended-transcoding'
7 import { onHLSVideoFileTranscoding } from '@server/lib/transcoding/hls-transcoding'
8 import { buildNewFile, removeAllWebTorrentFiles } from '@server/lib/video-file'
9 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
10 import { MVideo } from '@server/types/models'
11 import { MRunnerJob } from '@server/types/models/runners'
12 import { pick } from '@shared/core-utils'
13 import { buildUUID } from '@shared/extra-utils'
15 RunnerJobUpdatePayload,
16 RunnerJobVODHLSTranscodingPayload,
17 RunnerJobVODHLSTranscodingPrivatePayload,
18 VODHLSTranscodingSuccess
19 } from '@shared/models'
20 import { generateRunnerTranscodingVideoInputFileUrl } from '../runner-urls'
21 import { AbstractVODTranscodingJobHandler } from './abstract-vod-transcoding-job-handler'
22 import { loadTranscodingRunnerVideo } from './shared'
24 type CreateOptions = {
27 deleteWebVideoFiles: boolean
31 dependsOnRunnerJob?: MRunnerJob
34 // eslint-disable-next-line max-len
35 export class VODHLSTranscodingJobHandler extends AbstractVODTranscodingJobHandler<CreateOptions, RunnerJobUpdatePayload, VODHLSTranscodingSuccess> {
37 async create (options: CreateOptions) {
38 const { video, resolution, fps, dependsOnRunnerJob, priority } = options
40 const jobUUID = buildUUID()
42 const payload: RunnerJobVODHLSTranscodingPayload = {
44 videoFileUrl: generateRunnerTranscodingVideoInputFileUrl(jobUUID, video.uuid)
52 const privatePayload: RunnerJobVODHLSTranscodingPrivatePayload = {
53 ...pick(options, [ 'isNewVideo', 'deleteWebVideoFiles' ]),
58 const job = await this.createRunnerJob({
59 type: 'vod-hls-transcoding',
67 await VideoJobInfoModel.increaseOrCreate(video.uuid, 'pendingTranscode')
72 // ---------------------------------------------------------------------------
74 protected async specificComplete (options: {
76 resultPayload: VODHLSTranscodingSuccess
78 const { runnerJob, resultPayload } = options
79 const privatePayload = runnerJob.privatePayload as RunnerJobVODHLSTranscodingPrivatePayload
81 const video = await loadTranscodingRunnerVideo(runnerJob, this.lTags)
84 const videoFilePath = resultPayload.videoFile as string
85 const resolutionPlaylistFilePath = resultPayload.resolutionPlaylistFile as string
87 const videoFile = await buildNewFile({ path: videoFilePath, mode: 'hls' })
88 const newVideoFilePath = join(dirname(videoFilePath), videoFile.filename)
89 await move(videoFilePath, newVideoFilePath)
91 const resolutionPlaylistFilename = getHlsResolutionPlaylistFilename(videoFile.filename)
92 const newResolutionPlaylistFilePath = join(dirname(resolutionPlaylistFilePath), resolutionPlaylistFilename)
93 await move(resolutionPlaylistFilePath, newResolutionPlaylistFilePath)
95 await renameVideoFileInPlaylist(newResolutionPlaylistFilePath, videoFile.filename)
97 await onHLSVideoFileTranscoding({
100 m3u8OutputPath: newResolutionPlaylistFilePath,
101 videoOutputPath: newVideoFilePath
104 await onTranscodingEnded({ isNewVideo: privatePayload.isNewVideo, moveVideoToNextState: true, video })
106 if (privatePayload.deleteWebVideoFiles === true) {
107 logger.info('Removing web video files of %s now we have a HLS version of it.', video.uuid, this.lTags(video.uuid))
109 await removeAllWebTorrentFiles(video)
112 logger.info('Runner VOD HLS job %s for %s ended.', runnerJob.uuid, video.uuid, this.lTags(runnerJob.uuid, video.uuid))