add audio merger

This commit is contained in:
Mylloon 2023-07-30 01:40:06 +02:00
parent 56c33032d0
commit eddb4fdaac
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 27 additions and 6 deletions

View file

@ -1,6 +1,7 @@
import { FileFilter, BrowserWindow, app, dialog, ipcMain } from "electron"; import { FileFilter, BrowserWindow, app, dialog, ipcMain } from "electron";
import path = require("path"); import path = require("path");
import ffmpegPath = require("ffmpeg-static"); import ffmpegPath = require("ffmpeg-static");
import child_process = require("child_process");
/** Create a new window */ /** Create a new window */
const createWindow = () => { const createWindow = () => {
@ -31,13 +32,27 @@ const askFile = async () => {
).filePaths; ).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(() => { app.whenReady().then(() => {
/* Context bridge */ /* Context bridge */
ipcMain.handle("ffmpeg", () => ffmpegPath); ipcMain.handle("ffmpeg", () => ffmpegPath);
ipcMain.handle("argv", () => process.argv); ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter); ipcMain.handle("allowedExtensions", () => moviesFilter);
ipcMain.handle("askfile", () => askFile()); ipcMain.handle("askFile", () => askFile());
ipcMain.handle("exit", () => app.quit()); ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle("exit", async () => app.quit());
createWindow(); createWindow();

View file

@ -5,6 +5,7 @@ contextBridge.exposeInMainWorld("internals", {
ffmpeg: () => ipcRenderer.invoke("ffmpeg"), ffmpeg: () => ipcRenderer.invoke("ffmpeg"),
argv: () => ipcRenderer.invoke("argv"), argv: () => ipcRenderer.invoke("argv"),
allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"), allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"),
askFile: () => ipcRenderer.invoke("askfile"), askFile: () => ipcRenderer.invoke("askFile"),
mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file),
exit: () => ipcRenderer.invoke("exit"), exit: () => ipcRenderer.invoke("exit"),
}); });

View file

@ -7,11 +7,11 @@ let internals: {
}>; }>;
askFile: () => Promise<string[]>; askFile: () => Promise<string[]>;
exit: () => any; exit: () => any;
mergeAudio: (filename: string) => Promise<void>;
}; };
const get_file = async () => { const getFile = async () => {
const allowedExtensions = (await internals.allowedExtensions()).extensions; const allowedExtensions = (await internals.allowedExtensions()).extensions;
console.log(allowedExtensions);
const argv = await internals.argv(); const argv = await internals.argv();
if (argv.length === 2) { if (argv.length === 2) {
const file = argv.pop(); const file = argv.pop();
@ -27,4 +27,9 @@ const get_file = async () => {
return file.join(""); 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();