]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/optimize-old-videos.ts
Try something that could work with weblate
[github/Chocobozzz/PeerTube.git] / scripts / optimize-old-videos.ts
CommitLineData
2aaa1a3f
C
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
74dc3bca
C
4import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
5import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
edb4ffc7 6import { getMaxBitrate } from '../shared/models/videos'
edb4ffc7 7import { VideoModel } from '../server/models/video/video'
edb4ffc7 8import { optimizeVideofile } from '../server/lib/video-transcoding'
74cd011b 9import { initDatabaseModels } from '../server/initializers'
74dc3bca
C
10import { basename, dirname, join } from 'path'
11import { copy, move, remove } from 'fs-extra'
12import { CONFIG } from '../server/initializers/config'
edb4ffc7
FA
13
14run()
15 .then(() => process.exit(0))
16 .catch(err => {
17 console.error(err)
18 process.exit(-1)
19 })
20
5e10e8d7
FA
21let currentVideoId = null
22let currentFile = null
23
24process.on('SIGINT', async function () {
25 console.log('Cleaning up temp files')
26 await remove(`${currentFile}_backup`)
27 await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`)
28 process.exit(0)
29})
30
edb4ffc7 31async function run () {
74cd011b
C
32 await initDatabaseModels(true)
33
9f1ddd24 34 const localVideos = await VideoModel.listLocal()
edb4ffc7 35
9f1ddd24 36 for (const video of localVideos) {
5e10e8d7 37 currentVideoId = video.id
5b77537c 38
9f1ddd24 39 for (const file of video.VideoFiles) {
5e10e8d7 40 currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
9f1ddd24
C
41
42 const [ videoBitrate, fps, resolution ] = await Promise.all([
5e10e8d7
FA
43 getVideoFileBitrate(currentFile),
44 getVideoFileFPS(currentFile),
45 getVideoFileResolution(currentFile)
9f1ddd24
C
46 ])
47
5e10e8d7
FA
48 const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
49 const isMaxBitrateExceeded = videoBitrate > maxBitrate
9f1ddd24 50 if (isMaxBitrateExceeded) {
5b77537c
C
51 console.log(
52 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
53 basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
54 )
55
5e10e8d7
FA
56 const backupFile = `${currentFile}_backup`
57 await copy(currentFile, backupFile)
5b77537c 58
9f1ddd24 59 await optimizeVideofile(video, file)
5b77537c 60
5e10e8d7
FA
61 const originalDuration = await getDurationFromVideoFile(backupFile)
62 const newDuration = await getDurationFromVideoFile(currentFile)
5b77537c 63
5e10e8d7
FA
64 if (originalDuration === newDuration) {
65 console.log('Finished optimizing %s', basename(currentFile))
66 await remove(backupFile)
51326912 67 continue
5e10e8d7 68 }
5b77537c
C
69
70 console.log('Failed to optimize %s, restoring original', basename(currentFile))
71 await move(backupFile, currentFile, { overwrite: true })
72 await video.createTorrentAndSetInfoHash(file)
73 await file.save()
9f1ddd24 74 }
edb4ffc7
FA
75 }
76 }
9f1ddd24 77
edb4ffc7
FA
78 console.log('Finished optimizing videos')
79}