diff --git a/comfy_api/latest/_input_impl/video_types.py b/comfy_api/latest/_input_impl/video_types.py index 9f498a6bb..09db94198 100644 --- a/comfy_api/latest/_input_impl/video_types.py +++ b/comfy_api/latest/_input_impl/video_types.py @@ -707,13 +707,17 @@ class VideoFromFile(VideoInput): elif packet.stream == audio_stream and not audio_done: for resampled in itertools.chain.from_iterable(map(resampler.resample, packet.decode())): + frame_start = None + if resampled.pts is not None: + # passthrough frames keep the source stream's time base + tb = resampled.time_base if resampled.time_base else audio_time_base + frame_start = float(resampled.pts * tb) + if duration and not audio_started and frame_start >= start_time + duration: + audio_done = True + break if not audio_started: - if resampled.pts is None: + if frame_start is None: frame_start = 0.0 - else: - # passthrough frames keep the source stream's time base - tb = resampled.time_base if resampled.time_base else audio_time_base - frame_start = float(resampled.pts * tb) to_skip = max(0, int((start_time - frame_start) * sample_rate)) if to_skip >= resampled.samples: continue diff --git a/tests-unit/comfy_api_test/video_types_test.py b/tests-unit/comfy_api_test/video_types_test.py index 9d78b28b3..3dc7055f6 100644 --- a/tests-unit/comfy_api_test/video_types_test.py +++ b/tests-unit/comfy_api_test/video_types_test.py @@ -481,6 +481,12 @@ def test_save_to_transcode_learns_unprobed_audio_params(): assert result["audio_codecs"] == ["aac"] assert result["audio_seconds"] == pytest.approx(1.0, abs=0.1) + buffer.seek(0) + trimmed_before_audio = transcode_and_probe(VideoFromFile(buffer, duration=1)) + assert trimmed_before_audio["frames"] == fps + assert trimmed_before_audio["audio_codecs"] == [] + assert trimmed_before_audio["audio_seconds"] is None + def test_save_to_transcode_trimmed_fragmented_mp4_keeps_audio(): """Fragmented mp4 (MediaRecorder, DASH/HLS-derived files) delivers audio well behind