0

I have a list of numpy arrays lets call frames. I know I can use cv2.VideoWriter() to export a series of frames to a video file. However, I would like to just get the byte data instead of actually writing it to a file. My goal is to pass this video to the st.video() function. https://docs.streamlit.io/library/api-reference/media/st.video. Below is a rough outline of my current setup

vid = st.file_uploader(
    label="Upload Video",
    type=["mov", "mp4"]
)
if st.button("Load"):
    v = VideoClassInAnotherFile(vid)
    frames: list[np.ndarray] = v.process() # the function where I manipulate the frames using opencv
    byte_data = ... # TODO: turn the frames into byte data acceptable for st.video
    st.video(byte_data)
Austin Ulfers
  • 354
  • 6
  • 17
  • A possible solution is to use opencv's VideoWriter to save the data in temporary files that you then upload to streamlit – eyllanesc Nov 02 '21 at 02:47
  • Yes. I have already found that solution, but I am trying to achieve the same result without writing to a file at all. – Austin Ulfers Nov 02 '21 at 03:04
  • 1
    Then you have to look for a library that stores the n numpy.ndarray in memory (for example in a Io.BytesIo) with the mp4 format. Unfortunately neither opencv nor streamlit have that capacity – eyllanesc Nov 02 '21 at 03:06
  • Does this question point to this not being possible? https://stackoverflow.com/questions/51052268/streaming-video-in-memory-with-opencv-videowriter-and-python-bytesio/69792994 – Austin Ulfers Nov 02 '21 at 03:12
  • 1
    That's what I pointed out to you, opencv can't do it. – eyllanesc Nov 02 '21 at 03:15
  • I have a code sample that encodes BGR video frames (NumPy arrays) into a memory buffer using ffmpeg-python. It's not going to work with `mov` and `mp4` because they require "seekable output". (Error message is: `muxer does not support non seekable output`). It's working with MKV container, and when the video is not too long. I don't have any experience with streamlit. In case you want me to post an answer, modify your question to something like "How to encode video in memory?". – Rotem Nov 02 '21 at 11:45

0 Answers0