2021-08-27 14:32:44 +02:00
|
|
|
import { Job } from 'bull'
|
2021-01-21 15:58:17 +01:00
|
|
|
import { TranscodeOptionsType } from '@server/helpers/ffmpeg-utils'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { addTranscodingJob, getTranscodingJobPriority } from '@server/lib/video'
|
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager'
|
|
|
|
import { moveToNextState } from '@server/lib/video-state'
|
2021-05-11 11:15:29 +02:00
|
|
|
import { UserModel } from '@server/models/user/user'
|
2021-08-17 08:26:20 +02:00
|
|
|
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
|
|
|
|
import { MUser, MUserId, MVideo, MVideoFullLight, MVideoWithFile } from '@server/types/models'
|
2020-04-23 09:32:53 +02:00
|
|
|
import {
|
2021-01-21 15:58:17 +01:00
|
|
|
HLSTranscodingPayload,
|
2020-04-23 09:32:53 +02:00
|
|
|
MergeAudioTranscodingPayload,
|
|
|
|
NewResolutionTranscodingPayload,
|
|
|
|
OptimizeTranscodingPayload,
|
|
|
|
VideoTranscodingPayload
|
|
|
|
} from '../../../../shared'
|
2020-10-26 16:44:23 +01:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2020-11-20 17:16:55 +01:00
|
|
|
import { computeResolutionsToTranscode } from '../../../helpers/ffprobe-utils'
|
2021-08-26 09:18:57 +02:00
|
|
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
2020-10-26 16:44:23 +01:00
|
|
|
import { CONFIG } from '../../../initializers/config'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2021-01-21 15:58:17 +01:00
|
|
|
import {
|
|
|
|
generateHlsPlaylistResolution,
|
|
|
|
mergeAudioVideofile,
|
|
|
|
optimizeOriginalVideofile,
|
|
|
|
transcodeNewWebTorrentResolution
|
2021-05-11 10:57:25 +02:00
|
|
|
} from '../../transcoding/video-transcoding'
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
type HandlerFunction = (job: Job, payload: VideoTranscodingPayload, video: MVideoFullLight, user: MUser) => Promise<void>
|
2021-01-21 16:57:21 +01:00
|
|
|
|
2021-08-05 14:29:44 +02:00
|
|
|
const handlers: { [ id in VideoTranscodingPayload['type'] ]: HandlerFunction } = {
|
2021-01-21 15:58:17 +01:00
|
|
|
'new-resolution-to-hls': handleHLSJob,
|
|
|
|
'new-resolution-to-webtorrent': handleNewWebTorrentResolutionJob,
|
|
|
|
'merge-audio-to-webtorrent': handleWebTorrentMergeAudioJob,
|
|
|
|
'optimize-to-webtorrent': handleWebTorrentOptimizeJob
|
|
|
|
}
|
2017-10-02 12:20:26 +02:00
|
|
|
|
2021-08-26 09:18:57 +02:00
|
|
|
const lTags = loggerTagsFactory('transcoding')
|
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function processVideoTranscoding (job: Job) {
|
2019-03-19 17:00:08 +01:00
|
|
|
const payload = job.data as VideoTranscodingPayload
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Processing transcoding job %d.', job.id, lTags(payload.videoUUID))
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2018-09-18 12:00:49 +02:00
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoUUID)
|
2017-10-25 16:03:33 +02:00
|
|
|
// No video, maybe deleted?
|
|
|
|
if (!video) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Do not process job %d, video does not exist.', job.id, lTags(payload.videoUUID))
|
2017-10-25 16:03:33 +02:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
const handler = handlers[payload.type]
|
2020-10-26 16:44:23 +01:00
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
if (!handler) {
|
|
|
|
throw new Error('Cannot find transcoding handler for ' + payload.type)
|
|
|
|
}
|
2020-10-26 16:44:23 +01:00
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
await handler(job, payload, video, user)
|
2021-01-21 15:58:17 +01:00
|
|
|
|
|
|
|
return video
|
|
|
|
}
|
2019-01-29 08:37:25 +01:00
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Job handlers
|
|
|
|
// ---------------------------------------------------------------------------
|
2018-06-12 20:04:58 +02:00
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function handleHLSJob (job: Job, payload: HLSTranscodingPayload, video: MVideoFullLight, user: MUser) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Handling HLS transcoding job for %s.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
const videoFileInput = payload.copyCodecs
|
|
|
|
? video.getWebTorrentFile(payload.resolution)
|
|
|
|
: video.getMaxQualityFile()
|
|
|
|
|
|
|
|
const videoOrStreamingPlaylist = videoFileInput.getVideoOrStreamingPlaylist()
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await VideoPathManager.Instance.makeAvailableVideoFile(videoOrStreamingPlaylist, videoFileInput, videoInputPath => {
|
|
|
|
return generateHlsPlaylistResolution({
|
|
|
|
video,
|
|
|
|
videoInputPath,
|
|
|
|
resolution: payload.resolution,
|
|
|
|
copyCodecs: payload.copyCodecs,
|
|
|
|
isPortraitMode: payload.isPortraitMode || false,
|
|
|
|
job
|
|
|
|
})
|
2021-01-21 15:58:17 +01:00
|
|
|
})
|
2019-05-16 16:55:34 +02:00
|
|
|
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('HLS transcoding job for %s ended.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-02-08 10:51:10 +01:00
|
|
|
await retryTransactionWrapper(onHlsPlaylistGeneration, video, user, payload)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
2018-06-12 20:04:58 +02:00
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
async function handleNewWebTorrentResolutionJob (
|
2021-08-27 14:32:44 +02:00
|
|
|
job: Job,
|
2021-01-21 16:57:21 +01:00
|
|
|
payload: NewResolutionTranscodingPayload,
|
|
|
|
video: MVideoFullLight,
|
|
|
|
user: MUserId
|
|
|
|
) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Handling WebTorrent transcoding job for %s.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
await transcodeNewWebTorrentResolution(video, payload.resolution, payload.isPortraitMode || false, job)
|
2017-10-17 15:37:40 +02:00
|
|
|
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('WebTorrent transcoding job for %s ended.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
await retryTransactionWrapper(onNewWebTorrentFileResolution, video, user, payload)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function handleWebTorrentMergeAudioJob (job: Job, payload: MergeAudioTranscodingPayload, video: MVideoFullLight, user: MUserId) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Handling merge audio transcoding job for %s.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
await mergeAudioVideofile(video, payload.resolution, job)
|
|
|
|
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Merge audio transcoding job for %s ended.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-02-08 10:51:10 +01:00
|
|
|
await retryTransactionWrapper(onVideoFileOptimizer, video, payload, 'video', user)
|
2017-10-02 12:20:26 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:32:44 +02:00
|
|
|
async function handleWebTorrentOptimizeJob (job: Job, payload: OptimizeTranscodingPayload, video: MVideoFullLight, user: MUserId) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Handling optimize transcoding job for %s.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
const { transcodeType } = await optimizeOriginalVideofile(video, video.getMaxQualityFile(), job)
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('Optimize transcoding job for %s ended.', video.uuid, lTags(video.uuid))
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
await retryTransactionWrapper(onVideoFileOptimizer, video, payload, transcodeType, user)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-02-08 10:51:10 +01:00
|
|
|
async function onHlsPlaylistGeneration (video: MVideoFullLight, user: MUser, payload: HLSTranscodingPayload) {
|
|
|
|
if (payload.isMaxQuality && CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
|
|
|
|
// Remove webtorrent files if not enabled
|
2019-11-15 15:06:03 +01:00
|
|
|
for (const file of video.VideoFiles) {
|
2021-07-23 11:20:00 +02:00
|
|
|
await video.removeFileAndTorrent(file)
|
2019-11-15 15:06:03 +01:00
|
|
|
await file.destroy()
|
2018-06-28 16:58:15 +02:00
|
|
|
}
|
2018-06-12 20:04:58 +02:00
|
|
|
|
2019-11-15 15:06:03 +01:00
|
|
|
video.VideoFiles = []
|
2021-02-08 10:51:10 +01:00
|
|
|
|
|
|
|
// Create HLS new resolution jobs
|
2021-08-17 08:26:20 +02:00
|
|
|
await createLowerResolutionsJobs({
|
|
|
|
video,
|
|
|
|
user,
|
|
|
|
videoFileResolution: payload.resolution,
|
|
|
|
isPortraitMode: payload.isPortraitMode,
|
|
|
|
isNewVideo: payload.isNewVideo ?? true,
|
|
|
|
type: 'hls'
|
|
|
|
})
|
2019-11-15 15:06:03 +01:00
|
|
|
}
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
|
|
|
|
await moveToNextState(video, payload.isNewVideo)
|
2019-11-15 15:06:03 +01:00
|
|
|
}
|
2018-12-28 13:47:17 +01:00
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
async function onVideoFileOptimizer (
|
2020-12-22 15:42:02 +01:00
|
|
|
videoArg: MVideoWithFile,
|
2021-02-08 10:51:10 +01:00
|
|
|
payload: OptimizeTranscodingPayload | MergeAudioTranscodingPayload,
|
2021-01-21 16:57:21 +01:00
|
|
|
transcodeType: TranscodeOptionsType,
|
|
|
|
user: MUserId
|
2020-12-22 15:42:02 +01:00
|
|
|
) {
|
2021-08-06 13:35:25 +02:00
|
|
|
const { resolution, isPortraitMode } = await videoArg.getMaxQualityResolution()
|
2018-06-12 20:04:58 +02:00
|
|
|
|
2021-06-15 09:17:19 +02:00
|
|
|
// Maybe the video changed in database, refresh it
|
|
|
|
const videoDatabase = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoArg.uuid)
|
|
|
|
// Video does not exist anymore
|
|
|
|
if (!videoDatabase) return undefined
|
|
|
|
|
|
|
|
// Generate HLS version of the original file
|
2021-08-17 08:26:20 +02:00
|
|
|
const originalFileHLSPayload = {
|
|
|
|
...payload,
|
|
|
|
|
2021-06-15 09:17:19 +02:00
|
|
|
isPortraitMode,
|
|
|
|
resolution: videoDatabase.getMaxQualityFile().resolution,
|
|
|
|
// If we quick transcoded original file, force transcoding for HLS to avoid some weird playback issues
|
|
|
|
copyCodecs: transcodeType !== 'quick-transcode',
|
|
|
|
isMaxQuality: true
|
2021-08-17 08:26:20 +02:00
|
|
|
}
|
2021-06-15 09:17:19 +02:00
|
|
|
const hasHls = await createHlsJobIfEnabled(user, originalFileHLSPayload)
|
2021-08-17 08:26:20 +02:00
|
|
|
const hasNewResolutions = await createLowerResolutionsJobs({
|
|
|
|
video: videoDatabase,
|
|
|
|
user,
|
|
|
|
videoFileResolution: resolution,
|
|
|
|
isPortraitMode,
|
|
|
|
type: 'webtorrent',
|
|
|
|
isNewVideo: payload.isNewVideo ?? true
|
|
|
|
})
|
2018-03-19 15:02:36 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await VideoJobInfoModel.decrease(videoDatabase.uuid, 'pendingTranscode')
|
2018-12-28 13:47:17 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
// Move to next state if there are no other resolutions to generate
|
2021-06-15 09:17:19 +02:00
|
|
|
if (!hasHls && !hasNewResolutions) {
|
2021-08-17 08:26:20 +02:00
|
|
|
await moveToNextState(videoDatabase, payload.isNewVideo)
|
2021-06-15 09:17:19 +02:00
|
|
|
}
|
2017-10-02 12:20:26 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
async function onNewWebTorrentFileResolution (
|
2021-08-17 08:26:20 +02:00
|
|
|
video: MVideo,
|
2021-01-21 16:57:21 +01:00
|
|
|
user: MUserId,
|
2021-02-02 14:00:46 +01:00
|
|
|
payload: NewResolutionTranscodingPayload | MergeAudioTranscodingPayload
|
2021-01-21 15:58:17 +01:00
|
|
|
) {
|
2021-08-17 08:26:20 +02:00
|
|
|
await createHlsJobIfEnabled(user, { ...payload, copyCodecs: true, isMaxQuality: false })
|
|
|
|
await VideoJobInfoModel.decrease(video.uuid, 'pendingTranscode')
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await moveToNextState(video, payload.isNewVideo)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
async function createHlsJobIfEnabled (user: MUserId, payload: {
|
|
|
|
videoUUID: string
|
|
|
|
resolution: number
|
|
|
|
isPortraitMode?: boolean
|
|
|
|
copyCodecs: boolean
|
2021-02-08 10:51:10 +01:00
|
|
|
isMaxQuality: boolean
|
2021-08-17 08:26:20 +02:00
|
|
|
isNewVideo?: boolean
|
2021-01-21 16:57:21 +01:00
|
|
|
}) {
|
2021-08-17 08:26:20 +02:00
|
|
|
if (!payload || CONFIG.TRANSCODING.ENABLED !== true || CONFIG.TRANSCODING.HLS.ENABLED !== true) return false
|
2021-01-21 16:57:21 +01:00
|
|
|
|
|
|
|
const jobOptions = {
|
2021-05-05 09:25:11 +02:00
|
|
|
priority: await getTranscodingJobPriority(user)
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
2019-01-29 08:37:25 +01:00
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
const hlsTranscodingPayload: HLSTranscodingPayload = {
|
|
|
|
type: 'new-resolution-to-hls',
|
|
|
|
videoUUID: payload.videoUUID,
|
|
|
|
resolution: payload.resolution,
|
|
|
|
isPortraitMode: payload.isPortraitMode,
|
2021-02-08 10:51:10 +01:00
|
|
|
copyCodecs: payload.copyCodecs,
|
2021-08-17 08:26:20 +02:00
|
|
|
isMaxQuality: payload.isMaxQuality,
|
|
|
|
isNewVideo: payload.isNewVideo
|
2019-01-29 08:37:25 +01:00
|
|
|
}
|
2021-01-21 16:57:21 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await addTranscodingJob(hlsTranscodingPayload, jobOptions)
|
2021-02-22 10:33:33 +01:00
|
|
|
|
|
|
|
return true
|
2019-01-29 08:37:25 +01:00
|
|
|
}
|
2021-01-21 15:58:17 +01:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processVideoTranscoding,
|
|
|
|
createHlsJobIfEnabled,
|
|
|
|
onNewWebTorrentFileResolution
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function createLowerResolutionsJobs (options: {
|
|
|
|
video: MVideoFullLight
|
|
|
|
user: MUserId
|
|
|
|
videoFileResolution: number
|
|
|
|
isPortraitMode: boolean
|
|
|
|
isNewVideo: boolean
|
2021-02-08 10:51:10 +01:00
|
|
|
type: 'hls' | 'webtorrent'
|
2021-08-17 08:26:20 +02:00
|
|
|
}) {
|
|
|
|
const { video, user, videoFileResolution, isPortraitMode, isNewVideo, type } = options
|
|
|
|
|
2021-01-21 15:58:17 +01:00
|
|
|
// Create transcoding jobs if there are enabled resolutions
|
|
|
|
const resolutionsEnabled = computeResolutionsToTranscode(videoFileResolution, 'vod')
|
2021-08-26 09:18:57 +02:00
|
|
|
const resolutionCreated: string[] = []
|
2021-01-21 15:58:17 +01:00
|
|
|
|
|
|
|
for (const resolution of resolutionsEnabled) {
|
|
|
|
let dataInput: VideoTranscodingPayload
|
|
|
|
|
2021-02-08 10:51:10 +01:00
|
|
|
if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED && type === 'webtorrent') {
|
2021-01-21 15:58:17 +01:00
|
|
|
// WebTorrent will create subsequent HLS job
|
|
|
|
dataInput = {
|
|
|
|
type: 'new-resolution-to-webtorrent',
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
resolution,
|
2021-08-17 08:26:20 +02:00
|
|
|
isPortraitMode,
|
|
|
|
isNewVideo
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
2021-08-26 09:18:57 +02:00
|
|
|
|
|
|
|
resolutionCreated.push('webtorrent-' + resolution)
|
2021-02-08 10:51:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CONFIG.TRANSCODING.HLS.ENABLED && type === 'hls') {
|
2021-01-21 15:58:17 +01:00
|
|
|
dataInput = {
|
|
|
|
type: 'new-resolution-to-hls',
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
resolution,
|
|
|
|
isPortraitMode,
|
2021-02-08 10:51:10 +01:00
|
|
|
copyCodecs: false,
|
2021-08-17 08:26:20 +02:00
|
|
|
isMaxQuality: false,
|
|
|
|
isNewVideo
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
2021-08-26 09:18:57 +02:00
|
|
|
|
|
|
|
resolutionCreated.push('hls-' + resolution)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:33:33 +01:00
|
|
|
if (!dataInput) continue
|
|
|
|
|
2021-01-21 16:57:21 +01:00
|
|
|
const jobOptions = {
|
2021-05-05 09:25:11 +02:00
|
|
|
priority: await getTranscodingJobPriority(user)
|
2021-01-21 16:57:21 +01:00
|
|
|
}
|
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
await addTranscodingJob(dataInput, jobOptions)
|
2021-01-21 15:58:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:33:33 +01:00
|
|
|
if (resolutionCreated.length === 0) {
|
2021-08-26 09:18:57 +02:00
|
|
|
logger.info('No transcoding jobs created for video %s (no resolutions).', video.uuid, lTags(video.uuid))
|
2021-02-22 10:33:33 +01:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
'New resolutions %s transcoding jobs created for video %s and origin file resolution of %d.', type, video.uuid, videoFileResolution,
|
2021-08-26 09:18:57 +02:00
|
|
|
{ resolutionCreated, ...lTags(video.uuid) }
|
2021-02-22 10:33:33 +01:00
|
|
|
)
|
2021-01-21 15:58:17 +01:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|