aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/optimize-old-videos.ts
blob: ab44acfbeb755027daccccf87cac3bd2904cc558 (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
import { join } from 'path'
import { readdir } from 'fs-extra'
import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
import { getVideoFileResolution, getVideoFileBitrate, getVideoFileFPS } from '../server/helpers/ffmpeg-utils'
import { getMaxBitrate } from '../shared/models/videos'
import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
import { VideoModel } from '../server/models/video/video'
import { getUUIDFromFilename } from '../server/helpers/utils'
import { optimizeVideofile } from '../server/lib/video-transcoding'

run()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(-1)
  })

async function run () {
  const files = await readdir(CONFIG.STORAGE.VIDEOS_DIR)
  for (const file of files) {
    const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, file)
    const videoBitrate = await getVideoFileBitrate(inputPath)
    const fps = await getVideoFileFPS(inputPath)
    const resolution = await getVideoFileResolution(inputPath)
    const uuid = getUUIDFromFilename(file)

    const isLocalVideo = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
    const isMaxBitrateExceeded =
      videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
    if (uuid && isLocalVideo && isMaxBitrateExceeded) {
      const videoModel = await VideoModel.loadByUUIDWithFile(uuid)
      await optimizeVideofile(videoModel, inputPath)
    }
  }
  console.log('Finished optimizing videos')
}