-
Notifications
You must be signed in to change notification settings - Fork 232
perf: optimize ffmpeg video saving #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -260,6 +260,7 @@ def save_to_video( | |||||||||||||||||||||||||
| # Get ffmpeg executable from imageio_ffmpeg | ||||||||||||||||||||||||||
| ffmpeg_exe = ffmpeg.get_ffmpeg_exe() | ||||||||||||||||||||||||||
| out_pix = output_pix_fmt or "yuv420p" | ||||||||||||||||||||||||||
| ffmpeg_preset = os.environ.get("LIGHTX2V_FFMPEG_PRESET", "").strip() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if lossless: | ||||||||||||||||||||||||||
| command = [ | ||||||||||||||||||||||||||
|
|
@@ -283,9 +284,10 @@ def save_to_video( | |||||||||||||||||||||||||
| "libx264rgb", | ||||||||||||||||||||||||||
| "-crf", | ||||||||||||||||||||||||||
| "0", | ||||||||||||||||||||||||||
| "-an", # No audio | ||||||||||||||||||||||||||
| output_path, | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| if ffmpeg_preset: | ||||||||||||||||||||||||||
| command.extend(["-preset", ffmpeg_preset]) | ||||||||||||||||||||||||||
| command.extend(["-an", output_path]) # No audio | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| command = [ | ||||||||||||||||||||||||||
| ffmpeg_exe, | ||||||||||||||||||||||||||
|
|
@@ -308,9 +310,10 @@ def save_to_video( | |||||||||||||||||||||||||
| "libx264", | ||||||||||||||||||||||||||
| "-pix_fmt", | ||||||||||||||||||||||||||
| out_pix, | ||||||||||||||||||||||||||
| "-an", # No audio | ||||||||||||||||||||||||||
| output_path, | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| if ffmpeg_preset: | ||||||||||||||||||||||||||
| command.extend(["-preset", ffmpeg_preset]) | ||||||||||||||||||||||||||
| command.extend(["-an", output_path]) # No audio | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Run FFmpeg (stderr to DEVNULL: avoids pipe buffer deadlock; no need to capture for errors) | ||||||||||||||||||||||||||
| process = subprocess.Popen( | ||||||||||||||||||||||||||
|
|
@@ -322,14 +325,15 @@ def save_to_video( | |||||||||||||||||||||||||
| if process.stdin is None: | ||||||||||||||||||||||||||
| raise BrokenPipeError("No stdin buffer received.") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Write frames to FFmpeg | ||||||||||||||||||||||||||
| for frame in frames: | ||||||||||||||||||||||||||
| # Pad frame if needed | ||||||||||||||||||||||||||
| if frame.shape[0] < height or frame.shape[1] < width: | ||||||||||||||||||||||||||
| padded = np.zeros((height, width, 3), dtype=np.uint8) | ||||||||||||||||||||||||||
| padded[: frame.shape[0], : frame.shape[1]] = frame | ||||||||||||||||||||||||||
| frame = padded | ||||||||||||||||||||||||||
| process.stdin.write(frame.tobytes()) | ||||||||||||||||||||||||||
| if frames.shape[1] == height and frames.shape[2] == width: | ||||||||||||||||||||||||||
| process.stdin.write(np.ascontiguousarray(frames).tobytes()) | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| for frame in frames: | ||||||||||||||||||||||||||
| if frame.shape[0] < height or frame.shape[1] < width: | ||||||||||||||||||||||||||
| padded = np.zeros((height, width, 3), dtype=np.uint8) | ||||||||||||||||||||||||||
| padded[: frame.shape[0], : frame.shape[1]] = frame | ||||||||||||||||||||||||||
| frame = padded | ||||||||||||||||||||||||||
| process.stdin.write(frame.tobytes()) | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, calling
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| process.stdin.close() | ||||||||||||||||||||||||||
| process.wait() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
.tobytes()on a numpy array creates a full copy of the array data as a Pythonbytesobject in memory. For large video tensors, this can cause a significant memory spike and unnecessary CPU overhead.Since
framesis already a contiguous numpy array (due to the.copy()on line 252), andprocess.stdin.write()accepts any object implementing the buffer protocol (including contiguous numpy arrays), you can writeframesdirectly to the pipe without any copying.