-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Video.tsx
135 lines (119 loc) · 3.77 KB
/
Video.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { ReactElement, useEffect, useRef } from "react"
import { Video as VideoProto } from "@streamlit/lib/src/proto"
import { StreamlitEndpoints } from "@streamlit/lib/src/StreamlitEndpoints"
import { IS_DEV_ENV } from "@streamlit/lib/src/baseconsts"
const DEFAULT_HEIGHT = 528
export interface VideoProps {
endpoints: StreamlitEndpoints
width: number
element: VideoProto
}
export interface Subtitle {
label: string
url: string
}
export default function Video({
element,
width,
endpoints,
}: VideoProps): ReactElement {
const videoRef = useRef<HTMLVideoElement>(null)
/* Element may contain "url" or "data" property. */
const { type, url, startTime, subtitles } = element
// Handle startTime changes
useEffect(() => {
if (videoRef.current) {
videoRef.current.currentTime = startTime
}
}, [startTime])
useEffect(() => {
const videoNode = videoRef.current
const setStartTime: () => void = () => {
if (videoNode) {
// setStartTime
videoNode.currentTime = element.startTime
}
}
if (videoNode) {
videoNode.addEventListener("loadedmetadata", setStartTime)
}
return () => {
if (videoNode) {
videoNode.removeEventListener("loadedmetadata", setStartTime)
}
}
}, [element])
const getYoutubeSrc = (url: string): string => {
const { startTime } = element
if (startTime) {
return `${url}?start=${startTime}`
}
return url
}
/* Is this a YouTube link? If so we need a fancier tag.
NOTE: This part assumes the URL is already an "embed" link.
*/
if (type === VideoProto.Type.YOUTUBE_IFRAME) {
// At some point the width 0 will be passed to this component
// which is caused by the AutoSizer of the VerticalLayout
// Width 0 will result in height being 0, which results in issue
// https://github.com/streamlit/streamlit/issues/5069
// To avoid this, when we detect width is 0, we set height to 528,
// which is default height based on the default streamlit width
const height = width !== 0 ? width * 0.75 : DEFAULT_HEIGHT
return (
<iframe
data-testid="stVideo"
title={url}
src={getYoutubeSrc(url)}
width={width}
height={height}
style={{ colorScheme: "normal" }}
frameBorder="0"
allow="autoplay; encrypted-media"
allowFullScreen
/>
)
}
// Only in dev mode we set crossOrigin to "anonymous" to avoid CORS issues
// when streamlit frontend and backend are running on different ports
return (
<video
data-testid="stVideo"
ref={videoRef}
controls
src={endpoints.buildMediaURL(url)}
className="stVideo"
style={{ width, height: width === 0 ? DEFAULT_HEIGHT : undefined }}
crossOrigin={
IS_DEV_ENV && subtitles.length > 0 ? "anonymous" : undefined
}
>
{subtitles &&
subtitles.map((subtitle: Subtitle, idx: number) => (
<track
key={idx}
kind="captions"
src={endpoints.buildMediaURL(subtitle.url)}
label={subtitle.label}
default={idx === 0}
/>
))}
</video>
)
}