Audio encoding fixes

This commit is contained in:
Djkato 2022-07-14 21:22:00 +02:00
parent a5b65b067e
commit b97992294f

View file

@ -4,6 +4,7 @@ const termkit = require('terminal-kit')
class Encoder { class Encoder {
settings settings
encoder encoder
#maxOpusBitrate = 500 //kbits
constructor(settings, currentSetting) { constructor(settings, currentSetting) {
this.settings = settings this.settings = settings
} }
@ -28,8 +29,12 @@ class Encoder {
*/ */
async encodeAudio(path, out) { async encodeAudio(path, out) {
let [duration, resolution] = await this.#getDurationAndResolution(path) let [duration, resolution] = await this.#getDurationAndResolution(path)
const audioBitRate = Math.round(62000 / duration) let audioBitRate = Math.round(this.settings.size_limit / duration)
if (audioBitRate > this.#maxOpusBitrate) {
audioBitRate = this.#maxOpusBitrate
}
this.encoder = exec(`ffmpeg -y -i "${path}" -c:a libvorbis -b:a ${audioBitRate}k "${out}.ogg"`) this.encoder = exec(`ffmpeg -y -i "${path}" -c:a libvorbis -b:a ${audioBitRate}k "${out}.ogg"`)
console.log(`ffmpeg -y -i "${path}" -c:a libvorbis -b:a ${audioBitRate}k "${out}.ogg"`)
return [duration, out, undefined] return [duration, out, undefined]
} }
@ -47,14 +52,13 @@ class Encoder {
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.95 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((this.settings.size_limit / 8 * 1 / duration) * 0.95) 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) 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 this.#maxOpusBitrate reached, cap the audio bit rate and give the rest of the bits to video
if (audioBitRate > maxOpusBitrate) { if (audioBitRate > this.#maxOpusBitrate) {
videoBitRate += audioBitRate - maxOpusBitrate videoBitRate += audioBitRate - this.#maxOpusBitrate
audioBitRate = maxOpusBitrate audioBitRate = this.#maxOpusBitrate
} }
let command = "" let command = ""