const fs = require("fs"); const cp = require("child_process"); const os = require("os"); const ffmpeg = require("ffmpeg-static"); const path = require("path"); const { shell, ipcRenderer, clipboard } = require("electron"); const { default: YTDlpWrap } = require("yt-dlp-wrap"); // Directories const homedir = os.homedir(); const appdir = path.join(homedir, "ytDownloader"); const tempDir = path.join(homedir, ".ytDownloader", "temp"); fs.mkdirSync(tempDir, { recursive: true }); // Download directory let downloadDir = ""; // Global variables let title, onlyvideo, id, thumbnail, ytdlp, duration; let rangeCmd = ""; let rangeOption = "--download-sections"; let willBeSaved = true; function getId(id) { return document.getElementById(id); } function downloadPathSelection() { let localPath = localStorage.getItem("downloadPath"); if (localPath) { downloadDir = localPath; } else { downloadDir = appdir; localStorage.setItem("downloadPath", appdir); } fs.mkdir(downloadDir, { recursive: true }, () => {}); } downloadPathSelection(); // Checking for yt-dlp let ytDlp; let ytdlpPath = path.join(os.homedir(), ".ytDownloader", "ytdlp"); // Downloading yt-dlp async function downloadYtdlp() { document.querySelector("#popupBox p").textContent = "Downloading yt-dlp"; getId("popupSvg").style.display = "inline"; await YTDlpWrap.downloadFromGithub(ytdlpPath); getId("popupBox").style.display = "none"; ytDlp = ytdlpPath; ytdlp = new YTDlpWrap(ytDlp); console.log("yt-dlp bin Path: " + ytDlp); } // Checking is yt-dlp has been installed by user cp.exec("yt-dlp --version", (error, stdout, stderr) => { if (error) { // Checking if yt-dlp has been installed by program cp.exec(`${ytdlpPath} --version`, (error, stdout, stderr) => { if (error) { getId("popupBox").style.display = "block"; process.on("uncaughtException", (reason, promise) => { document.querySelector("#popupBox p").textContent = "Failed to download yt-dlp. Please check your connection and try again"; getId("popupSvg").style.display = "none"; getId( "popup" ).innerHTML += ``; console.log("Failed to download yt-dlp"); getId("tryBtn").addEventListener("click", () => { getId("popup").removeChild(getId("popup").lastChild); downloadYtdlp(); }); }); downloadYtdlp(); } else { console.log("yt-dlp binary is present in PATH"); ytDlp = ytdlpPath; ytdlp = new YTDlpWrap(ytDlp); cp.spawn(`${ytDlp}`, ["-U"]) .stdout.on("data", (data) => console.log(data.toString("utf8")) ) .stderr.on("data", (data) => { console.log(data.toString("utf8")); }); console.log("yt-dlp bin Path: " + ytDlp); } }); } else { console.log("yt-dlp binary is present in PATH"); ytDlp = "yt-dlp"; ytdlp = new YTDlpWrap(ytDlp); console.log("yt-dlp bin Path: " + ytDlp); } }); function defaultVideoToggle() { videoToggle.style.backgroundColor = "var(--box-toggleOn)"; audioToggle.style.backgroundColor = "var(--box-toggle)"; getId("audioList").style.display = "none"; getId("videoList").style.display = "block"; } //! Pasting url from clipboard function pasteUrl() { defaultVideoToggle(); getId("hidden").style.display = "none"; getId("loadingWrapper").style.display = "flex"; getId("incorrectMsg").textContent = ""; const url = clipboard.readText(); getInfo(url); } getId("closeHidden").addEventListener("click", () => { getId("hidden").style.display = "none"; getId("loadingWrapper").style.display = "none"; }); document.addEventListener("keydown", (event) => { if (event.ctrlKey && event.key == "v") { pasteUrl(); } }); getId("pasteUrl").addEventListener("click", () => { pasteUrl(); }); // Getting video info async function getInfo(url) { downloadPathSelection(); getId("videoFormatSelect").innerHTML = ""; getId("audioFormatSelect").innerHTML = ""; let info; cp.exec(`${ytDlp} -j ${url}`, (error, stdout, stderr) => { try { info = JSON.parse(stdout); } catch (error) { info = false; } console.log(info); if (info) { title = info.title; id = info.id; thumbnail = info.thumbnail; duration = info.duration; const formats = info.formats; console.log(formats); const urlElements = document.querySelectorAll(".url"); urlElements.forEach((element) => { element.value = url; }); getId("hidden").style.display = "inline-block"; getId("title").innerHTML = "Title: " + title; getId("videoList").style.display = "block"; videoToggle.style.backgroundColor = "rgb(67, 212, 164)"; let audioSize = 0; // Getting approx size of audio file for (let format of formats) { if ( format.audio_ext !== "none" || (format.acodec !== "none" && format.video_ext === "none") ) { onlyvideo = true; audioSize = Number(format.filesize || format.filesize_approx) / 1000000; } } for (let format of formats) { let size; if (format.filesize || format.filesize_approx) { size = ( Number(format.filesize || format.filesize_approx) / 1000000 ).toFixed(2); } else { size = "Unknown size"; } // For videos if ( format.video_ext !== "none" && format.audio_ext === "none" ) { if (size !== "Unknown size") { size = (Number(size) + 0 || Number(audioSize)).toFixed( 2 ); size = size + " MB"; } const format_id = format.format_id + "|" + format.ext; const element = ""; getId("videoFormatSelect").innerHTML += element; } // For audios else if ( format.audio_ext !== "none" || (format.acodec !== "none" && format.video_ext === "none") ) { size = size + " MB"; let audio_ext; if (format.audio_ext === "webm") { audio_ext = "opus"; } else { audio_ext = format.audio_ext; } const format_id = format.format_id + "|" + audio_ext; const element = ""; getId("audioFormatSelect").innerHTML += element; } // Both audio and video available else if ( format.audio_ext !== "none" || (format.acodec !== "none" && format.video_ext !== "none") ) { let size; if (format.filesize || format.filesize_approx) { size = ( Number(format.filesize || format.filesize_approx) / 1000000 ).toFixed(2); } else { size = "Unknown size"; } const element = ""; getId("videoFormatSelect").innerHTML += element; } } } else { getId("loadingWrapper").style.display = "none"; getId("incorrectMsg").textContent = "Some error has occured. Check your connection and use correct URL"; } }); } // Video download event getId("videoDownload").addEventListener("click", (event) => { getId("hidden").style.display = "none"; clickAnimation("videoDownload"); download("video"); }); // Audio download event getId("audioDownload").addEventListener("click", (event) => { getId("hidden").style.display = "none"; clickAnimation("audioDownload"); download("audio"); }); // function restorePrevious() { // if (!localStorage.getItem("itemList")) return; // const items = JSON.parse(localStorage.getItem("itemList")); // if (items) { // console.log(items); // items.forEach((item) => { // const newItem = ` //
//