Output video using OpenCV h264 mp4s on Streamlit Sharing

I am uploading a video to Streamlit sharing, OpenCV is processing it, and encoding it into an mp4. The video is shown via st.video(), and then someone using the site can download the video.

This all works locally, on Windows, but not on Streamlit Sharing, due to codecs and stuff.

For Windows (local):
The mp4v codec encodes the video properly, doesn’t show the video because html5 can’t play it (“No video with supported format and MIME found”)
The avc1 codec works when Cisco’s openh264-1.8.0-win64.dll is in the same directory, encoding and then the video is playable. This solution came from this stack overflow post on using opencv to output h264
Avc3 behaves the same as avc1.

For Debian Buster (what Streamlit sharing uses):
mp4v doesn’t work, as expected, still due to the MIME error.
avc1 and avc3 don’t work, even though I have the Linux equivalent, libopenh264-1.8.0-linux64.4.so from cisco in the repository. I get the error:
open Could not find encoder for codec_id=27, error: Encoder not found
and the encoding fails.

Basically, it seems like there’s no perfect setup that let’s me encode AND display on Streamlit Sharing.
Other things I’ve tried:
packages.txt with ffmpeg, x264, and libx264-dev so that the OS will install those first.
Using h264, hev1 seem to default to avc1 so they don’t work.

The codec is set using:
out = cv2.VideoWriter(output_name, cv2.VideoWriter_fourcc(*'avc3'), fps, insize)

1 Like