From b7dd6e752be1688ab7f91901ccfd5b55d9af93cd Mon Sep 17 00:00:00 2001 From: Reimar Date: Mon, 5 Jan 2026 11:07:14 +0100 Subject: [PATCH] Calculate bitrate more accurately --- public/assets/scripts/main.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/public/assets/scripts/main.js b/public/assets/scripts/main.js index 9a88228..c3f0e00 100644 --- a/public/assets/scripts/main.js +++ b/public/assets/scripts/main.js @@ -11,8 +11,8 @@ async function compress() { const videoLength = document.getElementById("uploaded-video").duration; let targetFilesize; // Stored in kBit switch (filesizeUnit) { - case "K": targetFilesize = filesize * 8.192; break; - case "M": targetFilesize = filesize * 8388.608; break; + case "K": targetFilesize = filesize * 8; break; + case "M": targetFilesize = filesize * 8000; break; } updateProgress(0); @@ -34,19 +34,19 @@ async function compress() { // Use Two-Pass to create a video file with desired file size // https://trac.ffmpeg.org/wiki/Encode/H.264#twopass - const bitrate = Math.round(targetFilesize / videoLength) - 128; // Subtract audio bitrate + const bitrate = Math.floor(targetFilesize / videoLength) - 128; // Subtract audio bitrate - console.debug("Target bitrate:", bitrate); + console.debug("Target bitrate:", bitrate, "Video length:", videoLength); - const options = ["-i", file.name, "-preset", "veryfast", "-c:v", "libx264", "-b:v", bitrate + "k"]; + const options = ["-i", file.name, "-preset", "ultrafast", "-c:v", "libx264", "-b:v", bitrate + "k"]; pass = 1; - await ffmpeg.exec(["-y", ...options, "-pass", "1", "-vsync", "cfr", "-f", "null", "/dev/null"]); + await ffmpeg.exec([...options, "-pass", "1", "-vsync", "cfr", "-f", "null", "/dev/null"]); pass = 2; - await ffmpeg.exec([...options, "-pass", "2", "-c:a", "aac", "-b:a", "128k", "compressed.mp4"]); + await ffmpeg.exec([...options, "-pass", "2", "-c:a", "copy", "-b:a", "128k", "compressed.mp4"]); const video = await ffmpeg.readFile("compressed.mp4");