UNPKG

ytdownload

Version:

Download YouTube Videos Easily (Now with CLI support!)

87 lines (86 loc) 3.92 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downlaod = void 0; var fs_1 = require("fs"); var readline_1 = require("readline"); var table_1 = require("table"); var ytdl_core_1 = __importStar(require("ytdl-core")); var downlaod = function (url, logging) { if (logging === void 0) { logging = true; } return new Promise(function (resolve, reject) { (0, fs_1.mkdir)("videos", function (err) { if (err && err.code !== "EEXIST") reject("[YTDownload] ".concat(err, " \n\nFOR MORE ADVANCED HELP: https://discord.com/invite/BjEJFwh")); (0, ytdl_core_1.getInfo)(url) .then(function (info) { var video = (0, ytdl_core_1.default)(info.videoDetails.video_url); var filePath = "./videos/".concat(info.videoDetails.title.replace(/[\W_]+/g, " "), ".mp4"); video.pipe((0, fs_1.createWriteStream)(filePath)); if (logging) { var data = (0, table_1.table)([ ["YTDownload", "Status", "Percent"], [ info.videoDetails.title, "Starting Download", "0%", ], ]); console.info(data); } video.on("end", function () { if (logging) { var data = (0, table_1.table)([ ["YTDownload", "Status", "Percent"], [ info.videoDetails.title, "Download Finished", "100%", ], ]); console.log("\n".concat(data)); } resolve(filePath); }); video.on("error", function (error) { return reject("[YTDownload] ".concat(error, " \n\nFOR MORE ADVANCED HELP: https://discord.com/invite/BjEJFwh")); }); video.on("progress", function (chunk, downloaded, total) { if (logging) { var percent = ((downloaded * 100) / total).toFixed(3); (0, readline_1.clearLine)(process.stdout, 0); (0, readline_1.cursorTo)(process.stdout, 0); process.stdout.write("[YTDownload] Downloading ".concat(percent, "%")); } }); }) .catch(function (err) { return reject("[YTDownload] ".concat(err, " \n\nFOR MORE ADVANCED HELP: https://discord.com/invite/BjEJFwh")); }); }); }); }; exports.downlaod = downlaod;