The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
fix(Video): don't crash on videos with undecodable audio streams (#14746)
Alexander Piskuna3020f107e30
diff --git a/comfy_api/latest/_input_impl/video_types.py b/comfy_api/latest/_input_impl/video_types.pyindex 6c69256ab..bc95a5b99 100644--- a/comfy_api/latest/_input_impl/video_types.py+++ b/comfy_api/latest/_input_impl/video_types.py@@ -281,11 +281,18 @@ class VideoFromFile(VideoInput):video_done = Falseaudio_done = True- if len(container.streams.audio):- audio_stream = container.streams.audio[-1]+ # Use the last decodable audio stream. Streams FFmpeg has no decoder for have no codec context,+ # and decoding their packets crashes the process. (e.g. APAC spatial-audio track in iPhone)+ audio_stream = next(+ (s for s in reversed(container.streams.audio) if s.codec_context is not None),+ None,+ )+ if audio_stream is not None:streams += [audio_stream]resampler = av.audio.resampler.AudioResampler(format='fltp')audio_done = False+ elif len(container.streams.audio):+ logging.warning("No decodable audio stream found in video; ignoring audio.")for packet in container.demux(*streams):if video_done and audio_done:@@ -457,10 +464,13 @@ class VideoFromFile(VideoInput):else:output_container.metadata[key] = json.dumps(value)- # Add streams to the new container+ # Add streams to the new container. Streams with no codec context cannot be used as an output template.stream_map = {}for stream in streams:if isinstance(stream, (av.VideoStream, av.AudioStream, SubtitleStream)):+ if stream.codec_context is None:+ logging.warning("Skipping %s stream %d with unsupported codec", stream.type, stream.index)+ continueout_stream = output_container.add_stream_from_template(template=stream, opaque=True)stream_map[stream] = out_streamdiff --git a/comfy_api_nodes/util/upload_helpers.py b/comfy_api_nodes/util/upload_helpers.pyindex 6d1d107a1..f7029ee78 100644--- a/comfy_api_nodes/util/upload_helpers.py+++ b/comfy_api_nodes/util/upload_helpers.py@@ -158,7 +158,14 @@ async def upload_video_to_comfyapi(# Convert VideoInput to BytesIO using specified container/codecvideo_bytes_io = BytesIO()- video.save_to(video_bytes_io, format=container, codec=codec)+ try:+ video.save_to(video_bytes_io, format=container, codec=codec)+ except Exception as e:+ raise ValueError(+ f"Could not convert the input video to {container.value.upper()} for upload; "+ f"the file may be corrupted or use an unsupported codec. "+ f"Try re-exporting it as MP4 (H.264). Original error: {e}"+ ) from evideo_bytes_io.seek(0)return await upload_file_to_comfyapi(cls, video_bytes_io, filename, upload_mime_type, wait_label)