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