]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Change a little bit optimize-old-videos logic
authorChocobozzz <me@florianbigard.com>
Mon, 8 Oct 2018 14:50:56 +0000 (16:50 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 8 Oct 2018 14:50:56 +0000 (16:50 +0200)
scripts/optimize-old-videos.ts
server/lib/video-transcoding.ts
server/models/video/video.ts

index ab44acfbeb755027daccccf87cac3bd2904cc558..02026b3dadeb0a0e0d45a80def58a5d1b0f575ab 100644 (file)
@@ -1,11 +1,7 @@
-import { join } from 'path'
-import { readdir } from 'fs-extra'
-import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
-import { getVideoFileResolution, getVideoFileBitrate, getVideoFileFPS } from '../server/helpers/ffmpeg-utils'
+import { VIDEO_TRANSCODING_FPS } from '../server/initializers/constants'
+import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffmpeg-utils'
 import { getMaxBitrate } from '../shared/models/videos'
-import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy'
 import { VideoModel } from '../server/models/video/video'
-import { getUUIDFromFilename } from '../server/helpers/utils'
 import { optimizeVideofile } from '../server/lib/video-transcoding'
 
 run()
@@ -16,21 +12,24 @@ run()
   })
 
 async function run () {
-  const files = await readdir(CONFIG.STORAGE.VIDEOS_DIR)
-  for (const file of files) {
-    const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, file)
-    const videoBitrate = await getVideoFileBitrate(inputPath)
-    const fps = await getVideoFileFPS(inputPath)
-    const resolution = await getVideoFileResolution(inputPath)
-    const uuid = getUUIDFromFilename(file)
+  const localVideos = await VideoModel.listLocal()
 
-    const isLocalVideo = await VideoRedundancyModel.isLocalByVideoUUIDExists(uuid)
-    const isMaxBitrateExceeded =
-      videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
-    if (uuid && isLocalVideo && isMaxBitrateExceeded) {
-      const videoModel = await VideoModel.loadByUUIDWithFile(uuid)
-      await optimizeVideofile(videoModel, inputPath)
+  for (const video of localVideos) {
+    for (const file of video.VideoFiles) {
+      const inputPath = video.getVideoFilename(file)
+
+      const [ videoBitrate, fps, resolution ] = await Promise.all([
+        getVideoFileBitrate(inputPath),
+        getVideoFileFPS(inputPath),
+        getVideoFileResolution(inputPath)
+      ])
+
+      const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS)
+      if (isMaxBitrateExceeded) {
+        await optimizeVideofile(video, file)
+      }
     }
   }
+
   console.log('Finished optimizing videos')
 }
index 04cadf74bd26b54090132e75901790f4ec4f96c6..a78de61e5673052955b8da1d7c48f36441a8b4f8 100644 (file)
@@ -1,5 +1,5 @@
 import { CONFIG } from '../initializers'
-import { join, extname, basename } from 'path'
+import { extname, join } from 'path'
 import { getVideoFileFPS, getVideoFileResolution, transcode } from '../helpers/ffmpeg-utils'
 import { copy, remove, rename, stat } from 'fs-extra'
 import { logger } from '../helpers/logger'
@@ -7,16 +7,12 @@ import { VideoResolution } from '../../shared/models/videos'
 import { VideoFileModel } from '../models/video/video-file'
 import { VideoModel } from '../models/video/video'
 
-async function optimizeVideofile (video: VideoModel, videoInputPath?: string) {
+async function optimizeVideofile (video: VideoModel, inputVideoFileArg?: VideoFileModel) {
   const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR
   const newExtname = '.mp4'
-  let inputVideoFile = null
-  if (videoInputPath == null) {
-    inputVideoFile = video.getOriginalFile()
-    videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
-  } else {
-    inputVideoFile = basename(videoInputPath)
-  }
+
+  const inputVideoFile = inputVideoFileArg ? inputVideoFileArg : video.getOriginalFile()
+  const videoInputPath = join(videosDirectory, video.getVideoFilename(inputVideoFile))
   const videoTranscodedPath = join(videosDirectory, video.id + '-transcoded' + newExtname)
 
   const transcodeOptions = {
index 46d823240526ee7daddce7e371057070f07f397f..070ac762395014a7d8900743416c3629c2a2968b 100644 (file)
@@ -788,6 +788,16 @@ export class VideoModel extends Model<VideoModel> {
     return VideoModel.scope(ScopeNames.WITH_FILES).findAll()
   }
 
+  static listLocal () {
+    const query = {
+      where: {
+        remote: false
+      }
+    }
+
+    return VideoModel.scope(ScopeNames.WITH_FILES).findAll(query)
+  }
+
   static listAllAndSharedByActorForOutbox (actorId: number, start: number, count: number) {
     function getRawQuery (select: string) {
       const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +