]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - scripts/optimize-old-videos.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / scripts / optimize-old-videos.ts
index 1bee1b0f3ba4e8f149789e348bdd7a9fde7804af..9595efd9c6623af298b4369d80db272bc2d89176 100644 (file)
@@ -1,11 +1,16 @@
-import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
-import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution, getDurationFromVideoFile } from '../server/helpers/ffmpeg-utils'
+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/ffmpeg-utils'
 import { getMaxBitrate } from '../shared/models/videos'
 import { VideoModel } from '../server/models/video/video'
-import { optimizeVideofile } from '../server/lib/video-transcoding'
-import { initDatabaseModels } from '../server/initializers'
-import { join, basename, dirname } from 'path'
-import { copy, remove, move } from 'fs-extra'
+import { optimizeOriginalVideofile } from '../server/lib/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'
 
 run()
   .then(() => process.exit(0))
@@ -31,8 +36,9 @@ async function run () {
 
   for (const video of localVideos) {
     currentVideoId = video.id
+
     for (const file of video.VideoFiles) {
-      currentFile = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file))
+      currentFile = getVideoFilePath(video, file)
 
       const [ videoBitrate, fps, resolution ] = await Promise.all([
         getVideoFileBitrate(currentFile),
@@ -43,22 +49,29 @@ async function run () {
       const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_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)
+        console.log(
+          'Optimizing video file %s with bitrate %s kbps (max: %s kbps)',
+          basename(currentFile), videoBitrate / 1000, maxBitrate / 1000
+        )
+
         const backupFile = `${currentFile}_backup`
         await copy(currentFile, backupFile)
-        await optimizeVideofile(video, file)
+
+        await optimizeOriginalVideofile(video, file)
+
         const originalDuration = await getDurationFromVideoFile(backupFile)
         const newDuration = await getDurationFromVideoFile(currentFile)
+
         if (originalDuration === newDuration) {
           console.log('Finished optimizing %s', basename(currentFile))
           await remove(backupFile)
-        } else {
-          console.log('Failed to optimize %s, restoring original', basename(currentFile))
-          move(backupFile, currentFile, { overwrite: true })
-          await video.createTorrentAndSetInfoHash(file)
-          await file.save()
+          continue
         }
+
+        console.log('Failed to optimize %s, restoring original', basename(currentFile))
+        await move(backupFile, currentFile, { overwrite: true })
+        await createTorrentAndSetInfoHash(video, file)
+        await file.save()
       }
     }
   }