PeerTube/scripts/optimize-old-videos.ts
Felix Ableitner edb4ffc7e0 Set bitrate limits for transcoding (fixes #638) (#1135)
* Set bitrate limits for transcoding (fixes #638)

* added optimization script and test, changed stuff

* fix test, improve docs

* re-add optimize-old-videos script

* added documentation

* Don't optimize videos without valid UUID, or redundancy videos

* move getUUIDFromFilename

* fix tests?

* update torrent and file size, some more fixes/improvements

* use higher bitrate for high fps video, adjust bitrates

* add test video

* don't throw error if resolution is undefined

* generate test fixture on the fly

* use random noise video for bitrate test, add promise

* shorten test video to avoid timeout

* use existing function to optimize video

* various fixes

* increase test timeout

* limit test fixture size, add link

* test fixes

* add await

* more test fixes, add -b:v parameter

* replace ffmpeg wiki link

* fix ffmpeg params

* fix unit test

* add test fixture to .gitgnore

* add video transcoding fps model

* add missing file
2018-10-08 16:26:04 +02:00

36 lines
1.5 KiB
TypeScript

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')
}