Streamlit video – Streamlit video stream – Projectpro
Recipe Objective: How to display a video in streamlit?
“st.video()” is used to display video in Streamlit.
Syntax: st.video(data, format=’video/mp4′, start_time=0)
Parameters:
data -> It could be raw video data, filename, or a URL to the file to load. Must be opened using io.open(). Streamlit also includes support for YouTube URLs.
format -> It is the The mime type for the video file. If nothing is specified it defaults to ‘video/mp4’.
start_time -> The time entered as integer from which this element should start playing.
Note: Since the MP4V codec (which is an export option in OpenCV) is not generally supported by browsers, some videos may not display. Your video will be able to be displayed in Streamlit after being converted to H.264.
Code:
#importing streamlit library
import streamlit as st
#displaying a local video file
video_file = open('FILENAME', 'rb') #enter the filename with filepath
video_bytes = video_file.read() #reading the file
st.video(video_bytes) #displaying the video
#displaying a video by simply passing a Youtube link
st.video("https://youtu.be/yVV_t_Tewvs")
To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the “streamlit run appname.py” command. Once you run the command, the app will automatically open in your default browser.