aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos/shared/upload.ts
blob: ea0dddc3c249f628daeb324cb09f4611dd04d1b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import express from 'express'
import { logger } from '@server/helpers/logger'
import { getVideoStreamDuration } from '@shared/ffmpeg'
import { HttpStatusCode } from '@shared/models'

export async function addDurationToVideoFileIfNeeded (options: {
  res: express.Response
  videoFile: { path: string, duration?: number }
  middlewareName: string
}) {
  const { res, middlewareName, videoFile } = options

  try {
    if (!videoFile.duration) await addDurationToVideo(videoFile)
  } catch (err) {
    logger.error('Invalid input file in ' + middlewareName, { err })

    res.fail({
      status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
      message: 'Video file unreadable.'
    })
    return false
  }

  return true
}

// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------

async function addDurationToVideo (videoFile: { path: string, duration?: number }) {
  const duration = await getVideoStreamDuration(videoFile.path)

  // FFmpeg may not be able to guess video duration
  // For example with m2v files: https://trac.ffmpeg.org/ticket/9726#comment:2
  if (isNaN(duration)) videoFile.duration = 0
  else videoFile.duration = duration
}