const { clipboard, shell, ipcRenderer } = require("electron");
const { default: YTDlpWrap } = require("yt-dlp-wrap-plus");
const path = require("path");
const os = require("os");
const fs = require("fs");
const { execSync, exec, spawnSync } = require("child_process");
let url;
const ytDlp = localStorage.getItem("ytdlp");
const ytdlp = new YTDlpWrap(ytDlp);
const downloadDir = localStorage.getItem("downloadPath");
const i18n = new (require("../translations/i18n"))();
let cookieArg = "";
let browser = "";
const formats = {
144: 160,
240: 133,
360: 134,
480: 135,
720: 136,
1080: 137,
1440: 400,
2160: 401,
4320: 571,
};
let originalCount = 0;
let ffmpeg;
let ffmpegPath;
if (os.platform() === "win32") {
ffmpeg = `"${__dirname}\\..\\ffmpeg.exe"`;
ffmpegPath = `${__dirname}\\..\\ffmpeg.exe`;
} else {
ffmpeg = `"${__dirname}/../ffmpeg"`;
ffmpegPath = `${__dirname}/../ffmpeg`;
}
if (!fs.existsSync(ffmpegPath)) {
try {
ffmpeg = execSync("which ffmpeg", { encoding: "utf8" });
ffmpeg = `"${ffmpeg.trimEnd()}"`;
} catch (error) {
console.log(error);
}
}
console.log("ffmpeg:", ffmpeg);
let foldernameFormat = "%(playlist_title)s";
let filenameFormat = "%(playlist_index)s.%(title)s.%(ext)s";
let playlistIndex = 1;
let playlistEnd = "";
function getId(id) {
return document.getElementById(id);
}
function pasteLink() {
const clipboardText = clipboard.readText();
getId("loadingWrapper").style.display = "flex";
getId("incorrectMsg").textContent = "";
getId("errorBtn").style.display = "none";
getId("errorDetails").style.display = "none";
getId("errorDetails").textContent = "";
exec(
`yt-dlp --yes-playlist --no-warnings -J --flat-playlist "${clipboardText}"`,
(error, stdout, stderr) => {
if (error) {
getId("loadingWrapper").style.display = "none";
getId("incorrectMsg").textContent = i18n.__(
"Some error has occurred. Check your network and use correct URL"
);
getId("errorDetails").innerHTML = `
URL: ${clipboardText}
${error}
`;
getId("errorDetails").title = i18n.__("Click to copy");
getId("errorBtn").style.display = "inline-block";
} else {
const parsed = JSON.parse(stdout);
console.log(parsed);
let items = "";
// If correct playlist url is used
if (parsed.entries) {
parsed.entries.forEach((entry) => {
const randId = Math.random()
.toFixed(10)
.toString()
.slice(2);
if (entry.channel) {
items += `