X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Foptimize-old-videos.ts;h=01d30244fb62fe5f58bf73064f404791735ed35b;hb=12fa2a6a9ab3af53e2f9461ed36f8c0a3f88776f;hp=a1d5345a1164a668ccaded484c407faf9dfdbc25;hpb=74dc3bca2b14f5fd3fe80c394dfc34177a46db77;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index a1d5345a1..01d30244f 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts @@ -1,12 +1,16 @@ +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 { getDurationFromVideoFile, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../server/helpers/ffprobe-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 { basename, dirname, join } from 'path' +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 { CONFIG } from '../server/initializers/config' +import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' +import { getVideoFilePath } from '@server/lib/video-paths' run() .then(() => process.exit(0)) @@ -30,10 +34,13 @@ async function run () { const localVideos = await VideoModel.listLocal() - for (const video of localVideos) { + for (const localVideo of localVideos) { + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(localVideo.id) + 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), @@ -44,22 +51,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() } } }