From 64610714f1b5cfd53242bef3c0550a97fe4e665c Mon Sep 17 00:00:00 2001 From: Djkato Date: Sun, 19 Jun 2022 02:29:02 +0200 Subject: [PATCH] removed params, cleanup --- lib/encoder.js | 111 +++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 58 deletions(-) diff --git a/lib/encoder.js b/lib/encoder.js index 82acb60..09195cd 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -2,14 +2,10 @@ const { exec } = require('child_process') const termkit = require('terminal-kit') class Encoder { - currentSetting settings encoder - encodePresetIndexArg - constructor(settings, currentSetting, encodePresetIndexArg = undefined) { + constructor(settings, currentSetting) { this.settings = settings - this.currentSetting = currentSetting - this.encodePresetIndexArg = encodePresetIndexArg } /** @@ -51,89 +47,88 @@ class Encoder { async #constructVideoCommand(path, out) { let [duration, resolutionHeight] = await this.#getDurationAndResolution(path) - //Calculates video bitrate to fit right under 8mb 1:7 audio:video. 8Mb * 8 = 64000(8mb) - 1000 for overhead, *0.97 to leave space for container. + //Calculates video bitrate to fit right under 8mb 1:7 audio:video. 8Mb * 8 = 64000(8mb) - 1000 for overhead, *0.95 to leave space for container. const maxOpusBitrate = 256 //kbits - let audioBitRate = Math.round((62000 / 8 * 1 / duration) * 0.97) - let videoBitRate = Math.round((62000 / 8 * 7 / duration) * 0.97) - + let audioBitRate = Math.round((this.settings.size_limit / 8 * 1 / duration) * 0.95) + let videoBitRate = Math.round((this.settings.size_limit / 8 * 7 / duration) * 0.95) //if maxOpusBitrate reached, cap the audio bit rate and give the rest of the bits to video if (audioBitRate > maxOpusBitrate) { videoBitRate += audioBitRate - maxOpusBitrate audioBitRate = maxOpusBitrate } - //if command had argument of anotehr quality setting change to use that setting - if (this.encodePresetIndexArg) { - this.currentSetting = this.settings.presets[this.encodePresetIndexArg] - } let command = "" - let isTwoPass = true - /* REMOVING CRF AS ITS NO LONGER USED -- REPLACE WITH -qmin equivalent + let outputHeight + //resolution limiter based on video length. + resolutionHeight >= 480 && duration > 900 ? + outputHeight = 480 : + resolutionHeight >= 720 && duration > 600 ? + outputHeight = 720 : + resolutionHeight >= 1080 && duration > 150 ? + outputHeight = 1080 : outputHeight = resolutionHeight - //Compares current video height to CRFMAP to determine optimal CRF - while (resolutionHeight > this.currentSetting.crfMap[crfIndex].resolution) { - crfIndex++ - //if the resolution is still higher, just use highest res - if (!this.currentSetting.crfMap[crfIndex]?.resolution) { - crfIndex-- - break - } - } - */ - //realtime doesnt support two pass, so just use real time settings - if (this.currentSetting.deadline == "realtime") { - command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus ` - command += `-deadline ${this.currentSetting.deadline} ` - command += `-quality ${this.currentSetting.deadline} ` - command += `-cpu-used ${this.currentSetting.cpuUsed} ` - command += `-undershoot-pct 0 -overshoot-pct 0 ` - command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k ` - command += `-minrate ${Math.round(videoBitRate * 0.5)}k ` - command += `-maxrate ${Math.floor(videoBitRate * 1.4)}k ` - command += `-b:a ${audioBitRate}k ` - command += `-tile-columns 2 -threads 6 ` - command += `-qmax 60 ` - command += `-g 240 ` - command += `-row-mt 1 "${out}.webm" ` - //console.log(command) - return [command, duration, false] - } + //realtime doesnt support two pass, so just use real time settings + /** + * REMOVING SUPPORT FOR REALTIME, IMPOSSIBLE TO HAVE RT SPEED AND AIM AT 8MB + * + */ + // if (this.currentSetting.deadline == "realtime") { + // command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus ` + // command += `-deadline ${this.currentSetting.deadline} ` + // command += `-quality ${this.currentSetting.deadline} ` + // command += `-cpu-used ${this.currentSetting.cpuUsed} ` + // command += `-undershoot-pct 0 -overshoot-pct 0 ` + // command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k ` + // command += `-minrate ${Math.round(videoBitRate * 0.5)}k ` + // command += `-maxrate ${Math.floor(videoBitRate * 1.4)}k ` + // command += `-b:a ${audioBitRate}k ` + // command += `-tile-columns 2 -threads 6 ` + // command += `-qmax 60 ` + // command += `-g 240 ` + // command += `-row-mt 1 "${out}.webm" ` + // //console.log(command) + // return [command, duration, false] + // } //Pass 1 force to have good deadline and cpu-used 1 command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus ` + command += `-vf scale=-1:${outputHeight} ` command += `-deadline good ` command += `-quality good ` - command += `-cpu-used 1 ` + command += `-cpu-used 0 ` command += `-undershoot-pct 0 -overshoot-pct 0 ` - command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k ` + command += `-b:v ${Math.round(videoBitRate)}k ` command += `-minrate ${Math.round(videoBitRate * 0.5)}k ` - command += `-maxrate ${Math.floor(videoBitRate * 1.2)}k ` + command += `-maxrate ${Math.floor(videoBitRate * 1.45)}k ` command += `-b:a ${audioBitRate}k ` + command += `-row-mt 1 -tile-rows 2 ` + command += `-tile-columns 4 -threads 32 ` command += `-auto-alt-ref 6 ` command += `-qmax 60 ` command += `-g 240 ` - command += `-row-mt 1 -pass 1 -f webm NUL && ` + command += `-pass 1 -f webm NUL && ` //Pass 2 take in settings command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus ` - command += `-deadline ${this.currentSetting.deadline} ` - command += `-quality ${this.currentSetting.deadline} ` - command += `-cpu-used ${this.currentSetting.cpuUsed} ` + command += `-vf scale=-1:${outputHeight} ` + command += `-deadline good ` + command += `-quality good ` + command += `-cpu-used 0 ` command += `-undershoot-pct 0 -overshoot-pct 0 ` - command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k ` + command += `-b:v ${Math.round(videoBitRate)}k ` command += `-minrate ${Math.round(videoBitRate * 0.5)}k ` - command += `-maxrate ${Math.floor(videoBitRate * 1.4)}k ` + command += `-maxrate ${Math.floor(videoBitRate * 1.45)}k ` command += `-b:a ${audioBitRate}k ` - command += `-tile-columns 2 -threads 6 ` + command += `-row-mt 1 -tile-rows 2 ` + command += `-tile-columns 4 -threads 32 ` command += `-auto-alt-ref 6 ` command += `-qmax 60 ` command += `-g 240 ` - command += `-row-mt 1 -pass 2 "${out}.webm" ` - //console.log(command) - return [command, duration, isTwoPass] + command += `-pass 2 "${out}.webm" ` + return [command, duration] } async #getDurationAndResolution(file) { @@ -144,8 +139,8 @@ class Encoder { const seconds = arr[0] * 3600 + arr[1] * 60 + (+arr[2]) // converting to s //resolution height - const resolutionHeight = query.split("Stream #0:0")[1]?.split(",")[2].split(" ")[1].split("x")[1] - + let resolutionHeight = query.split("Stream #0:0")[1].split("kb/s")[0].split(",") + resolutionHeight = Number.parseInt(resolutionHeight[resolutionHeight.length - 2].split("x")[1]) return [Number.parseFloat(seconds), resolutionHeight] }