From eddb4fdaac5a6a3e75cba2e820c8c32e16bc16c3 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 30 Jul 2023 01:40:06 +0200 Subject: [PATCH] add audio merger --- src/main.ts | 19 +++++++++++++++++-- src/preload.ts | 3 ++- src/scripts/renderer.ts | 11 ++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index ca3bd4c..a3bf19d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { FileFilter, BrowserWindow, app, dialog, ipcMain } from "electron"; import path = require("path"); import ffmpegPath = require("ffmpeg-static"); +import child_process = require("child_process"); /** Create a new window */ const createWindow = () => { @@ -31,13 +32,27 @@ const askFile = async () => { ).filePaths; }; +const getNewFilename = (ogFile: string, part: string) => { + const oldFile = path.parse(ogFile); + return path.join(oldFile.dir, `${part}`.concat(oldFile.base)); +}; + +/** Merge all audios track of a video into one */ +const mergeAudio = (file: string) => { + const outFile = getNewFilename(file, "(merged audio) "); + child_process.exec( + `${ffmpegPath} -i "${file}" -filter_complex "[0:a]amerge=inputs=2[a]" -ac 1 -map 0:v -map "[a]" -c:v copy "${outFile}"` + ); +}; + app.whenReady().then(() => { /* Context bridge */ ipcMain.handle("ffmpeg", () => ffmpegPath); ipcMain.handle("argv", () => process.argv); ipcMain.handle("allowedExtensions", () => moviesFilter); - ipcMain.handle("askfile", () => askFile()); - ipcMain.handle("exit", () => app.quit()); + ipcMain.handle("askFile", () => askFile()); + ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file)); + ipcMain.handle("exit", async () => app.quit()); createWindow(); diff --git a/src/preload.ts b/src/preload.ts index 688d8dd..13357a9 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -5,6 +5,7 @@ contextBridge.exposeInMainWorld("internals", { ffmpeg: () => ipcRenderer.invoke("ffmpeg"), argv: () => ipcRenderer.invoke("argv"), allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"), - askFile: () => ipcRenderer.invoke("askfile"), + askFile: () => ipcRenderer.invoke("askFile"), + mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file), exit: () => ipcRenderer.invoke("exit"), }); diff --git a/src/scripts/renderer.ts b/src/scripts/renderer.ts index bbe3fdb..c07c2e3 100644 --- a/src/scripts/renderer.ts +++ b/src/scripts/renderer.ts @@ -7,11 +7,11 @@ let internals: { }>; askFile: () => Promise; exit: () => any; + mergeAudio: (filename: string) => Promise; }; -const get_file = async () => { +const getFile = async () => { const allowedExtensions = (await internals.allowedExtensions()).extensions; - console.log(allowedExtensions); const argv = await internals.argv(); if (argv.length === 2) { const file = argv.pop(); @@ -27,4 +27,9 @@ const get_file = async () => { return file.join(""); }; -get_file().then((file) => (document.getElementById("info").innerText = file)); +const main = async () => { + const file = await getFile(); + document.getElementById("info").innerText = file.concat(); + await internals.mergeAudio(file); +}; +main();