UNPKG

n8n-nodes-ffmpeg-features

Version:
102 lines 4.14 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.execute = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); const childProcess = __importStar(require("child_process")); async function execute(items) { const returnData = []; for (let i = 0; i < items.length; i++) { const item = items[i]; if (!item.binary) continue; const binaryKeys = Object.keys(item.binary); if (!binaryKeys.length) continue; const binaryKey = binaryKeys[0]; const binaryData = item.binary[binaryKey]; if (!(binaryData === null || binaryData === void 0 ? void 0 : binaryData.data)) continue; const extension = path.extname(binaryData.fileName || '') || '.mp4'; const tempFilePath = path.join(os.tmpdir(), `ffinfo_${Date.now()}_${Math.random().toString(36).slice(2)}${extension}`); fs.writeFileSync(tempFilePath, Buffer.from(binaryData.data, 'base64')); const cmd = `ffprobe -v quiet -print_format json -show_format -show_streams "${tempFilePath}"`; let ffprobeOutput; try { ffprobeOutput = childProcess.execSync(cmd, { encoding: 'utf-8' }); } catch (error) { if (fs.existsSync(tempFilePath)) fs.unlinkSync(tempFilePath); throw new Error(`FFprobe error: ${error}`); } fs.unlinkSync(tempFilePath); let info; try { info = JSON.parse(ffprobeOutput); } catch (parseError) { throw new Error(`Could not parse ffprobe JSON output: ${parseError}`); } const formatBlock = info.format || {}; const durationStr = formatBlock.duration || '0'; const duration = parseFloat(durationStr); const sizeStr = formatBlock.size || '0'; const size = parseInt(sizeStr, 10); const bitRateStr = formatBlock.bit_rate || '0'; const bitRate = parseInt(bitRateStr, 10); const formatName = formatBlock.format_name || 'unknown'; let streams = []; if (Array.isArray(info.streams)) { streams = info.streams.map((stream) => ({ index: stream.index, codec_type: stream.codec_type, codec_name: stream.codec_name, width: stream.width || null, height: stream.height || null, sample_rate: stream.sample_rate || null, channels: stream.channels || null, bit_rate: stream.bit_rate || null, duration: stream.duration || null, })); } returnData.push({ json: { fileName: binaryData.fileName, formatName, duration, size, bitRate, streams, }, pairedItem: { item: i }, }); } return returnData; } exports.execute = execute; //# sourceMappingURL=info.operation.js.map