UNPKG

lw-ffmpeg-node

Version:

`lw-ffmpeg-node` is a lightweight Node.js library for manipulating video files using FFmpeg. It provides various methods to retrieve information about video files, compress videos, create video slices, crop videos, change video size and frame rate, and sa

183 lines (182 loc) 6.41 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileManager = exports.Print = void 0; const child_process_1 = require("child_process"); const path = __importStar(require("path")); const promises_1 = __importDefault(require("fs/promises")); const os_1 = __importDefault(require("os")); class LinuxError extends Error { constructor(command, extraData) { super(`Running the '${command}' command caused this error`); console.error(extraData); } } class Print { } exports.Print = Print; Print.colors = { RED: "\x1b[31m", GREEN: "\x1b[32m", YELLOW: "\x1b[33m", BLUE: "\x1b[34m", MAGENTA: "\x1b[35m", CYAN: "\x1b[36m", }; Print.RESET = "\x1b[0m"; Print.red = (...args) => console.log(Print.colors.RED, ...args, Print.RESET); Print.green = (...args) => console.log(Print.colors.GREEN, ...args, Print.RESET); Print.yellow = (...args) => console.log(Print.colors.YELLOW, ...args, Print.RESET); Print.blue = (...args) => console.log(Print.colors.BLUE, ...args, Print.RESET); Print.magenta = (...args) => console.log(Print.colors.MAGENTA, ...args, Print.RESET); Print.cyan = (...args) => console.log(Print.colors.CYAN, ...args, Print.RESET); class FileManager { static async exists(filePath) { try { await promises_1.default.access(filePath); return true; // The file exists } catch (error) { return false; // The file does not exist } } static async createDirectory(directoryPath, options) { if (await this.exists(directoryPath)) { if (options?.overwrite) { await promises_1.default.rm(directoryPath, { recursive: true, force: true }); await promises_1.default.mkdir(directoryPath, { recursive: true }); } } else { await promises_1.default.mkdir(directoryPath, { recursive: true }); } } } exports.FileManager = FileManager; class CLI { static isLinux() { const platform = os_1.default.platform(); return platform === "linux"; } static isWindows() { const platform = os_1.default.platform(); return platform === "win32"; } static getAbsolutePath(filePath) { return path.join(__dirname, path.normalize(filePath)); } /** * * Synchronous linux command execution. Returns the stdout */ static linux_sync(command, args = []) { try { const { status, stdout, stderr } = (0, child_process_1.spawnSync)(command, args, { encoding: "utf8", }); if (stderr) { throw new LinuxError(command, stderr); } return stdout; } catch (e) { console.error(e); throw new LinuxError(command); } } /** * Asynchronous command execution for executable files * * @param filepath the path to the executable * @param command any commands to pass to the executable * @param options cli options * @returns stdout or stderr */ static cmd(filepath, command, options) { const args = command .match(/(?:[^\s"]+|"[^"]*")+/g) ?.map((arg) => arg.replace(/"/g, "")); if (!args) { throw new Error("Invalid command"); } return new Promise((resolve, reject) => { (0, child_process_1.execFile)(filepath, args, { maxBuffer: 500 * 1000000, ...options, }, (error, stdout, stderr) => { if (error) { Print.yellow(`Error executing ${path.basename(filepath)}:`, error); reject(stderr); } else { resolve(stdout); } }); }); } /** * Asynchronous command execution for bash shell * * @param command the command to run * @param options cli options * @returns stdout or stderr */ static linux(command, options) { try { // send back stderr and stdout return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, { shell: true, ...options, }); let stdout = ""; let stderr = ""; child.stdout?.on("data", (data) => { options?.quiet === false && console.log(data.toString()); stdout += data.toString(); }); child.stderr?.on("data", (data) => { options?.quiet === false && console.log(data.toString()); stderr += data.toString(); }); child.on("close", (code) => { if (code !== 0) { reject(new LinuxError(command, stderr)); } else { resolve(stdout); } }); }); } catch (e) { throw new LinuxError(command); } } } exports.default = CLI;