X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Foptimize-old-videos.ts;h=9595efd9c6623af298b4369d80db272bc2d89176;hb=8797d1042f1733b589670675a929ec2e544c4b1c;hp=c93f8231661dd16da7d88e658c3f963b5fe841e5;hpb=6d8c8ea73a774c3568e6d28a4cbebcf7979d5c2a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index c93f82316..9595efd9c 100644 --- a/scripts/optimize-old-videos.ts +++ b/scripts/optimize-old-videos.ts @@ -1,10 +1,16 @@ -import { CONFIG, VIDEO_TRANSCODING_FPS } from '../server/initializers/constants' -import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } 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 } 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 { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' +import { getVideoFilePath } from '@server/lib/video-paths' run() .then(() => process.exit(0)) @@ -13,24 +19,59 @@ 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 () { await initDatabaseModels(true) const localVideos = await VideoModel.listLocal() for (const video of localVideos) { + currentVideoId = video.id + for (const file of video.VideoFiles) { - const inputPath = join(CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename(file)) + currentFile = getVideoFilePath(video, file) const [ videoBitrate, fps, resolution ] = await Promise.all([ - getVideoFileBitrate(inputPath), - getVideoFileFPS(inputPath), - getVideoFileResolution(inputPath) + getVideoFileBitrate(currentFile), + getVideoFileFPS(currentFile), + getVideoFileResolution(currentFile) ]) - const isMaxBitrateExceeded = videoBitrate > getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) + const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) + const isMaxBitrateExceeded = videoBitrate > maxBitrate if (isMaxBitrateExceeded) { - await optimizeVideofile(video, file) + 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() } } }