fix(video): ensure contiguous arrays for both audio and video paths
- Fix potential BufferError in video frame writing path by enforcing C-contiguity with np.ascontiguousarray - Add regression tests for non-contiguous array writing to Popen.stdin - Rename variables for clarity (images_bytes -> image_chunks) - Ensure cross-platform compatibility in tests
This commit is contained in:
@@ -640,7 +640,8 @@ class DiscordSendSaveVideo:
|
||||
|
||||
# Convert tensor images to bytes
|
||||
# Optimization: Use tensor_to_numpy_uint8 for faster conversion
|
||||
image_chunks = map(lambda x: tensor_to_numpy_uint8(x), image_sequence)
|
||||
# Ensure contiguity to avoid ValueError in subprocess.stdin.write
|
||||
image_chunks = map(lambda x: np.ascontiguousarray(tensor_to_numpy_uint8(x)), image_sequence)
|
||||
|
||||
# Base ffmpeg arguments
|
||||
args = [
|
||||
@@ -697,7 +698,8 @@ class DiscordSendSaveVideo:
|
||||
i_pix_fmt = 'rgb24'
|
||||
|
||||
# Optimization: Use tensor_to_numpy_uint8 for faster conversion
|
||||
image_chunks = map(lambda x: tensor_to_numpy_uint8(x), image_sequence)
|
||||
# Ensure contiguity to avoid ValueError in subprocess.stdin.write
|
||||
image_chunks = map(lambda x: np.ascontiguousarray(tensor_to_numpy_uint8(x)), image_sequence)
|
||||
|
||||
# Set up ffmpeg arguments based on format
|
||||
loop_args = []
|
||||
|
||||
@@ -66,5 +66,36 @@ class TestNumpyToSubprocess(unittest.TestCase):
|
||||
res = subprocess.run(cmd, input=mv, capture_output=True)
|
||||
self.assertEqual(res.stdout, contiguous_data.tobytes())
|
||||
|
||||
def test_popen_stdin_write_fixed_non_contiguous(self):
|
||||
"""
|
||||
Test writing fixed (made contiguous) numpy array to Popen.stdin.
|
||||
"""
|
||||
# Create a 2D array and transpose it to make it non-contiguous
|
||||
data = np.zeros((10, 10), dtype=np.uint8)
|
||||
# Fill with some data
|
||||
for i in range(10):
|
||||
for j in range(10):
|
||||
data[i, j] = i + j
|
||||
|
||||
transposed_data = data.T
|
||||
self.assertFalse(transposed_data.flags['C_CONTIGUOUS'])
|
||||
|
||||
# Fix it
|
||||
contiguous_data = np.ascontiguousarray(transposed_data)
|
||||
self.assertTrue(contiguous_data.flags['C_CONTIGUOUS'])
|
||||
|
||||
# Use python to echo input to output (cross-platform)
|
||||
cmd = [sys.executable, '-c', 'import sys; sys.stdout.buffer.write(sys.stdin.buffer.read())']
|
||||
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
try:
|
||||
# Should succeed now
|
||||
p.stdin.write(contiguous_data)
|
||||
out, _ = p.communicate()
|
||||
self.assertEqual(out, contiguous_data.tobytes())
|
||||
|
||||
except Exception as e:
|
||||
self.fail(f"Caught unexpected exception: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user