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