dsr/src/main.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-07-30 00:14:20 +02:00
import { FileFilter, BrowserWindow, app, dialog, ipcMain } from "electron";
import path = require("path");
2023-07-29 19:23:18 +02:00
import ffmpegPath = require("ffmpeg-static");
2023-07-30 01:40:06 +02:00
import child_process = require("child_process");
2023-07-28 02:46:49 +02:00
2023-07-30 00:14:20 +02:00
/** Create a new window */
2023-07-28 02:32:18 +02:00
const createWindow = () => {
const win = new BrowserWindow({
2023-07-29 19:23:18 +02:00
width: 1280,
height: 720,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
2023-07-28 02:32:18 +02:00
});
2023-07-29 15:30:41 +02:00
win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html"));
2023-07-29 19:23:18 +02:00
win.webContents.openDevTools(); // debug
2023-07-30 02:01:16 +02:00
return win;
2023-07-28 02:32:18 +02:00
};
2023-07-29 15:30:41 +02:00
2023-07-30 00:14:20 +02:00
const moviesFilter = {
name: "Videos",
extensions: ["mp4", "mkv"],
} as FileFilter;
2023-07-30 01:40:06 +02:00
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}"`
);
2023-07-30 02:01:16 +02:00
return outFile;
2023-07-30 01:40:06 +02:00
};
2023-07-28 02:32:18 +02:00
app.whenReady().then(() => {
2023-07-30 02:01:16 +02:00
const win = createWindow();
/** Ask user a file */
const askFile = async () => {
return dialog.showOpenDialogSync(win, {
filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"],
});
};
/** Send confirmation to user */
const confirmation = async (message: string) => {
dialog.showMessageBoxSync(win, { message });
};
2023-07-30 00:14:20 +02:00
/* Context bridge */
2023-07-29 20:20:55 +02:00
ipcMain.handle("argv", () => process.argv);
2023-07-30 00:14:20 +02:00
ipcMain.handle("allowedExtensions", () => moviesFilter);
2023-07-30 01:40:06 +02:00
ipcMain.handle("askFile", () => askFile());
ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle("exit", async () => app.quit());
2023-07-30 02:01:16 +02:00
ipcMain.handle("confirmation", async (_, text: string) => confirmation(text));
app.on("activate", () => {
2023-07-29 19:23:18 +02:00
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
2023-07-29 19:23:18 +02:00
if (process.platform !== "darwin") {
app.quit();
}
2023-07-28 02:32:18 +02:00
});