streamlit

Форк
0
/
video.py 
88 строк · 2.5 Кб
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

15
import os
16

17
import streamlit as st
18

19
# These are the formats supported in Streamlit right now.
20
VIDEO_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

26
st.title("Video Widget Examples")
27

28
st.header("Local video files")
29
st.write(
30
    "You can use st.video to play a locally-stored video by supplying it with a valid filesystem path."
31
)
32

33

34
def get_video_files_in_dir(directory):
35
    out = []
36
    for item in os.listdir(directory):
37
        try:
38
            name, ext = item.split(".")
39
        except:
40
            continue
41
        if name and ext:
42
            if ext in VIDEO_EXTENSIONS:
43
                out.append(item)
44
    return out
45

46

47
avdir = os.path.expanduser("~")
48
files = get_video_files_in_dir(avdir)
49

50
if len(files) == 0:
51
    st.write(
52
        "Put some video files in your home directory (%s) to activate this player."
53
        % avdir
54
    )
55

56
else:
57
    filename = st.selectbox(
58
        "Select a video file from your home directory (%s) to play" % avdir,
59
        files,
60
        0,
61
    )
62

63
    st.video(os.path.join(avdir, filename))
64
st.header("Remote video playback")
65
st.write("st.video allows a variety of HTML5 supported video links, including YouTube.")
66

67

68
def shorten_vid_option(opt):
69
    return 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!
74
vidurl = 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
    ),
84
    0,
85
    shorten_vid_option,
86
)
87

88
st.video(vidurl)
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.