{
  "name": "Image to Motion Video (Kie.ai) - Fixed & Optimized",
  "nodes": [
    {
      "parameters": {},
      "id": "Manual_Trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "filePath": "/home/user/input/image.jpg"
      },
      "id": "Read_Image",
      "name": "Read Image",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        200,
        150
      ]
    },
    {
      "parameters": {
        "filePath": "/home/user/input/reference.mp4"
      },
      "id": "Read_Video",
      "name": "Read Video",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        200,
        450
      ]
    },
    {
      "parameters": {
        "url": "https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/image/upload",
        "method": "POST",
        "sendBinaryData": true,
        "binaryPropertyName": "data",
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "upload_preset",
              "value": "YOUR_UPLOAD_PRESET"
            }
          ]
        }
      },
      "id": "Upload_Image",
      "name": "Upload Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        400,
        150
      ]
    },
    {
      "parameters": {
        "url": "https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/video/upload",
        "method": "POST",
        "sendBinaryData": true,
        "binaryPropertyName": "data",
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "upload_preset",
              "value": "YOUR_UPLOAD_PRESET"
            }
          ]
        }
      },
      "id": "Upload_Video",
      "name": "Upload Video",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        400,
        450
      ]
    },
    {
      "parameters": {
        "mode": "chooseData",
        "outputData": "combined"
      },
      "id": "Merge",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2.1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.kie.ai/v1/video/motion",
        "method": "POST",
        "jsonParameters": true,
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_KIE_API_KEY"
            }
          ]
        },
        "bodyParametersJson": "={\n  \"source_image\": \"{{ $('Upload Image').item.json.secure_url }}\",\n  \"driving_video\": \"{{ $('Upload Video').item.json.secure_url }}\",\n  \"motion_strength\": 0.75,\n  \"lip_sync\": true,\n  \"expression_transfer\": true,\n  \"face_enhance\": true\n}"
      },
      "id": "Call_Kie",
      "name": "Call Kie Motion",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "loop_counter",
              "value": 0
            }
          ]
        }
      },
      "id": "Init_Counter",
      "name": "Init Counter",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "waitTime": 20
      },
      "id": "Wait",
      "name": "Wait 20s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://api.kie.ai/v1/video/status/{{ $('Call Kie Motion').item.json.job_id }}",
        "method": "GET",
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_KIE_API_KEY"
            }
          ]
        }
      },
      "id": "Check_Status",
      "name": "Check Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1400,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.status }}",
              "operation": "equal",
              "value2": "completed"
            }
          ]
        }
      },
      "id": "If_Complete",
      "name": "Is Done?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1600,
        300
      ]
    },
    {
      "parameters": {
        "assignmentType": "increment",
        "fields": {
          "values": [
            {
              "name": "loop_counter",
              "value": 1
            }
          ]
        }
      },
      "id": "Increment",
      "name": "Increment",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        1400,
        500
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.loop_counter }}",
              "operation": "smaller",
              "value2": 20
            }
          ]
        }
      },
      "id": "Retry_Check",
      "name": "Retry?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1200,
        500
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.result_video_url }}",
        "method": "GET",
        "responseFormat": "file"
      },
      "id": "Download",
      "name": "Download",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1800,
        200
      ]
    },
    {
      "parameters": {
        "filePath": "/home/user/output/final_video.mp4"
      },
      "id": "Save_File",
      "name": "Save Video",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        2000,
        200
      ]
    }
  ],
  "connections": {
    "Manual_Trigger": {
      "main": [
        [
          {
            "node": "Read_Image",
            "index": 0
          },
          {
            "node": "Read_Video",
            "index": 0
          }
        ]
      ]
    },
    "Read_Image": {
      "main": [
        [
          {
            "node": "Upload_Image",
            "index": 0
          }
        ]
      ]
    },
    "Read_Video": {
      "main": [
        [
          {
            "node": "Upload_Video",
            "index": 0
          }
        ]
      ]
    },
    "Upload_Image": {
      "main": [
        [
          {
            "node": "Merge",
            "index": 0
          }
        ]
      ]
    },
    "Upload_Video": {
      "main": [
        [
          {
            "node": "Merge",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Call_Kie",
            "index": 0
          }
        ]
      ]
    },
    "Call_Kie": {
      "main": [
        [
          {
            "node": "Init_Counter",
            "index": 0
          }
        ]
      ]
    },
    "Init_Counter": {
      "main": [
        [
          {
            "node": "Wait",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Check_Status",
            "index": 0
          }
        ]
      ]
    },
    "Check_Status": {
      "main": [
        [
          {
            "node": "If_Complete",
            "index": 0
          }
        ]
      ]
    },
    "If_Complete": {
      "main": [
        [
          {
            "node": "Download",
            "index": 0
          }
        ],
        [
          {
            "node": "Increment",
            "index": 0
          }
        ]
      ]
    },
    "Increment": {
      "main": [
        [
          {
            "node": "Retry_Check",
            "index": 0
          }
        ]
      ]
    },
    "Retry_Check": {
      "main": [
        [
          {
            "node": "Wait",
            "index": 0
          }
        ]
      ]
    },
    "Download": {
      "main": [
        [
          {
            "node": "Save_File",
            "index": 0
          }
        ]
      ]
    }
  }
}