X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Foptimize-old-videos.ts;h=a84845068f5f24520b6d46c73e6f422a8871f5a5;hb=99ae9d3a9eeaf3db5c66bde006aded133757cb17;hp=ab44acfbeb755027daccccf87cac3bd2904cc558;hpb=edb4ffc7e0b13659d7c73b120f2c87b27e4c26a1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index ab44acfbe..a84845068 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts @@ -1,12 +1,16 @@ -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 { 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 { 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' +import { optimizeOriginalVideofile } from '../server/lib/video-transcoding' +import { initDatabaseModels } from '../server/initializers' +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)) @@ -15,22 +19,62 @@ run() process.exit(-1) }) +let currentVideoId = null +let currentFile = null + +process.on('SIGINT', async function () { + console.log('Cleaning up temp files') + await remove(`${currentFile}_backup`) + await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`) + process.exit(0) +}) + 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 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() + } } } + console.log('Finished optimizing videos') }