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

View file

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

View file

@ -11,12 +11,12 @@ let internals: {
title: string; title: string;
duration: number; duration: number;
size: number; size: number;
nbTracks: number; audioTracks: number[];
}>; }>;
reduceSize: ( reduceSize: (
file: string, file: string,
bitrate: number, bitrate: number,
nbTracks: number audioTracks: number[]
) => Promise<string>; ) => Promise<string>;
moveMetadata: (file: string) => Promise<string>; moveMetadata: (file: string) => Promise<string>;
confirmation: (text: string) => Promise<void>; confirmation: (text: string) => Promise<void>;
@ -135,7 +135,7 @@ const main = async () => {
finalTitle = await internals.reduceSize( finalTitle = await internals.reduceSize(
newFile.title, newFile.title,
bitrate, bitrate,
newFile.nbTracks newFile.audioTracks
); );
} else { } else {
updateMessage(`\nPréparation pour le partage...`, true, Mode.Append); 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 */ /** Return the number of audio tracks */
export const getNumberOfAudioTracks = (file: string) => { export const getNumberOfAudioTracks = (file: string): number[] => {
const command = `"${ffprobePath}" -v error -show_entries stream=index -select_streams a -of json "${file}"`; const command = `"${ffprobePath}" -v error -show_entries stream=bit_rate -select_streams a -of json "${file}"`;
const result = child_process.execSync(command, { encoding: "utf8" }); 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 */ /** Print an error to the console and open the dev tool panel */