Using Streamlit's file_uploader for an imageio.get_reader function

I am hoping to allow a user to upload a mp4 or mkv clip with streamlitā€™s file_uploader function, and then use the uploaded video in an imageio.get_reader function. So far I have tried various options:

inp_vid = st.sidebar.file_uploader("Upload driving video", type=(["mp4", "mkv"]))
if inp_vid:
   reader = imageio.get_reader(inp_vid)

which results in a Could not find a format to read the specified file in any-mode mode' value error and a " Could not find a format to read the specified file in %s mode % modename" error in the get_reader funciton.

I have also tried:

inp_vid = st.sidebar.file_uploader("Upload driving video", type=(["mp4", "mkv"]))
if inp_vid:
        with tempfile.NamedTemporaryFile() as fp:
            fp.write_bytes(inp_vid.read())
            reader = imageio.get_reader(fp.name)
which gives a '_io.BufferedRandom' object has no attribute 'write_bytes' attribute error.

Iā€™ve also tried

inp_vid = st.sidebar.file_uploader("Upload driving video", type=(["mp4", "mkv"]))
if inp_vid:
        tfile = tempfile.NamedTemporaryFile(delete=False)
        tfile.write(inp_vid.read())

which gives the same error as the first code I mentioned.

Iā€™m evidently not really familiar with all the bytes IO stuff, so any help would be greatly appreciated!

According to the documentation, get_reader will accept bytes

This one should work, if you read the buffer (untested):

inp_vid = st.sidebar.file_uploader("Upload driving video", type=(["mp4", "mkv"]))
if inp_vid:
   reader = imageio.get_reader(inp_vid.read())

Thanks for the response! Unfortunately this still isnā€™t working. Iā€™m still getting the ā€˜Could not find a format to read the specified file in any-mode modeā€™ value error.

Unfortunately, not having experience with ImageIO, I donā€™t have any more suggestions. Youā€™ve got the bytes there, now itā€™s just a matter of convincing get_reader that the data you have is actually a video. Perhaps you can pass a value for format or mode, since it doesnā€™t seem to be autodetecting what you have.