diff --git a/src/compressor.js b/src/compressor.js
index fdb31c2..f4bac45 100644
--- a/src/compressor.js
+++ b/src/compressor.js
@@ -5,111 +5,41 @@ const os = require("os");
const si = require("systeminformation");
const {existsSync} = require("fs");
-document.addEventListener("translations-loaded", () => {
- window.i18n.translatePage();
-});
-
-let menuIsOpen = false;
-
-getId("menuIcon").addEventListener("click", () => {
- if (menuIsOpen) {
- getId("menuIcon").style.transform = "rotate(0deg)";
- menuIsOpen = false;
- let count = 0;
- let opacity = 1;
- const fade = setInterval(() => {
- if (count >= 10) {
- getId("menu").style.display = "none";
- clearInterval(fade);
- } else {
- opacity -= 0.1;
- getId("menu").style.opacity = opacity.toFixed(3).toString();
- count++;
- }
- }, 50);
- } else {
- getId("menuIcon").style.transform = "rotate(90deg)";
- menuIsOpen = true;
-
- setTimeout(() => {
- getId("menu").style.display = "flex";
- getId("menu").style.opacity = "1";
- }, 150);
- }
-});
-
-const ffmpeg = `"${getFfmpegPath()}"`;
-
-console.log(ffmpeg);
-
-const vaapi_device = "/dev/dri/renderD128";
-
-// Checking GPU
-si.graphics().then((info) => {
- console.log({gpuInfo: info});
- const gpuDevices = info.controllers;
-
- gpuDevices.forEach((gpu) => {
- // NVIDIA
- const gpuName = gpu.vendor.toLowerCase();
- const gpuModel = gpu.model.toLowerCase();
-
- if (gpuName.includes("nvidia") || gpuModel.includes("nvidia")) {
- document.querySelectorAll(".nvidia_opt").forEach((opt) => {
- opt.style.display = "block";
- });
- } else if (
- gpuName.includes("advanced micro devices") ||
- gpuModel.includes("amd")
- ) {
- if (os.platform() == "win32") {
- document.querySelectorAll(".amf_opt").forEach((opt) => {
- opt.style.display = "block";
- });
- } else {
- document.querySelectorAll(".vaapi_opt").forEach((opt) => {
- opt.style.display = "block";
- });
- }
- } else if (gpuName.includes("intel")) {
- if (os.platform() == "win32") {
- document.querySelectorAll(".qsv_opt").forEach((opt) => {
- opt.style.display = "block";
- });
- } else if (os.platform() != "darwin") {
- document.querySelectorAll(".vaapi_opt").forEach((opt) => {
- opt.style.display = "block";
- });
- }
- } else {
- if (os.platform() == "darwin") {
- document
- .querySelectorAll(".videotoolbox_opt")
- .forEach((opt) => {
- opt.style.display = "block";
- });
- }
- }
- });
-});
+const VAAPI_DEVICE = "/dev/dri/renderD128";
+const FFMPEG_PATH = `"${getFfmpegPath()}"`;
+console.log(`FFmpeg Path: ${FFMPEG_PATH}`);
/** @type {File[]} */
let files = [];
let activeProcesses = new Set();
let currentItemId = "";
let isCancelled = false;
+let menuIsOpen = false;
-/**
- * @param {string} id
- */
-function getId(id) {
- return document.getElementById(id);
-}
+(function init() {
+ setupTheme();
+ detectHardware();
+})();
+
+document.addEventListener("translations-loaded", () => {
+ window.i18n.translatePage();
+});
+
+getId("menuIcon").addEventListener("click", toggleMenu);
+getId("preferenceWin").addEventListener("click", () =>
+ navigateTo("/preferences.html")
+);
+getId("playlistWin").addEventListener("click", () =>
+ navigateToWin("/playlist.html")
+);
+getId("aboutWin").addEventListener("click", () => navigateTo("/about.html"));
+getId("historyWin").addEventListener("click", () =>
+ navigateTo("/history.html")
+);
+getId("homeWin").addEventListener("click", () => navigateToWin("/index.html"));
-// File Handling
const dropZone = document.querySelector(".drop-zone");
const fileInput = getId("fileInput");
-const selectedFilesDiv = getId("selected-files");
dropZone.addEventListener("dragover", (e) => {
e.preventDefault();
@@ -124,7 +54,6 @@ dropZone.addEventListener("drop", (e) => {
e.preventDefault();
dropZone.classList.remove("dragover");
// @ts-ignore
- console.log(e.dataTransfer);
files = Array.from(e.dataTransfer.files);
updateSelectedFiles();
});
@@ -135,20 +64,38 @@ fileInput.addEventListener("change", (e) => {
updateSelectedFiles();
});
-getId("custom-folder-select").addEventListener("click", (e) => {
+getId("compress-btn").addEventListener("click", startCompression);
+getId("cancel-btn").addEventListener("click", cancelCompression);
+
+getId("custom-folder-select").addEventListener("click", () => {
ipcRenderer.send("get-directory", "");
});
-function updateSelectedFiles() {
- const fileList = files
- .map((f) => `${f.name} (${formatBytes(f.size)})
`)
- .join("\n");
- selectedFilesDiv.innerHTML = fileList || "No files selected";
-}
+getId("output-folder-input").addEventListener("change", (e) => {
+ const checked = e.target.checked;
+ const selectBtn = getId("custom-folder-select");
+ const pathDisplay = getId("custom-folder-path");
-// Compression Logic
-getId("compress-btn").addEventListener("click", startCompression);
-getId("cancel-btn").addEventListener("click", cancelCompression);
+ if (!checked) {
+ selectBtn.style.display = "block";
+ } else {
+ selectBtn.style.display = "none";
+ pathDisplay.textContent = "";
+ pathDisplay.style.display = "none";
+ }
+});
+
+getId("themeToggle").addEventListener("change", () => {
+ const theme = getId("themeToggle").value;
+ document.documentElement.setAttribute("theme", theme);
+ localStorage.setItem("theme", theme);
+});
+
+ipcRenderer.on("directory-path", (_event, msg) => {
+ let customFolderPathItem = getId("custom-folder-path");
+ customFolderPathItem.textContent = msg;
+ customFolderPathItem.style.display = "inline";
+});
async function startCompression() {
if (files.length === 0) return alert("Please select files first!");
@@ -156,8 +103,7 @@ async function startCompression() {
const settings = getEncoderSettings();
for (const file of files) {
- const itemId =
- "f" + Math.random().toFixed(10).toString().slice(2).toString();
+ const itemId = "f" + Math.random().toFixed(10).toString().slice(2);
currentItemId = itemId;
const outputPath = generateOutputPath(file, settings);
@@ -168,24 +114,10 @@ async function startCompression() {
if (isCancelled) {
isCancelled = false;
} else {
- updateProgress("success", "", itemId);
- const fileSavedElement = document.createElement("b");
- fileSavedElement.textContent = i18n.__("fileSavedClickToOpen");
- fileSavedElement.onclick = () => {
- ipcRenderer.send("show-file", outputPath);
- };
- getId(itemId + "_prog").appendChild(fileSavedElement);
- currentItemId = "";
+ handleCompressionSuccess(itemId, outputPath);
}
} catch (error) {
- const errorElement = document.createElement("div");
- errorElement.onclick = () => {
- ipcRenderer.send("error_dialog", error.message);
- };
- errorElement.textContent = i18n.__("errorClickForDetails");
- updateProgress("error", "", itemId);
- getId(itemId + "_prog").appendChild(errorElement);
- currentItemId = "";
+ handleCompressionError(itemId, error);
}
}
}
@@ -201,36 +133,12 @@ function cancelCompression() {
/**
* @param {File} file
- */
-function generateOutputPath(file, settings) {
- console.log({settings});
- const output_extension = settings.extension;
- const parsed_file = path.parse(file.path);
-
- let outputDir = settings.outputPath || parsed_file.dir;
-
- if (output_extension == "unchanged") {
- return path.join(
- outputDir,
- `${parsed_file.name}${settings.outputSuffix}${parsed_file.ext}`
- );
- }
-
- return path.join(
- outputDir,
- `${parsed_file.name}_compressed.${output_extension}`
- );
-}
-
-/**
- * @param {File} file
- * @param {{ encoder: any; speed: any; videoQuality: any; audioQuality?: any; audioFormat: string, extension: string }} settings
+ * @param {Object} settings
* @param {string} itemId
* @param {string} outputPath
*/
async function compressVideo(file, settings, itemId, outputPath) {
const command = buildFFmpegCommand(file, settings, outputPath);
-
console.log("Command: " + command);
return new Promise((resolve, reject) => {
@@ -240,14 +148,9 @@ async function compressVideo(file, settings, itemId, outputPath) {
});
activeProcesses.add(child);
- child.on("exit", (_code) => {
- activeProcesses.delete(child);
- });
+ child.on("exit", () => activeProcesses.delete(child));
- let video_info = {
- duration: "",
- bitrate: "",
- };
+ let video_info = {duration: "", bitrate: ""};
createProgressItem(
path.basename(file.path),
@@ -257,22 +160,11 @@ async function compressVideo(file, settings, itemId, outputPath) {
);
child.stderr.on("data", (data) => {
- // console.log(data)
const duration_match = data.match(/Duration:\s*([\d:.]+)/);
- if (duration_match) {
- video_info.duration = duration_match[1];
- }
-
- // const bitrate_match = data.match(/bitrate:\s*([\d:.]+)/);
- // if (bitrate_match) {
- // // Bitrate in kb/s
- // video_info.bitrate = bitrate_match[1];
- // }
+ if (duration_match) video_info.duration = duration_match[1];
const progressTime = data.match(/time=(\d+:\d+:\d+\.\d+)/);
-
const totalSeconds = timeToSeconds(video_info.duration);
-
const currentSeconds =
progressTime && progressTime.length > 1
? timeToSeconds(progressTime[1])
@@ -282,7 +174,6 @@ async function compressVideo(file, settings, itemId, outputPath) {
const progress = Math.round(
(currentSeconds / totalSeconds) * 100
);
-
getId(
itemId + "_prog"
).innerHTML = `