2 2 Displaying Videos Explained
Key Concepts
- st.video: A Streamlit function to embed and display videos.
- Video Sources: The types of sources from which videos can be loaded.
- Video Formats: The supported video formats for embedding.
- Customization: Options to customize the video player.
Explanation
1. st.video
st.video
is a Streamlit function that allows you to embed and display videos directly within your application. This function is particularly useful for showcasing tutorials, demos, or any other video content relevant to your app.
2. Video Sources
Videos can be loaded from various sources, including local files, URLs, and cloud storage services. The source can be specified as a file path or a URL string.
3. Video Formats
Streamlit supports a variety of video formats, including MP4, WebM, and Ogg. Ensure that the video format is compatible with the browser for optimal playback.
4. Customization
While st.video
offers limited customization options, you can control the start time of the video using the start_time
parameter. This allows you to skip to a specific part of the video when it starts playing.
Examples
Example 1: Embedding a Local Video
import streamlit as st video_file = open('example.mp4', 'rb') video_bytes = video_file.read() st.video(video_bytes)
Example 2: Embedding a Video from a URL
import streamlit as st video_url = "https://www.example.com/video.mp4" st.video(video_url)
Example 3: Customizing the Start Time
import streamlit as st video_url = "https://www.example.com/video.mp4" st.video(video_url, start_time=120)
Analogies
Think of st.video
as a video player embedded directly into your Streamlit app. Just like a DVD player can play videos from a disc, st.video
can play videos from various sources. The start time customization is like skipping to a specific chapter in a DVD.
By mastering the use of st.video
, you can enhance your Streamlit applications by embedding rich multimedia content, making your apps more engaging and informative.