Skip to content

Add numpy read/write for MgVideo/MgAudio and memory-based grid processing#342

Merged
alexarje merged 3 commits intomasterfrom
copilot/read-write-original-files-as-arrays
Apr 15, 2026
Merged

Add numpy read/write for MgVideo/MgAudio and memory-based grid processing#342
alexarje merged 3 commits intomasterfrom
copilot/read-write-original-files-as-arrays

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 15, 2026

Adds the ability to read/write MgVideo and MgAudio objects as numpy arrays, initialize MgVideo from an array, and return grid images as arrays instead of writing to disk.

Changes

Bug fix: from_numpy path handling

When constructing MgVideo(filename=..., array=..., fps=..., path=...), the file was correctly written to path/filename but self.filename was left as the bare filename, causing an immediate FileNotFoundError in get_video(). Fixed by updating self.filename, self.of, and self.fex to the actual write path after FFmpeg completes.

Features (previously implemented, now tested and documented)

  • MgVideo.numpy() – returns (array, fps) where array has shape (N, H, W, 3), BGR uint8
  • MgAudio.numpy() – returns the audio waveform as a 1-D float array
  • MgVideo(filename, array=array, fps=fps, path=path) – initializes from a numpy array, writing the encoded video to disk
  • MgVideo.from_numpy(array, fps, target_name=None) – writes an array directly to a specified output path
  • mg_grid(return_array=True) – returns the grid image as a numpy array without writing to disk

Usage

import musicalgestures

# Read frames as array
video = musicalgestures.MgVideo("input.mp4")
array, fps = video.numpy()           # shape: (N, H, W, 3), uint8 BGR

# Round-trip: write array back to a new MgVideo
new_video = musicalgestures.MgVideo(
    filename="output.avi",
    array=array,
    fps=fps,
    path="/some/output/dir",         # was broken before this fix
)

# Memory-based grid preview (no file written)
grid = video.grid(height=300, rows=3, cols=3, return_array=True)  # numpy array

Tests

Added tests/test_numpy.py with 18 cases covering all of the above, including the path-parameter regression.

Copilot AI and others added 2 commits April 15, 2026 08:56
Agent-Logs-Url: https://github.com/fourMs/MGT-python/sessions/804afced-8cfa-4b53-b671-348fedf3ab55

Co-authored-by: alexarje <114316+alexarje@users.noreply.github.com>
Copilot AI changed the title [WIP] Add functionality to read/write original files as arrays Add numpy read/write for MgVideo/MgAudio and memory-based grid processing Apr 15, 2026
Copilot AI requested a review from alexarje April 15, 2026 09:07
@alexarje alexarje marked this pull request as ready for review April 15, 2026 09:08
@alexarje alexarje merged commit c6bffae into master Apr 15, 2026
4 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Read/write original files as arrays, and a completely memory-based processing flow

2 participants