diff --git a/.gitignore b/.gitignore index d9f1762..db0844c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules +dist + .env .env.local \ No newline at end of file diff --git a/dist/app.js b/dist/app.js deleted file mode 100644 index c137211..0000000 --- a/dist/app.js +++ /dev/null @@ -1,40 +0,0 @@ -"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const express_1 = __importDefault(require("express")); -const cors_1 = __importDefault(require("cors")); -const morgan_1 = __importDefault(require("morgan")); -const video_routes_1 = __importDefault(require("./routes/video.routes")); -const app = (0, express_1.default)(); -const allowedOrigins = [ - "http://localhost:3000", - "https://yt-downloader-jet.vercel.app/", -]; -// settings -app.set("port", process.env.PORT || 8000); -// middlewares -app.use((0, cors_1.default)({ - origin: (origin, callback) => { - if (!origin) - return callback(null, true); - if (allowedOrigins.indexOf(origin) === -1) { - var msg = "The CORS policy for this site does not " + - "allow access from the specified Origin."; - return callback(new Error(msg), false); - } - return callback(null, true); - }, -})); -app.use((0, morgan_1.default)("dev")); -app.use(express_1.default.json()); -// routes -app.use("/api/video", video_routes_1.default); -app.get("/", (req, res) => { - res.status(200).json({ message: "Welcome to the API" }); -}); -// start -app.listen(app.get("port"), () => { - console.log("Server on port", app.get("port")); -}); diff --git a/dist/controllers/video.controllers.js b/dist/controllers/video.controllers.js deleted file mode 100644 index 8171c56..0000000 --- a/dist/controllers/video.controllers.js +++ /dev/null @@ -1,89 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.config = exports.download = void 0; -const ytdl_core_1 = __importDefault(require("ytdl-core")); -const downloadVideoWithAudio_1 = __importDefault(require("../utils/downloadVideoWithAudio")); -const child_process_1 = __importDefault(require("child_process")); -const ffmpeg_static_1 = __importDefault(require("ffmpeg-static")); -const download = (req, res) => __awaiter(void 0, void 0, void 0, function* () { - var _a; - const { url, quality, trim } = req.body; - // console.log("trim", trim); - res.setHeader("Content-Disposition", `attachment; filename="video.mp4"`); - res.setHeader("Content-Type", "video/mp4"); - // 18 = 360p this always come with audio so we don't need to download audio - if (quality === 18) { - const info = yield ytdl_core_1.default.getInfo(url); - // get 360p video - const format = ytdl_core_1.default.chooseFormat(info.formats, { quality: quality }); - const video = (0, ytdl_core_1.default)(url, { format }); - // if trim is set then trim the video - if (trim) { - const ffmpegProcess = child_process_1.default.spawn(ffmpeg_static_1.default || "d:\\ffmpeg\\bin\\ffmpeg.exe", [ - // Remove ffmpeg's console spamming - "-loglevel", - "0", - "-hide_banner", - // inputs - "-i", - "pipe:3", - // trim video - "-ss", - trim.start, - "-to", - trim.end, - // Choose some fancy codes - "-c:v", - "copy", - "-c:a", - "copy", - // Define output container - "-f", - "matroska", - "pipe:4", - ], { - windowsHide: true, - stdio: [ - /* Standard: stdin, stdout, stderr */ - "inherit", - "inherit", - "inherit", - /* Custom: pipe:3, pipe:4 */ - "pipe", - "pipe", - ], - }); - ffmpegProcess.on("close", () => { - process.stdout.write("\n"); - }); - video.pipe(ffmpegProcess.stdio[3]); - (_a = ffmpegProcess.stdio[4]) === null || _a === void 0 ? void 0 : _a.pipe(res); - } - else { - // if trim is not set then just download the video - video.pipe(res); - } - } - else { - // download video with audio - (0, downloadVideoWithAudio_1.default)({ url, quality, trim }, res); - } -}); -exports.download = download; -exports.config = { - api: { - responseLimit: false, - }, -}; diff --git a/dist/routes/video.routes.js b/dist/routes/video.routes.js deleted file mode 100644 index 5392582..0000000 --- a/dist/routes/video.routes.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const express_1 = require("express"); -const router = (0, express_1.Router)(); -const video_controllers_1 = require("../controllers/video.controllers"); -router.post("/download", video_controllers_1.download); -exports.default = router; diff --git a/dist/utils/downloadVideoWithAudio.js b/dist/utils/downloadVideoWithAudio.js deleted file mode 100644 index da1131a..0000000 --- a/dist/utils/downloadVideoWithAudio.js +++ /dev/null @@ -1,79 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const ytdl_core_1 = __importDefault(require("ytdl-core")); -const child_process_1 = __importDefault(require("child_process")); -const ffmpeg_static_1 = __importDefault(require("ffmpeg-static")); -function downloadVideoWithAudio(data, res) { - var _a; - return __awaiter(this, void 0, void 0, function* () { - const info = yield ytdl_core_1.default.getInfo(data.url); - const videoFormat = ytdl_core_1.default.chooseFormat(info.formats, { - quality: data.quality, - filter: "videoonly", - }); - // get highest quality audio - const audioFormat = ytdl_core_1.default.chooseFormat(info.formats, { - quality: "highestaudio", - filter: "audioonly", - }); - // download video and audio and merge them into one file using ffmpeg - const video = (0, ytdl_core_1.default)(data.url, { format: videoFormat }); - const audio = (0, ytdl_core_1.default)(data.url, { format: audioFormat }); - const ffmpegProcess = child_process_1.default.spawn(ffmpeg_static_1.default || "d:\\ffmpeg\\bin\\ffmpeg.exe", [ - // Remove ffmpeg's console spamming - "-loglevel", - "0", - "-hide_banner", - // inputs - "-i", - "pipe:3", - "-i", - "pipe:4", - // if trim is set then trim the video - ...(data.trim ? ["-ss", data.trim.start, "-to", data.trim.end] : []), - // Choose some fancy codes - "-c:v", - "copy", - "-c:a", - "aac", - // Define output container - "-f", - "matroska", - "pipe:5", - ], { - windowsHide: true, - stdio: [ - /* Standard: stdin, stdout, stderr */ - "inherit", - "inherit", - "pipe", - "pipe", - "pipe", - "pipe", - ], - }); - ffmpegProcess.on("close", () => { - process.stdout.write("\n"); - }); - // return video and audio to client - res.status(200); - audio.pipe(ffmpegProcess.stdio[3]); - video.pipe(ffmpegProcess.stdio[4]); - // ignore error of index - // @ts-ignore - (_a = ffmpegProcess.stdio[5]) === null || _a === void 0 ? void 0 : _a.pipe(res); - }); -} -exports.default = downloadVideoWithAudio;