diff --git a/kie_api/kling26_i2v.py b/kie_api/kling26_i2v.py index d3dd927..e13be04 100644 --- a/kie_api/kling26_i2v.py +++ b/kie_api/kling26_i2v.py @@ -14,9 +14,7 @@ from .log import _log from .results import _extract_result_urls from .upload import _image_tensor_to_png_bytes, _truncate_url, _upload_image from .validation import _validate_prompt -from .video import _download_video, _video_path_to_comfy_video_output, _write_video_to_temp_file - - +from .video import _download_video, _video_bytes_to_comfy_video CREATE_TASK_URL = "https://api.kie.ai/api/v1/jobs/createTask" MODEL_NAME = "kling-2.6/image-to-video" PROMPT_MAX_LENGTH = 1000 @@ -124,15 +122,13 @@ def run_kling26_i2v_video( result_urls = _extract_result_urls(record_data) video_url = result_urls[0] - _log(True, f"Video result URL: {video_url}") - _log(log, f"Downloading video result from {video_url}...") + _log(log, f"Final video URL: {video_url}") video_bytes = _download_video(video_url) - video_path = _write_video_to_temp_file(video_bytes) - _log(log, f"Video saved to {video_path}") + video_output = _video_bytes_to_comfy_video(video_bytes) _log_remaining_credits(log, record_data, api_key, _log) - return _video_path_to_comfy_video_output(video_path) + return video_output def run_kling26_i2v( diff --git a/kie_api/video.py b/kie_api/video.py index 6032903..b3f495d 100644 --- a/kie_api/video.py +++ b/kie_api/video.py @@ -1,7 +1,8 @@ -"""Shared helpers for handling video results from KIE models.""" +"""Video helpers shared across KIE models.""" -from pathlib import Path -from tempfile import NamedTemporaryFile +from io import BytesIO + +from comfy_api.latest.input_impl.video_types import VideoFromFile from .http import requests @@ -19,25 +20,8 @@ def _download_video(url: str) -> bytes: return response.content -def _write_video_to_temp_file(video_bytes: bytes, suffix: str = ".mp4") -> str: - """Persist video bytes to a temporary .mp4 file and return its path.""" - try: - temp_file = NamedTemporaryFile(delete=False, suffix=suffix) - with temp_file: - temp_file.write(video_bytes) - return str(Path(temp_file.name)) - except OSError as exc: - raise RuntimeError("Failed to write temporary video file.") from exc - - -def _video_path_to_comfy_video_output(video_path: str) -> dict: - """Wrap a local video path in a ComfyUI VIDEO-compatible dict.""" - path = Path(video_path) - suffix = path.suffix.lstrip(".") or "mp4" - resolved = path.expanduser().resolve() - return { - "path": str(resolved), - "format": suffix, - "frame_rate": None, - "frame_count": None, - } +def _video_bytes_to_comfy_video(video_bytes: bytes) -> VideoFromFile: + """Convert raw MP4 bytes into a ComfyUI VIDEO object.""" + buffer = BytesIO(video_bytes) + buffer.name = "kie_temp_video.mp4" + return VideoFromFile(buffer)