streamlit
1# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os16
17import streamlit as st18
19# These are the formats supported in Streamlit right now.
20VIDEO_EXTENSIONS = ["mp4", "ogv", "m4v", "webm"]21
22# For sample video files, try the Internet Archive, or download a few samples here:
23# http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5
24
25
26st.title("Video Widget Examples")27
28st.header("Local video files")29st.write(30"You can use st.video to play a locally-stored video by supplying it with a valid filesystem path."31)
32
33
34def get_video_files_in_dir(directory):35out = []36for item in os.listdir(directory):37try:38name, ext = item.split(".")39except:40continue41if name and ext:42if ext in VIDEO_EXTENSIONS:43out.append(item)44return out45
46
47avdir = os.path.expanduser("~")48files = get_video_files_in_dir(avdir)49
50if len(files) == 0:51st.write(52"Put some video files in your home directory (%s) to activate this player."53% avdir54)55
56else:57filename = st.selectbox(58"Select a video file from your home directory (%s) to play" % avdir,59files,600,61)62
63st.video(os.path.join(avdir, filename))64st.header("Remote video playback")65st.write("st.video allows a variety of HTML5 supported video links, including YouTube.")66
67
68def shorten_vid_option(opt):69return opt.split("/")[-1]70
71
72# A random sampling of videos found around the web. We should replace
73# these with those sourced from the streamlit community if possible!
74vidurl = st.selectbox(75"Pick a video to play",76(77"https://youtu.be/_T8LGqJtuGc",78"https://www.youtube.com/watch?v=kmfC-i9WgH0",79"https://www.youtube.com/embed/sSn4e1lLVpA",80"http://www.rochikahn.com/video/videos/zapatillas.mp4",81"http://www.marmosetcare.com/video/in-the-wild/intro.webm",82"https://www.orthopedicone.com/u/home-vid-4.mp4",83),840,85shorten_vid_option,86)
87
88st.video(vidurl)89