n8n-nodes-piapi
Version:
Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows
241 lines • 10 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MMAudioVideoToAudio = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("../shared/GenericFunctions");
class MMAudioVideoToAudio {
constructor() {
this.description = {
displayName: 'PiAPI MMAudio',
name: 'mmaudioVideoToAudio',
icon: 'file:../piapi.svg',
group: ['transform'],
version: 1,
description: 'Generate audio for videos using PiAPI MMAudio',
defaults: {
name: 'MMAudio Video to Audio',
},
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 (MP4 format only)',
},
{
displayName: 'Video Requirements',
name: 'videoRequirements',
type: 'notice',
default: 'Video must be in MP4 format. Maximum length is 30 seconds.',
},
{
displayName: 'Prompt',
name: 'prompt',
type: 'string',
typeOptions: {
rows: 4,
},
default: '',
description: 'Prompt text for audio generation',
},
{
displayName: 'Negative Prompt',
name: 'negativePrompt',
type: 'string',
typeOptions: {
rows: 4,
},
default: '',
description: 'Negative prompt text for audio generation',
},
{
displayName: 'Steps',
name: 'steps',
type: 'number',
typeOptions: {
minValue: 20,
maxValue: 50,
},
default: 20,
description: 'Number of steps for audio generation (20-50)',
},
{
displayName: 'Seed',
name: 'seed',
type: 'number',
default: 0,
description: 'Seed for audio generation (0 for random)',
},
{
displayName: 'Wait For Completion',
name: 'waitForCompletion',
type: 'boolean',
default: false,
description: 'Whether to wait for the audio generation process to complete before continuing',
},
{
displayName: 'Max Retries',
name: 'maxRetries',
type: 'number',
default: 20,
description: 'Maximum number of retries to check task status',
displayOptions: {
show: {
waitForCompletion: [true],
},
},
},
{
displayName: 'Retry Interval',
name: 'retryInterval',
type: 'number',
default: 3000,
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 prompt = this.getNodeParameter('prompt', i);
const negativePrompt = this.getNodeParameter('negativePrompt', i, '');
const steps = this.getNodeParameter('steps', i, 20);
const seed = this.getNodeParameter('seed', i, 0);
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/mmaudio',
task_type: 'video2audio',
input: {
prompt,
video: videoData,
},
config: {},
};
if (negativePrompt) {
requestBody.input.negative_prompt = negativePrompt;
}
if (steps) {
requestBody.input.steps = steps;
}
if (seed) {
requestBody.input.seed = seed;
}
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, 20);
const retryInterval = this.getNodeParameter('retryInterval', i, 3000);
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 is accessible, in MP4 format, and no longer than 30 seconds.';
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.MMAudioVideoToAudio = MMAudioVideoToAudio;
//# sourceMappingURL=MMAudioVideoToAudio.node.js.map