Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create video into buffer with python

I have several images which took from a video. For now, I want to convert images to a video. But I don't want to save this video on my disk, I just want to save it to buffer (e.g.: StringIO, BytesIO, etc.).

I tried PyAV to write a video.

output = av.open('test.mp4', 'w')
stream = output.add_stream('mpeg4', 30)
for image in images:
    frame = av.VideoFrame.from_ndarray(image, format='rgb24')
    packet = stream.encode(frame)
    output.mux(packet)
output.close()

The former code can generate the test.mp4 file. But how to write it into buffer instead of a file? Use openCV?

Thanks.

like image 267
MarshalSHI Avatar asked Feb 22 '26 13:02

MarshalSHI


1 Answers

PyAV Documentation Specify That, you need to call decode from packet, even to get encoded data

Packet() A packet of encoded data within a Stream.

This may, or may not include a complete object within a stream. decode() must be called to extract encoded data.

like image 185
Anshul Avatar answered Feb 25 '26 07:02

Anshul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!