compute audio tracks final bitrate

This commit is contained in:
Mylloon 2024-09-18 01:10:28 +02:00
parent 8d34199ae0
commit a74bb33967
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 21 additions and 17 deletions

View file

@ -89,9 +89,9 @@ app.whenReady().then(() => {
const tmpFile = getNewFilename(file, "TMP_");
let outFile;
const nbTracks = getNumberOfAudioTracks(file);
const audioTracks = getNumberOfAudioTracks(file);
switch (nbTracks) {
switch (audioTracks.length) {
case 2:
// Merge 2 audio
// See: https://trac.ffmpeg.org/wiki/AudioChannelManipulation#a2stereostereo
@ -137,7 +137,7 @@ app.whenReady().then(() => {
title: outFile,
size: stats.size / 1024 / 1024,
duration,
nbTracks,
audioTracks,
};
};
@ -145,9 +145,11 @@ app.whenReady().then(() => {
const reduceSize = async (
file: string,
bitrate: number,
nbTracks: number
audioTracks: number[]
) => {
const audioBitrate = 500; // keep some room
const audioBitrate = Math.ceil(
audioTracks.reduce((sum, current) => current + sum, 0)
);
let videoBitrate = bitrate - audioBitrate;
const finalFile = getNewFilename(file, "Compressed - ");
@ -156,7 +158,7 @@ app.whenReady().then(() => {
const nul = process.platform === "win32" ? "NUL" : "/dev/null";
// Mapping of tracks for FFMPEG, adding 1 for the video stream
const mappingTracks = Array(nbTracks + 1)
const mappingTracks = Array(audioTracks.length + 1)
.fill("-map 0:")
.map((str, index) => {
return str + index;
@ -173,7 +175,7 @@ app.whenReady().then(() => {
hwAcc = "-hwaccel cuda";
// Increase video bitrate
videoBitrate = Math.floor(videoBitrate * 1.7);
videoBitrate = Math.floor(videoBitrate);
}
// Compress the video
@ -229,8 +231,8 @@ app.whenReady().then(() => {
ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle(
"reduceSize",
(_, file: string, bitrate: number, nbTracks: number) =>
reduceSize(file, bitrate, nbTracks)
(_, file: string, bitrate: number, audioTracks: number[]) =>
reduceSize(file, bitrate, audioTracks)
);
ipcMain.handle("moveMetadata", (_, file: string) => moveMetadata(file));
ipcMain.handle("exit", () => (error ? {} : app.quit()));

View file

@ -13,8 +13,8 @@ contextBridge.exposeInMainWorld("internals", {
ipcRenderer.invoke("getFilename", filepath),
askFiles: () => ipcRenderer.invoke("askFiles"),
mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file),
reduceSize: (file: string, bitrate: number, nbTracks: number) =>
ipcRenderer.invoke("reduceSize", file, bitrate, nbTracks),
reduceSize: (file: string, bitrate: number, audioTracks: number[]) =>
ipcRenderer.invoke("reduceSize", file, bitrate, audioTracks),
moveMetadata: (file: string) => ipcRenderer.invoke("moveMetadata", file),
exit: () => ipcRenderer.invoke("exit"),
confirmation: (text: string) => ipcRenderer.invoke("confirmation", text),

View file

@ -11,12 +11,12 @@ let internals: {
title: string;
duration: number;
size: number;
nbTracks: number;
audioTracks: number[];
}>;
reduceSize: (
file: string,
bitrate: number,
nbTracks: number
audioTracks: number[]
) => Promise<string>;
moveMetadata: (file: string) => Promise<string>;
confirmation: (text: string) => Promise<void>;
@ -135,7 +135,7 @@ const main = async () => {
finalTitle = await internals.reduceSize(
newFile.title,
bitrate,
newFile.nbTracks
newFile.audioTracks
);
} else {
updateMessage(`\nPréparation pour le partage...`, true, Mode.Append);

View file

@ -22,10 +22,12 @@ export const getVideoDuration = (file: string) => {
};
/** Return the number of audio tracks */
export const getNumberOfAudioTracks = (file: string) => {
const command = `"${ffprobePath}" -v error -show_entries stream=index -select_streams a -of json "${file}"`;
export const getNumberOfAudioTracks = (file: string): number[] => {
const command = `"${ffprobePath}" -v error -show_entries stream=bit_rate -select_streams a -of json "${file}"`;
const result = child_process.execSync(command, { encoding: "utf8" });
return JSON.parse(result).streams.length;
return JSON.parse(result).streams.map(
(v: { bit_rate: string }) => Number(v.bit_rate) / 1000
);
};
/** Print an error to the console and open the dev tool panel */