X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=scripts%2Foptimize-old-videos.ts;h=bde9d1e016cc2d8ce8d179951ada961a8f9f36ef;hb=4c37cbfbe28eb1e51b63393f4497d975d70aa45d;hp=2c80f16bcf35e40384278b69d9f04d67e8a481b5;hpb=5b77537ce54832f47931ba47dc513be2a9197f92;p=github%2FChocobozzz%2FPeerTube.git diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts index 2c80f16bc..bde9d1e01 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/transcoding/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)) @@ -15,13 +19,13 @@ run() process.exit(-1) }) -let currentVideoId = null -let currentFile = null +let currentVideoId: string +let currentFilePath: string process.on('SIGINT', async function () { console.log('Cleaning up temp files') - await remove(`${currentFile}_backup`) - await remove(`${dirname(currentFile)}/${currentVideoId}-transcoded.mp4`) + await remove(`${currentFilePath}_backup`) + await remove(`${dirname(currentFilePath)}/${currentVideoId}-transcoded.mp4`) process.exit(0) }) @@ -30,16 +34,18 @@ 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)) + currentFilePath = getVideoFilePath(video, file) const [ videoBitrate, fps, resolution ] = await Promise.all([ - getVideoFileBitrate(currentFile), - getVideoFileFPS(currentFile), - getVideoFileResolution(currentFile) + getVideoFileBitrate(currentFilePath), + getVideoFileFPS(currentFilePath), + getVideoFileResolution(currentFilePath) ]) const maxBitrate = getMaxBitrate(resolution.videoFileResolution, fps, VIDEO_TRANSCODING_FPS) @@ -47,26 +53,28 @@ async function run () { if (isMaxBitrateExceeded) { console.log( 'Optimizing video file %s with bitrate %s kbps (max: %s kbps)', - basename(currentFile), videoBitrate / 1000, maxBitrate / 1000 + basename(currentFilePath), videoBitrate / 1000, maxBitrate / 1000 ) - const backupFile = `${currentFile}_backup` - await copy(currentFile, backupFile) + const backupFile = `${currentFilePath}_backup` + await copy(currentFilePath, backupFile) - await optimizeVideofile(video, file) + await optimizeOriginalVideofile(video, file) + // Update file path, the video filename changed + currentFilePath = getVideoFilePath(video, file) const originalDuration = await getDurationFromVideoFile(backupFile) - const newDuration = await getDurationFromVideoFile(currentFile) + const newDuration = await getDurationFromVideoFile(currentFilePath) if (originalDuration === newDuration) { - console.log('Finished optimizing %s', basename(currentFile)) + console.log('Finished optimizing %s', basename(currentFilePath)) await remove(backupFile) - return + continue } - console.log('Failed to optimize %s, restoring original', basename(currentFile)) - await move(backupFile, currentFile, { overwrite: true }) - await video.createTorrentAndSetInfoHash(file) + console.log('Failed to optimize %s, restoring original', basename(currentFilePath)) + await move(backupFile, currentFilePath, { overwrite: true }) + await createTorrentAndSetInfoHash(video, file) await file.save() } }