UNPKG

n8n-nodes-piapi

Version:

Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows

194 lines 8.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VideoUpscale = void 0; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../shared/GenericFunctions"); class VideoUpscale { constructor() { this.description = { displayName: 'PiAPI Video Upscale', name: 'videoUpscale', icon: 'file:../piapi.svg', group: ['transform'], version: 1, description: 'Enhance video resolution using PiAPI Video Upscale', defaults: { name: 'Video Upscale', }, inputs: ["main"], outputs: ["main"], credentials: [ { name: 'piAPIApi', required: true, }, ], properties: [ { displayName: 'Video Input Method', name: 'videoInputMethod', type: 'options', options: [ { name: 'URL', value: 'url', }, { name: 'Binary Data', value: 'binaryData', }, ], default: 'url', description: 'Method to input the video data', }, { displayName: 'Video Binary Property', name: 'videoBinaryPropertyName', type: 'string', default: 'data', required: true, displayOptions: { show: { videoInputMethod: ['binaryData'], }, }, description: 'Name of the binary property containing the video data', }, { displayName: 'Video URL', name: 'videoUrl', type: 'string', default: '', required: true, displayOptions: { show: { videoInputMethod: ['url'], }, }, description: 'URL of the video to upscale (MP4 format only)', }, { displayName: 'Video Requirements', name: 'videoRequirements', type: 'notice', default: 'The video must meet these requirements:\n- Maximum resolution: 720p (1280×720)\n- Frame count: Between 10 and 240 frames\n- File size: Maximum 10MB\n- Format: MP4 only\n- Service currently only supports 2x upscaling (doubles both width and height)', }, { displayName: 'Pricing Information', name: 'pricingInfo', type: 'notice', default: 'Cost: $0.0003 per frame processed\nExample: A 60-frame video will cost $0.018', }, { displayName: 'Wait For Completion', name: 'waitForCompletion', type: 'boolean', default: false, description: 'Whether to wait for the video upscaling process to complete before continuing', }, { displayName: 'Max Retries', name: 'maxRetries', type: 'number', default: 30, description: 'Maximum number of retries to check task status', displayOptions: { show: { waitForCompletion: [true], }, }, }, { displayName: 'Retry Interval', name: 'retryInterval', type: 'number', default: 5000, description: 'Interval between retries in milliseconds', displayOptions: { show: { waitForCompletion: [true], }, }, }, ], }; } async execute() { var _a, _b; const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const videoInputMethod = this.getNodeParameter('videoInputMethod', i); const waitForCompletion = this.getNodeParameter('waitForCompletion', i, false); let videoData; if (videoInputMethod === 'url') { videoData = this.getNodeParameter('videoUrl', i); } else { const videoBinaryPropertyName = this.getNodeParameter('videoBinaryPropertyName', i); const videoBinaryData = this.helpers.assertBinaryData(i, videoBinaryPropertyName); if (videoBinaryData.mimeType && !videoBinaryData.mimeType.includes('video/mp4')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'The provided binary data is not an MP4 video', { itemIndex: i }); } const base64String = Buffer.from(await this.helpers.getBinaryDataBuffer(i, videoBinaryPropertyName)).toString('base64'); videoData = `data:${videoBinaryData.mimeType};base64,${base64String}`; } const requestBody = { model: 'Qubico/video-toolkit', task_type: 'upscale', input: { video: videoData, }, }; const response = await GenericFunctions_1.piApiRequest.call(this, 'POST', '/api/v1/task', requestBody); const taskId = (_a = response.data) === null || _a === void 0 ? void 0 : _a.task_id; if (!taskId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Failed to get a valid task ID from the API'); } let executionData; if (waitForCompletion) { const maxRetries = this.getNodeParameter('maxRetries', i, 30); const retryInterval = this.getNodeParameter('retryInterval', i, 5000); executionData = await GenericFunctions_1.waitForTaskCompletion.call(this, taskId, maxRetries, retryInterval); } else { executionData = { task_id: taskId, status: ((_b = response.data) === null || _b === void 0 ? void 0 : _b.status) || 'pending', }; } returnData.push({ json: executionData, }); } catch (error) { if (error.message && error.message.includes('failed to get valid video')) { const errorMessage = 'The API could not process the provided video. Please ensure the video meets these requirements: maximum 720p resolution, 10-240 frames, maximum 10MB file size, and MP4 format.'; if (this.continueOnFail()) { returnData.push({ json: { error: errorMessage, details: error.message, }, }); continue; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), errorMessage); } if (this.continueOnFail()) { returnData.push({ json: { error: error.message, }, }); continue; } throw error; } } return [returnData]; } } exports.VideoUpscale = VideoUpscale; //# sourceMappingURL=VideoUpscale.node.js.map