PeerTube/scripts/optimize-old-videos.ts

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-10-18 16:53:52 +02:00
import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
import { getMaxBitrate } from '../shared/models/videos'
import { VideoModel } from '../server/models/video/video'
import { optimizeVideofile } from '../server/lib/video-transcoding'
2018-10-18 16:53:52 +02:00
import { initDatabaseModels } from '../server/initializers'
import { join } from 'path'
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
})
async function run () {
2018-10-18 16:53:52 +02:00
await initDatabaseModels(true)
const localVideos = await VideoModel.listLocal()
for (const video of localVideos) {
for (const file of video.VideoFiles) {
2018-10-18 16:53:52 +02:00
const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
const [ videoBitrate, fps, resolution ] = await Promise.all([
getVideoFileBitrate(inputPath),
getVideoFileFPS(inputPath),
getVideoFileResolution(inputPath)
])
const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
if (isMaxBitrateExceeded) {
await optimizeVideofile(video, file)
}
}
}
console.log('Finished optimizing videos')
}