removed params, cleanup
This commit is contained in:
parent
bc1ecdd7e8
commit
64610714f1
1 changed files with 53 additions and 58 deletions
109
lib/encoder.js
109
lib/encoder.js
|
@ -2,14 +2,10 @@ const { exec } = require('child_process')
|
||||||
const termkit = require('terminal-kit')
|
const termkit = require('terminal-kit')
|
||||||
|
|
||||||
class Encoder {
|
class Encoder {
|
||||||
currentSetting
|
|
||||||
settings
|
settings
|
||||||
encoder
|
encoder
|
||||||
encodePresetIndexArg
|
constructor(settings, currentSetting) {
|
||||||
constructor(settings, currentSetting, encodePresetIndexArg = undefined) {
|
|
||||||
this.settings = settings
|
this.settings = settings
|
||||||
this.currentSetting = currentSetting
|
|
||||||
this.encodePresetIndexArg = encodePresetIndexArg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,89 +47,88 @@ class Encoder {
|
||||||
async #constructVideoCommand(path, out) {
|
async #constructVideoCommand(path, out) {
|
||||||
let [duration, resolutionHeight] = await this.#getDurationAndResolution(path)
|
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
|
const maxOpusBitrate = 256 //kbits
|
||||||
|
|
||||||
let audioBitRate = Math.round((62000 / 8 * 1 / duration) * 0.97)
|
let audioBitRate = Math.round((this.settings.size_limit / 8 * 1 / duration) * 0.95)
|
||||||
let videoBitRate = Math.round((62000 / 8 * 7 / duration) * 0.97)
|
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 maxOpusBitrate reached, cap the audio bit rate and give the rest of the bits to video
|
||||||
if (audioBitRate > maxOpusBitrate) {
|
if (audioBitRate > maxOpusBitrate) {
|
||||||
videoBitRate += audioBitRate - maxOpusBitrate
|
videoBitRate += audioBitRate - maxOpusBitrate
|
||||||
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 command = ""
|
||||||
let isTwoPass = true
|
let outputHeight
|
||||||
/* REMOVING CRF AS ITS NO LONGER USED -- REPLACE WITH -qmin equivalent
|
//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
|
//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 `
|
* REMOVING SUPPORT FOR REALTIME, IMPOSSIBLE TO HAVE RT SPEED AND AIM AT 8MB
|
||||||
command += `-deadline ${this.currentSetting.deadline} `
|
*
|
||||||
command += `-quality ${this.currentSetting.deadline} `
|
*/
|
||||||
command += `-cpu-used ${this.currentSetting.cpuUsed} `
|
// if (this.currentSetting.deadline == "realtime") {
|
||||||
command += `-undershoot-pct 0 -overshoot-pct 0 `
|
// command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus `
|
||||||
command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k `
|
// command += `-deadline ${this.currentSetting.deadline} `
|
||||||
command += `-minrate ${Math.round(videoBitRate * 0.5)}k `
|
// command += `-quality ${this.currentSetting.deadline} `
|
||||||
command += `-maxrate ${Math.floor(videoBitRate * 1.4)}k `
|
// command += `-cpu-used ${this.currentSetting.cpuUsed} `
|
||||||
command += `-b:a ${audioBitRate}k `
|
// command += `-undershoot-pct 0 -overshoot-pct 0 `
|
||||||
command += `-tile-columns 2 -threads 6 `
|
// command += `-b:v ${Math.round(videoBitRate / 100 * this.currentSetting.bitrateError)}k `
|
||||||
command += `-qmax 60 `
|
// command += `-minrate ${Math.round(videoBitRate * 0.5)}k `
|
||||||
command += `-g 240 `
|
// command += `-maxrate ${Math.floor(videoBitRate * 1.4)}k `
|
||||||
command += `-row-mt 1 "${out}.webm" `
|
// command += `-b:a ${audioBitRate}k `
|
||||||
//console.log(command)
|
// command += `-tile-columns 2 -threads 6 `
|
||||||
return [command, duration, false]
|
// 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
|
//Pass 1 force to have good deadline and cpu-used 1
|
||||||
command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus `
|
command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus `
|
||||||
|
command += `-vf scale=-1:${outputHeight} `
|
||||||
command += `-deadline good `
|
command += `-deadline good `
|
||||||
command += `-quality good `
|
command += `-quality good `
|
||||||
command += `-cpu-used 1 `
|
command += `-cpu-used 0 `
|
||||||
command += `-undershoot-pct 0 -overshoot-pct 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 += `-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 += `-b:a ${audioBitRate}k `
|
||||||
|
command += `-row-mt 1 -tile-rows 2 `
|
||||||
|
command += `-tile-columns 4 -threads 32 `
|
||||||
command += `-auto-alt-ref 6 `
|
command += `-auto-alt-ref 6 `
|
||||||
command += `-qmax 60 `
|
command += `-qmax 60 `
|
||||||
command += `-g 240 `
|
command += `-g 240 `
|
||||||
|
|
||||||
command += `-row-mt 1 -pass 1 -f webm NUL && `
|
command += `-pass 1 -f webm NUL && `
|
||||||
|
|
||||||
//Pass 2 take in settings
|
//Pass 2 take in settings
|
||||||
command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus `
|
command += `ffmpeg -y -i "${path}" -vcodec libvpx-vp9 -acodec libopus `
|
||||||
command += `-deadline ${this.currentSetting.deadline} `
|
command += `-vf scale=-1:${outputHeight} `
|
||||||
command += `-quality ${this.currentSetting.deadline} `
|
command += `-deadline good `
|
||||||
command += `-cpu-used ${this.currentSetting.cpuUsed} `
|
command += `-quality good `
|
||||||
|
command += `-cpu-used 0 `
|
||||||
command += `-undershoot-pct 0 -overshoot-pct 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 += `-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 += `-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 += `-auto-alt-ref 6 `
|
||||||
command += `-qmax 60 `
|
command += `-qmax 60 `
|
||||||
command += `-g 240 `
|
command += `-g 240 `
|
||||||
|
|
||||||
command += `-row-mt 1 -pass 2 "${out}.webm" `
|
command += `-pass 2 "${out}.webm" `
|
||||||
//console.log(command)
|
return [command, duration]
|
||||||
return [command, duration, isTwoPass]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async #getDurationAndResolution(file) {
|
async #getDurationAndResolution(file) {
|
||||||
|
@ -144,8 +139,8 @@ class Encoder {
|
||||||
const seconds = arr[0] * 3600 + arr[1] * 60 + (+arr[2]) // converting to s
|
const seconds = arr[0] * 3600 + arr[1] * 60 + (+arr[2]) // converting to s
|
||||||
|
|
||||||
//resolution height
|
//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]
|
return [Number.parseFloat(seconds), resolutionHeight]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue