From a74bb33967a9154af100b6a5fdcde403b4213e70 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 18 Sep 2024 01:10:28 +0200 Subject: [PATCH] compute audio tracks final bitrate --- src/main.ts | 20 +++++++++++--------- src/preload.ts | 4 ++-- src/scripts/renderer.ts | 6 +++--- src/utils/misc.ts | 8 +++++--- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index fb381d6..814ed76 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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())); diff --git a/src/preload.ts b/src/preload.ts index e14b9a6..bda47ba 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -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), diff --git a/src/scripts/renderer.ts b/src/scripts/renderer.ts index 05f8a9a..eb44b56 100644 --- a/src/scripts/renderer.ts +++ b/src/scripts/renderer.ts @@ -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; moveMetadata: (file: string) => Promise; confirmation: (text: string) => Promise; @@ -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); diff --git a/src/utils/misc.ts b/src/utils/misc.ts index c372635..a3b5a6d 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -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 */