- 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 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)
+ await initDatabaseModels(true)
+
+ const localVideos = await VideoModel.listLocal()
+
+ for (const video of localVideos) {
+ currentVideoId = video.id
+
+ for (const file of video.VideoFiles) {
+ currentFile = getVideoFilePath(video, file)
+
+ const [ videoBitrate, fps, resolution ] = await Promise.all([
+ getVideoFileBitrate(currentFile),
+ getVideoFileFPS(currentFile),
+ getVideoFileResolution(currentFile)
+ ])
+
+ 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
+ )
+
+ const backupFile = `${currentFile}_backup`
+ await copy(currentFile, backupFile)
+
+ 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)
+ continue
+ }
+
+ console.log('Failed to optimize %s, restoring original', basename(currentFile))
+ await move(backupFile, currentFile, { overwrite: true })
+ await createTorrentAndSetInfoHash(video, file)
+ await file.save()
+ }