n8n-nodes-piapi
Version:
Community n8n nodes for PiAPI - integrate generative AI capabilities (image, video, audio, 3D) into your workflows
227 lines • 9.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.HunyuanImageToVideo = void 0;
const GenericFunctions_1 = require("../shared/GenericFunctions");
class HunyuanImageToVideo {
constructor() {
this.description = {
displayName: 'PiAPI Hunyuan Image to Video',
name: 'hunyuanImageToVideo',
icon: 'file:../piapi.svg',
group: ['transform'],
version: 1,
description: 'Generate videos from images using PiAPI Hunyuan',
defaults: {
name: 'Hunyuan Image to Video',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: 'piAPIApi',
required: true,
},
],
properties: [
{
displayName: 'Task Type',
name: 'taskType',
type: 'options',
options: [
{
name: 'Image to Video Concat',
value: 'img2video-concat',
description: 'Generate videos based on images for better movement (20 steps, 85 FPS)',
},
{
name: 'Image to Video Replace',
value: 'img2video-replace',
description: 'Generate videos following the guiding image better (20 steps, 85 FPS)',
},
],
default: 'img2video-concat',
description: 'Type of image-to-video transformation',
},
{
displayName: 'Prompt',
name: 'prompt',
type: 'string',
typeOptions: {
rows: 4,
},
default: '',
required: true,
description: 'Text prompt to guide the video generation',
},
{
displayName: 'Image Source',
name: 'imageSource',
type: 'options',
options: [
{
name: 'URL',
value: 'url',
description: 'Load image from URL',
},
{
name: 'Binary Data',
value: 'binaryData',
description: 'Use image from binary field',
},
],
default: 'url',
description: 'Where to get the image from',
},
{
displayName: 'Image URL',
name: 'imageUrl',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
imageSource: ['url'],
},
},
description: 'URL of the image to transform into a video',
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
imageSource: ['binaryData'],
},
},
description: 'Name of the binary property containing the image',
},
{
displayName: 'Aspect Ratio',
name: 'aspectRatio',
type: 'options',
options: [
{
name: '16:9',
value: '16:9',
description: 'Landscape (544x960)',
},
{
name: '9:16',
value: '9:16',
description: 'Portrait (960x544)',
},
{
name: '1:1',
value: '1:1',
description: 'Square (720x720)',
},
],
default: '16:9',
description: 'Aspect ratio of the generated video',
},
{
displayName: 'Wait for Completion',
name: 'waitForCompletion',
type: 'boolean',
default: false,
description: 'Whether to wait for the task to complete before returning',
},
],
};
}
async execute() {
var _a;
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
const taskType = this.getNodeParameter('taskType', i);
const prompt = this.getNodeParameter('prompt', i);
const imageSource = this.getNodeParameter('imageSource', i);
const aspectRatio = this.getNodeParameter('aspectRatio', i);
const waitForCompletion = this.getNodeParameter('waitForCompletion', i, true);
let imageBase64 = '';
if (imageSource === 'url') {
const imageUrl = this.getNodeParameter('imageUrl', i);
try {
new URL(imageUrl);
try {
const imageResponse = await this.helpers.request({
method: 'GET',
url: imageUrl,
encoding: null,
resolveWithFullResponse: true,
});
const buffer = Buffer.from(imageResponse.body);
const contentType = imageResponse.headers['content-type'] || 'image/png';
imageBase64 = `data:${contentType};base64,${buffer.toString('base64')}`;
}
catch (error) {
throw new Error(`Failed to download image from URL: ${error.message}`);
}
}
catch (error) {
throw new Error(`Invalid image URL: ${error.message}`);
}
}
else if (imageSource === 'binaryData') {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const binaryItem = (_a = items[i].binary) === null || _a === void 0 ? void 0 : _a[binaryPropertyName];
if (!binaryItem) {
throw new Error(`No binary data found in property ${binaryPropertyName}`);
}
const binaryData = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
if (!binaryData) {
throw new Error(`No binary data found in property ${binaryPropertyName}`);
}
const binaryMimeType = binaryItem.mimeType || 'image/png';
imageBase64 = `data:${binaryMimeType};base64,${binaryData.toString('base64')}`;
}
const body = {
model: 'Qubico/hunyuan',
task_type: taskType,
input: {
prompt,
image: imageBase64,
aspect_ratio: aspectRatio,
},
config: {
webhook_config: {
endpoint: '',
secret: '',
},
},
};
try {
const response = await GenericFunctions_1.piApiRequest.call(this, 'POST', '/api/v1/task', body);
if (response.code !== 200) {
throw new Error(`API Error: ${response.message}`);
}
const taskId = response.data.task_id;
let taskData = response.data;
if (waitForCompletion) {
taskData = await GenericFunctions_1.waitForTaskCompletion.call(this, taskId);
}
returnData.push({
json: taskData,
});
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.HunyuanImageToVideo = HunyuanImageToVideo;
//# sourceMappingURL=HunyuanImageToVideo.node.js.map
;