Calculate bitrate more accurately
This commit is contained in:
parent
574478db84
commit
b7dd6e752b
@ -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");
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user