]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/optimize-old-videos.ts
c93f8231661dd16da7d88e658c3f963b5fe841e5
[github/Chocobozzz/PeerTube.git] / scripts / optimize-old-videos.ts
1 import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
2 import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
3 import { getMaxBitrate } from '../shared/models/videos'
4 import { VideoModel } from '../server/models/video/video'
5 import { optimizeVideofile } from '../server/lib/video-transcoding'
6 import { initDatabaseModels } from '../server/initializers'
7 import { join } from 'path'
8
9 run()
10 .then(() => process.exit(0))
11 .catch(err => {
12 console.error(err)
13 process.exit(-1)
14 })
15
16 async function run () {
17 await initDatabaseModels(true)
18
19 const localVideos = await VideoModel.listLocal()
20
21 for (const video of localVideos) {
22 for (const file of video.VideoFiles) {
23 const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
24
25 const [ videoBitrate, fps, resolution ] = await Promise.all([
26 getVideoFileBitrate(inputPath),
27 getVideoFileFPS(inputPath),
28 getVideoFileResolution(inputPath)
29 ])
30
31 const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
32 if (isMaxBitrateExceeded) {
33 await optimizeVideofile(video, file)
34 }
35 }
36 }
37
38 console.log('Finished optimizing videos')
39 }