File size: 2,052 Bytes
10dd95d
 
 
 
 
 
 
 
 
 
 
da43928
10dd95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da43928
 
 
 
 
 
10dd95d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

import os
import streamlit as st

# These are the formats supported in Streamlit right now.
VIDEO_EXTENSIONS = ["mp4", "ogv", "m4v", "webm"]

# For sample video files, try the Internet Archive, or download a few samples here:
# http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5


st.title("Video Player")

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


def get_video_files_in_dir(directory):
    out = []
    for item in os.listdir(directory):
        try:
            name, ext = item.split(".")
        except:
            continue
        if name and ext:
            if ext in VIDEO_EXTENSIONS:
                out.append(item)
    return out


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

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

else:
    filename = st.selectbox(
        "Select a video file from your home directory (%s) to play" % avdir,
        files,
        0,
    )

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


def shorten_vid_option(opt):
    return opt.split("/")[-1]


# A random sampling of videos found around the web.  We should replace
# these with those sourced from the streamlit community if possible!
vidurl = st.selectbox(
    "Pick a video to play",
    (
        "https://youtu.be/IoQsyHVFflU",
        "https://youtu.be/0i3U47OQs0E?list=PLHgX2IExbFosM9aYef9-1ymyx3QNUw3w1",
        "https://youtu.be/L8ObyHp9PY0?list=PLHgX2IExbFosM9aYef9-1ymyx3QNUw3w1",
        "https://youtu.be/x9vRg5KmLIo?list=PLHgX2IExbFosM9aYef9-1ymyx3QNUw3w1",
        "https://youtu.be/ZEchaKXaaas?list=PLHgX2IExbFosM9aYef9-1ymyx3QNUw3w1",
        "https://youtu.be/5yToL7ymfNo?list=PLHgX2IExbFosM9aYef9-1ymyx3QNUw3w1",
    ),
    0,
    shorten_vid_option,
)

st.video(vidurl)