Skip to content

Commit

Permalink
server: validate video files after transcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kontrollanten committed Mar 18, 2022
1 parent 5357ce9 commit b12cdff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
35 changes: 35 additions & 0 deletions server/helpers/ffmpeg/ffmpeg-validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import ffmpeg from 'fluent-ffmpeg'
import { Job } from 'bull'
import { lTags } from '@server/lib/object-storage/shared'
import { logger } from '../logger'
import { runCommand } from './ffmpeg-commons'

async function validateVideoFile (options: {
job?: Job
path: string
}) {
logger.debug('Will validate video file.', { options, ...lTags() })

let validationFailed = false

const command = ffmpeg(options.path)
.addOption('-loglevel', 'error')
.addOption('-f', 'null')
.output('/dev/null')
.on('stderr', () => {
if (validationFailed) {
return command.kill('SIGKILL')
}
validationFailed = true
})

await runCommand({ command, job: options.job })

if (validationFailed) {
throw Error(`Video validation failed for file ${options.path}`)
}
}

export {
validateVideoFile
}
17 changes: 13 additions & 4 deletions server/lib/transcoding/transcoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '../paths'
import { VideoPathManager } from '../video-path-manager'
import { VideoTranscodingProfilesManager } from './default-transcoding-profiles'
import { validateVideoFile } from '@server/helpers/ffmpeg/ffmpeg-validate'

/**
*
Expand Down Expand Up @@ -66,7 +67,7 @@ function optimizeOriginalVideofile (video: MVideoFullLight, inputVideoFile: MVid
}

// Could be very long!
await transcodeVOD(transcodeOptions)
await transcodeVODAndValidate(transcodeOptions)

// Important to do this before getVideoFilename() to take in account the new filename
inputVideoFile.extname = newExtname
Expand Down Expand Up @@ -128,7 +129,7 @@ function transcodeNewWebTorrentResolution (video: MVideoFullLight, resolution: V
job
}

await transcodeVOD(transcodeOptions)
await transcodeVODAndValidate(transcodeOptions)

return onWebTorrentVideoFileTranscoding(video, newVideoFile, videoTranscodedPath, videoOutputPath)
})
Expand Down Expand Up @@ -165,7 +166,7 @@ function mergeAudioVideofile (video: MVideoFullLight, resolution: VideoResolutio
}

try {
await transcodeVOD(transcodeOptions)
await transcodeVODAndValidate(transcodeOptions)

await remove(audioInputPath)
await remove(tmpPreviewPath)
Expand Down Expand Up @@ -239,6 +240,14 @@ export {

// ---------------------------------------------------------------------------

async function transcodeVODAndValidate (transcodeOptions: TranscodeVODOptions) {
await transcodeVOD(transcodeOptions)
await validateVideoFile({
job: transcodeOptions.job,
path: transcodeOptions.outputPath
})
}

async function onWebTorrentVideoFileTranscoding (
video: MVideoFullLight,
videoFile: MVideoFile,
Expand Down Expand Up @@ -306,7 +315,7 @@ async function generateHlsPlaylistCommon (options: {
job
}

await transcodeVOD(transcodeOptions)
await transcodeVODAndValidate(transcodeOptions)

// Create or update the playlist
const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video)
Expand Down

0 comments on commit b12cdff

Please sign in to comment.