]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - scripts/optimize-old-videos.ts
Improve target bitrate calculation
[github/Chocobozzz/PeerTube.git] / scripts / optimize-old-videos.ts
index 01d30244fb62fe5f58bf73064f404791735ed35b..9e66105ddeb1e472c17d2ed8c6d7ffd167724586 100644 (file)
@@ -1,16 +1,15 @@
 import { registerTSPaths } from '../server/helpers/register-ts-paths'
 registerTSPaths()
 
-import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
 import { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-utils'
-import { getMaxBitrate } from '../shared/models/videos'
 import { VideoModel } from '../server/models/video/video'
-import { optimizeOriginalVideofile } from '../server/lib/video-transcoding'
+import { optimizeOriginalVideofile } from '../server/lib/transcoding/video-transcoding'
 import { initDatabaseModels } from '../server/initializers/database'
 import { basename, dirname } from 'path'
 import { copy, move, remove } from 'fs-extra'
 import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
 import { getVideoFilePath } from '@server/lib/video-paths'
+import { getMaxBitrate } from '@shared/core-utils'
 
 run()
   .then(() => process.exit(0))
@@ -19,13 +18,13 @@ run()
     process.exit(-1)
   })
 
-let currentVideoId = null
-let currentFile = null
+let currentVideoId: string
+let currentFilePath: string
 
 process.on('SIGINT', async function () {
   console.log('Cleaning up temp files')
-  await remove(`${currentFile}_backup`)
-  await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`)
+  await remove(`${currentFilePath}_backup`)
+  await remove(`${dirname(currentFilePath)}/${currentVideoId}-transcoded.mp4`)
   process.exit(0)
 })
 
@@ -40,38 +39,40 @@ async function run () {
     currentVideoId = video.id
 
     for (const file of video.VideoFiles) {
-      currentFile = getVideoFilePath(video, file)
+      currentFilePath = getVideoFilePath(video, file)
 
-      const [ videoBitrate, fps, resolution ] = await Promise.all([
-        getVideoFileBitrate(currentFile),
-        getVideoFileFPS(currentFile),
-        getVideoFileResolution(currentFile)
+      const [ videoBitrate, fps, dataResolution ] = await Promise.all([
+        getVideoFileBitrate(currentFilePath),
+        getVideoFileFPS(currentFilePath),
+        getVideoFileResolution(currentFilePath)
       ])
 
-      const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
+      const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
       const isMaxBitrateExceeded = videoBitrate > maxBitrate
       if (isMaxBitrateExceeded) {
         console.log(
           'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
-          basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
+          basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000
         )
 
-        const backupFile = `${currentFile}_backup`
-        await copy(currentFile, backupFile)
+        const backupFile = `${currentFilePath}_backup`
+        await copy(currentFilePath, backupFile)
 
         await optimizeOriginalVideofile(video, file)
+        // Update file path, the video filename changed
+        currentFilePath = getVideoFilePath(video, file)
 
         const originalDuration = await getDurationFromVideoFile(backupFile)
-        const newDuration = await getDurationFromVideoFile(currentFile)
+        const newDuration = await getDurationFromVideoFile(currentFilePath)
 
         if (originalDuration === newDuration) {
-          console.log('Finished optimizing %s', basename(currentFile))
+          console.log('Finished optimizing %s', basename(currentFilePath))
           await remove(backupFile)
           continue
         }
 
-        console.log('Failed to optimize %s, restoring original', basename(currentFile))
-        await move(backupFile, currentFile, { overwrite: true })
+        console.log('Failed to optimize %s, restoring original', basename(currentFilePath))
+        await move(backupFile, currentFilePath, { overwrite: true })
         await createTorrentAndSetInfoHash(video, file)
         await file.save()
       }