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