From edb4ffc7e0b13659d7c73b120f2c87b27e4c26a1 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 8 Oct 2018 09:26:04 -0500 Subject: Set bitrate limits for transcoding (fixes #638) (#1135) * Set bitrate limits for transcoding (fixes #638) * added optimization script and test, changed stuff * fix test, improve docs * re-add optimize-old-videos script * added documentation * Don't optimize videos without valid UUID, or redundancy videos * move getUUIDFromFilename * fix tests? * update torrent and file size, some more fixes/improvements * use higher bitrate for high fps video, adjust bitrates * add test video * don't throw error if resolution is undefined * generate test fixture on the fly * use random noise video for bitrate test, add promise * shorten test video to avoid timeout * use existing function to optimize video * various fixes * increase test timeout * limit test fixture size, add link * test fixes * add await * more test fixes, add -b:v parameter * replace ffmpeg wiki link * fix ffmpeg params * fix unit test * add test fixture to .gitgnore * add video transcoding fps model * add missing file --- scripts/help.sh | 1 + scripts/optimize-old-videos.ts | 36 ++++++++++++++++++++++++++++++++++++ scripts/prune-storage.ts | 10 +--------- 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 scripts/optimize-old-videos.ts (limited to 'scripts') diff --git a/scripts/help.sh b/scripts/help.sh index 8ac090139..bc38bdb40 100755 --- a/scripts/help.sh +++ b/scripts/help.sh @@ -18,6 +18,7 @@ printf " reset-password -- -u [user] -> Reset the password of user [user]\n" printf " create-transcoding-job -- -v [video UUID] \n" printf " -> Create a transcoding job for a particular video\n" printf " prune-storage -> Delete (after confirmation) unknown video files/thumbnails/previews... (due to a bad video deletion, transcoding job not finished...)\n" +printf " optimize-old-videos -> Re-transcode videos that have a high bitrate, to make them suitable for streaming over slow connections" printf " dev -> Watch, run the livereload and run the server so that you can develop the application\n" printf " start -> Run the server\n" printf " update-host -> Upgrade scheme/host in torrent files according to the webserver configuration (config/ folder)\n" diff --git a/scripts/optimize-old-videos.ts b/scripts/optimize-old-videos.ts new file mode 100644 index 000000000..ab44acfbe --- /dev/null +++ b/scripts/optimize-old-videos.ts @@ -0,0 +1,36 @@ +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 { 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' + +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +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) + } + } + console.log('Finished optimizing videos') +} diff --git a/scripts/prune-storage.ts b/scripts/prune-storage.ts index 4088fa700..4ab0b4863 100755 --- a/scripts/prune-storage.ts +++ b/scripts/prune-storage.ts @@ -5,6 +5,7 @@ import { VideoModel } from '../server/models/video/video' import { initDatabaseModels } from '../server/initializers' import { remove, readdir } from 'fs-extra' import { VideoRedundancyModel } from '../server/models/redundancy/video-redundancy' +import { getUUIDFromFilename } from '../server/helpers/utils' run() .then(() => process.exit(0)) @@ -82,15 +83,6 @@ async function pruneDirectory (directory: string, onlyOwned = false) { return toDelete } -function getUUIDFromFilename (filename: string) { - const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ - const result = filename.match(regex) - - if (!result || Array.isArray(result) === false) return null - - return result[0] -} - async function askConfirmation () { return new Promise((res, rej) => { prompt.start() -- cgit v1.2.3