# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"2": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_2_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"3": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_1_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"13": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_2_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"14": {
"class_type": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_1_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"27": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/ComfyUI",
"format": "auto",
"format.codec": "auto",
"video": [
"498:482",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"498:482": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"498:496",
0
],
"on_true": [
"498:491",
0
],
"switch": False
},
"_meta": {
"title": "If/Else Switch"
}
},
"498:483": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"14",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:484": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"13",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:485": {
"class_type": "BatchImagesNode",
"inputs": {
"images.image0": [
"498:483",
0
],
"images.image1": [
"498:484",
0
]
},
"_meta": {
"title": "Batch Images"
}
},
"498:486": {
"class_type": "CreateVideo",
"inputs": {
"audio": [
"498:487",
0
],
"bit_depth": "auto",
"codec": "none",
"color_space": "sRGB",
"fps": [
"498:483",
2
],
"images": [
"498:485",
0
]
},
"_meta": {
"title": "Create Video"
}
},
"498:487": {
"class_type": "AudioConcat",
"inputs": {
"audio1": [
"498:483",
1
],
"audio2": [
"498:484",
1
],
"direction": "after"
},
"_meta": {
"title": "Concatenate Audio"
}
},
"498:488": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"498:496",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:489": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"498:486",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:490": {
"class_type": "BatchImagesNode",
"inputs": {
"images.image0": [
"498:488",
0
],
"images.image1": [
"498:489",
0
]
},
"_meta": {
"title": "Batch Images"
}
},
"498:491": {
"class_type": "CreateVideo",
"inputs": {
"audio": [
"498:492",
0
],
"bit_depth": "auto",
"codec": "none",
"color_space": "sRGB",
"fps": [
"498:488",
2
],
"images": [
"498:490",
0
]
},
"_meta": {
"title": "Create Video"
}
},
"498:492": {
"class_type": "AudioConcat",
"inputs": {
"audio1": [
"498:488",
1
],
"audio2": [
"498:489",
1
],
"direction": "after"
},
"_meta": {
"title": "Concatenate Audio"
}
},
"498:493": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"3",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:494": {
"class_type": "GetVideoComponents",
"inputs": {
"video": [
"2",
0
]
},
"_meta": {
"title": "Get Video Components"
}
},
"498:495": {
"class_type": "BatchImagesNode",
"inputs": {
"images.image0": [
"498:493",
0
],
"images.image1": [
"498:494",
0
]
},
"_meta": {
"title": "Batch Images"
}
},
"498:496": {
"class_type": "CreateVideo",
"inputs": {
"audio": [
"498:497",
0
],
"bit_depth": "auto",
"codec": "none",
"color_space": "sRGB",
"fps": [
"498:493",
2
],
"images": [
"498:495",
0
]
},
"_meta": {
"title": "Create Video"
}
},
"498:497": {
"class_type": "AudioConcat",
"inputs": {
"audio1": [
"498:493",
1
],
"audio2": [
"498:494",
1
],
"direction": "after"
},
"_meta": {
"title": "Concatenate Audio"
}
}
})
workflow.set_input("3", "file", client.assets.from_url("https://comfy.org/catalog-media/5e42bcb0-8679-4b9f-b107-58361ec35ea2/6e6eede3-7e1c-49a8-8c5c-185adbd776d7"))
workflow.set_input("14", "file", client.assets.from_url("https://comfy.org/catalog-media/5e42bcb0-8679-4b9f-b107-58361ec35ea2/6e6eede3-7e1c-49a8-8c5c-185adbd776d7"))
workflow.set_input("2", "file", client.assets.from_url("https://comfy.org/catalog-media/ac1f2ff9-3684-4eb3-90b6-f3878432cf5c/41471c67-b27d-4508-83d6-600a29306fbe"))
workflow.set_input("13", "file", client.assets.from_url("https://comfy.org/catalog-media/ac1f2ff9-3684-4eb3-90b6-f3878432cf5c/41471c67-b27d-4508-83d6-600a29306fbe"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)
for output in job.get_outputs("27"):
output.to_file(output.name)