import subprocess
import torch

p_buf = torch.empty(2160, 3840, 3, dtype=torch.uint8, pin_memory=True)
arr = p_buf.numpy()

# Test FFmpeg or dummy cat
proc = subprocess.Popen(["cat"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL)
try:
    mv = memoryview(arr)
    proc.stdin.write(mv)
    print("Direct 3D memoryview write succeeded!")
except Exception as e:
    print(f"Direct write failed: {e}")

try:
    # 1D flattened view or contiguous buffer
    mv_flat = memoryview(arr.reshape(-1))
    proc.stdin.write(mv_flat)
    print(f"Flattened 1D memoryview write succeeded! Bytes: {len(mv_flat)}")
except Exception as e:
    print(f"Flattened write failed: {e}")

proc.stdin.close()
proc.wait()
