From 59bd8059208ae2b514b84d9b126d87022feb7700 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Mon, 2 Feb 2026 23:12:44 +0100 Subject: [PATCH] Fix ValueError flush of closed file in video encoding Replace communicate() with direct stderr.read() + wait() to avoid double-closing stdin. Catch BrokenPipeError for early ffmpeg exits. Co-Authored-By: Claude Opus 4.5 --- fast_saver.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/fast_saver.py b/fast_saver.py index 32d7250..b302b4c 100644 --- a/fast_saver.py +++ b/fast_saver.py @@ -314,11 +314,15 @@ class FastAbsoluteSaver: print(f"xx- FastSaver: Encoding {batch_size} frames to {out_file} ({codec}, crf={crf}, {fps}fps)...") proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE) - for img_tensor in images: - frame = (255.0 * img_tensor.cpu().numpy()).clip(0, 255).astype(np.uint8) - proc.stdin.write(frame.tobytes()) - proc.stdin.close() - _, stderr = proc.communicate() + try: + for img_tensor in images: + frame = (255.0 * img_tensor.cpu().numpy()).clip(0, 255).astype(np.uint8) + proc.stdin.write(frame.tobytes()) + proc.stdin.close() + except BrokenPipeError: + pass + stderr = proc.stderr.read() + proc.wait() if proc.returncode != 0: raise RuntimeError(f"ffmpeg failed: {stderr.decode()}")