gradio-pr-bot commited on
Commit
0fda32d
·
verified ·
1 Parent(s): 437f931

Upload folder using huggingface_hub

Browse files
6.8.1/video/Example.svelte ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Video from "./shared/Video.svelte";
3
+ import { playable } from "./shared/utils";
4
+ import { type FileData } from "@gradio/client";
5
+
6
+ interface Props {
7
+ type: "gallery" | "table";
8
+ selected?: boolean;
9
+ value?: null | FileData;
10
+ loop: boolean;
11
+ }
12
+
13
+ let { type, selected = false, value = null, loop }: Props = $props();
14
+
15
+ let video: HTMLVideoElement | undefined = $state();
16
+
17
+ async function init(): Promise<void> {
18
+ if (!video) return;
19
+ video.muted = true;
20
+ video.playsInline = true;
21
+ video.controls = false;
22
+ video.setAttribute("muted", "");
23
+
24
+ await video.play();
25
+ video.pause();
26
+ }
27
+ </script>
28
+
29
+ {#if value}
30
+ {#if playable()}
31
+ <div
32
+ class="container"
33
+ class:table={type === "table"}
34
+ class:gallery={type === "gallery"}
35
+ class:selected
36
+ >
37
+ <Video
38
+ muted
39
+ playsinline
40
+ bind:node={video}
41
+ onloadeddata={init}
42
+ onmouseover={() => video?.play()}
43
+ onmouseout={() => video?.pause()}
44
+ src={value?.url}
45
+ is_stream={false}
46
+ {loop}
47
+ />
48
+ </div>
49
+ {:else}
50
+ <div>{value}</div>
51
+ {/if}
52
+ {/if}
53
+
54
+ <style>
55
+ .container {
56
+ flex: none;
57
+ max-width: none;
58
+ }
59
+ .container :global(video) {
60
+ width: var(--size-full);
61
+ height: var(--size-full);
62
+ object-fit: cover;
63
+ }
64
+
65
+ .container:hover,
66
+ .container.selected {
67
+ border-color: var(--border-color-accent);
68
+ }
69
+ .container.table {
70
+ margin: 0 auto;
71
+ border: 2px solid var(--border-color-primary);
72
+ border-radius: var(--radius-lg);
73
+ overflow: hidden;
74
+ width: var(--size-20);
75
+ height: var(--size-20);
76
+ object-fit: cover;
77
+ }
78
+
79
+ .container.gallery {
80
+ height: var(--size-20);
81
+ max-height: var(--size-20);
82
+ object-fit: cover;
83
+ }
84
+ </style>
6.8.1/video/Index.svelte ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script lang="ts">
4
+ import { tick } from "svelte";
5
+ import type { FileData } from "@gradio/client";
6
+ import { Block, UploadText } from "@gradio/atoms";
7
+ import StaticVideo from "./shared/VideoPreview.svelte";
8
+ import Video from "./shared/InteractiveVideo.svelte";
9
+ import { StatusTracker } from "@gradio/statustracker";
10
+ import { Gradio } from "@gradio/utils";
11
+ import type { VideoProps, VideoEvents } from "./types";
12
+
13
+ const props = $props();
14
+
15
+ let upload_promise = $state<Promise<any>>();
16
+
17
+ class VideoGradio extends Gradio<VideoEvents, VideoProps> {
18
+ async get_data() {
19
+ if (upload_promise) {
20
+ await upload_promise;
21
+ await tick();
22
+ }
23
+ const data = await super.get_data();
24
+
25
+ return data;
26
+ }
27
+ }
28
+
29
+ const gradio = new VideoGradio(props);
30
+ let old_value = $state(gradio.props.value);
31
+
32
+ let uploading = $state(false);
33
+ let dragging = $state(false);
34
+ let active_source = $derived.by(() =>
35
+ gradio.props.sources ? gradio.props.sources[0] : undefined
36
+ );
37
+ let initial_value: FileData | null = gradio.props.value;
38
+
39
+ $effect(() => {
40
+ if (old_value != gradio.props.value) {
41
+ old_value = gradio.props.value;
42
+ gradio.dispatch("change");
43
+ }
44
+ });
45
+
46
+ const handle_reset_value = (): void => {
47
+ if (initial_value === null || gradio.props.value === initial_value) {
48
+ return;
49
+ }
50
+ gradio.props.value = initial_value;
51
+ };
52
+
53
+ function handle_change(detail: FileData | null): void {
54
+ if (detail != null) {
55
+ gradio.props.value = detail as FileData;
56
+ } else {
57
+ gradio.props.value = null;
58
+ }
59
+ }
60
+
61
+ function handle_error(detail: string): void {
62
+ const [level, status] = detail.includes("Invalid file type")
63
+ ? ["warning", "complete"]
64
+ : ["error", "error"];
65
+ gradio.shared.loading_status.status = status as any;
66
+ gradio.shared.loading_status.message = detail;
67
+ gradio.dispatch(level as "error" | "warning", detail);
68
+ }
69
+ </script>
70
+
71
+ {#if !gradio.shared.interactive}
72
+ <Block
73
+ visible={gradio.shared.visible}
74
+ variant={gradio.props.value === null && active_source === "upload"
75
+ ? "dashed"
76
+ : "solid"}
77
+ border_mode={dragging ? "focus" : "base"}
78
+ padding={false}
79
+ elem_id={gradio.shared.elem_id}
80
+ elem_classes={gradio.shared.elem_classes}
81
+ height={gradio.props.height || undefined}
82
+ width={gradio.props.width}
83
+ container={gradio.shared.container}
84
+ scale={gradio.shared.scale}
85
+ min_width={gradio.shared.min_width}
86
+ allow_overflow={false}
87
+ >
88
+ <StatusTracker
89
+ autoscroll={gradio.shared.autoscroll}
90
+ i18n={gradio.i18n}
91
+ {...gradio.shared.loading_status}
92
+ on_clear_status={() =>
93
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
94
+ />
95
+
96
+ <StaticVideo
97
+ value={gradio.props.value}
98
+ subtitle={gradio.props.subtitles}
99
+ label={gradio.shared.label}
100
+ show_label={gradio.shared.show_label}
101
+ autoplay={gradio.props.autoplay}
102
+ loop={gradio.props.loop}
103
+ buttons={gradio.props.buttons ?? ["download", "share"]}
104
+ on_custom_button_click={(id) => {
105
+ gradio.dispatch("custom_button_click", { id });
106
+ }}
107
+ bind:playback_position={gradio.props.playback_position}
108
+ onplay={() => gradio.dispatch("play")}
109
+ onpause={() => gradio.dispatch("pause")}
110
+ onstop={() => gradio.dispatch("stop")}
111
+ onend={() => gradio.dispatch("end")}
112
+ onshare={(detail) => gradio.dispatch("share", detail)}
113
+ onerror={(detail) => gradio.dispatch("error", detail)}
114
+ i18n={gradio.i18n}
115
+ upload={(...args) => gradio.shared.client.upload(...args)}
116
+ />
117
+ </Block>
118
+ {:else}
119
+ <Block
120
+ visible={gradio.shared.visible}
121
+ variant={gradio.props.value === null && active_source === "upload"
122
+ ? "dashed"
123
+ : "solid"}
124
+ border_mode={dragging ? "focus" : "base"}
125
+ padding={false}
126
+ elem_id={gradio.shared.elem_id}
127
+ elem_classes={gradio.shared.elem_classes}
128
+ height={gradio.props.height || undefined}
129
+ width={gradio.props.width}
130
+ container={gradio.shared.container}
131
+ scale={gradio.shared.scale}
132
+ min_width={gradio.shared.min_width}
133
+ allow_overflow={false}
134
+ >
135
+ <StatusTracker
136
+ autoscroll={gradio.shared.autoscroll}
137
+ i18n={gradio.i18n}
138
+ {...gradio.shared.loading_status}
139
+ on_clear_status={() =>
140
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
141
+ />
142
+
143
+ <Video
144
+ bind:upload_promise
145
+ value={gradio.props.value}
146
+ subtitle={gradio.props.subtitles}
147
+ onchange={handle_change}
148
+ ondrag={(detail) => (dragging = detail)}
149
+ onerror={handle_error}
150
+ bind:uploading
151
+ label={gradio.shared.label}
152
+ show_label={gradio.shared.show_label}
153
+ buttons={gradio.props.buttons ?? ["download", "share"]}
154
+ on_custom_button_click={(id) => {
155
+ gradio.dispatch("custom_button_click", { id });
156
+ }}
157
+ sources={gradio.props.sources}
158
+ {active_source}
159
+ webcam_options={gradio.props.webcam_options}
160
+ include_audio={gradio.props.include_audio}
161
+ autoplay={gradio.props.autoplay}
162
+ root={gradio.shared.root}
163
+ loop={gradio.props.loop}
164
+ {handle_reset_value}
165
+ bind:playback_position={gradio.props.playback_position}
166
+ onclear={() => {
167
+ gradio.props.value = null;
168
+ gradio.dispatch("clear");
169
+ gradio.dispatch("input");
170
+ }}
171
+ onplay={() => gradio.dispatch("play")}
172
+ onpause={() => gradio.dispatch("pause")}
173
+ onupload={() => {
174
+ gradio.dispatch("upload");
175
+ gradio.dispatch("input");
176
+ }}
177
+ onstop={() => gradio.dispatch("stop")}
178
+ onend={() => gradio.dispatch("end")}
179
+ onstart_recording={() => gradio.dispatch("start_recording")}
180
+ onstop_recording={() => gradio.dispatch("stop_recording")}
181
+ i18n={gradio.i18n}
182
+ max_file_size={gradio.shared.max_file_size}
183
+ upload={(...args) => gradio.shared.client.upload(...args)}
184
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
185
+ >
186
+ <UploadText i18n={gradio.i18n} type="video" />
187
+ </Video>
188
+ </Block>
189
+ {/if}
6.8.1/video/index.ts ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ export { default as BaseInteractiveVideo } from "./shared/InteractiveVideo.svelte";
2
+ export { default as BaseStaticVideo } from "./shared/VideoPreview.svelte";
3
+ export { default as BasePlayer } from "./shared/Player.svelte";
4
+ export { prettyBytes, playable, loaded } from "./shared/utils";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ import { default as Index } from "./Index.svelte";
7
+ export default Index;
6.8.1/video/package.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/video",
3
+ "version": "0.20.3",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@ffmpeg/ffmpeg": "^0.12.15",
11
+ "@ffmpeg/util": "^0.12.2",
12
+ "@gradio/atoms": "workspace:^",
13
+ "@gradio/client": "workspace:^",
14
+ "@gradio/icons": "workspace:^",
15
+ "@gradio/image": "workspace:^",
16
+ "@gradio/statustracker": "workspace:^",
17
+ "@gradio/upload": "workspace:^",
18
+ "@gradio/utils": "workspace:^",
19
+ "hls.js": "^1.6.13",
20
+ "mrmime": "^2.0.1"
21
+ },
22
+ "devDependencies": {
23
+ "@gradio/preview": "workspace:^"
24
+ },
25
+ "exports": {
26
+ "./package.json": "./package.json",
27
+ ".": {
28
+ "gradio": "./index.ts",
29
+ "svelte": "./dist/index.js",
30
+ "types": "./dist/index.d.ts"
31
+ },
32
+ "./example": {
33
+ "gradio": "./Example.svelte",
34
+ "svelte": "./dist/Example.svelte",
35
+ "types": "./dist/Example.svelte.d.ts"
36
+ },
37
+ "./shared": {
38
+ "gradio": "./shared/index.ts",
39
+ "svelte": "./dist/shared/index.js",
40
+ "types": "./dist/shared/index.d.ts"
41
+ },
42
+ "./base": {
43
+ "gradio": "./shared/VideoPreview.svelte",
44
+ "svelte": "./dist/shared/VideoPreview.svelte",
45
+ "types": "./dist/shared/VideoPreview.svelte.d.ts"
46
+ }
47
+ },
48
+ "peerDependencies": {
49
+ "svelte": "^5.48.0"
50
+ },
51
+ "main": "index.ts",
52
+ "main_changeset": true,
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/gradio-app/gradio.git",
56
+ "directory": "js/video"
57
+ }
58
+ }
6.8.1/video/shared/InteractiveVideo.svelte ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Upload } from "@gradio/upload";
3
+ import type { FileData, Client } from "@gradio/client";
4
+ import { BlockLabel } from "@gradio/atoms";
5
+ import { Webcam } from "@gradio/image";
6
+ import { Video } from "@gradio/icons";
7
+ import type { WebcamOptions } from "./utils";
8
+ import { prettyBytes, playable } from "./utils";
9
+ import Player from "./Player.svelte";
10
+ import type { I18nFormatter } from "@gradio/utils";
11
+ import { SelectSource } from "@gradio/atoms";
12
+ import type { Snippet } from "svelte";
13
+
14
+ interface Props {
15
+ value?: FileData | null;
16
+ subtitle?: FileData | null;
17
+ sources?:
18
+ | ["webcam"]
19
+ | ["upload"]
20
+ | ["webcam", "upload"]
21
+ | ["upload", "webcam"];
22
+ label?: string;
23
+ show_download_button?: boolean;
24
+ show_label?: boolean;
25
+ webcam_options: WebcamOptions;
26
+ include_audio: boolean;
27
+ autoplay: boolean;
28
+ root: string;
29
+ i18n: I18nFormatter;
30
+ active_source?: "webcam" | "upload";
31
+ handle_reset_value?: () => void;
32
+ max_file_size?: number | null;
33
+ upload: Client["upload"];
34
+ stream_handler: Client["stream"];
35
+ loop: boolean;
36
+ uploading?: boolean;
37
+ upload_promise?: Promise<any> | null;
38
+ playback_position?: number;
39
+ buttons?: (string | CustomButtonType)[] | null;
40
+ on_custom_button_click?: ((id: number) => void) | null;
41
+ onchange?: (value: FileData | null) => void;
42
+ onclear?: () => void;
43
+ onplay?: () => void;
44
+ onpause?: () => void;
45
+ onend?: () => void;
46
+ ondrag?: (dragging: boolean) => void;
47
+ onerror?: (error: string) => void;
48
+ onupload?: (value: FileData) => void;
49
+ onstart_recording?: () => void;
50
+ onstop_recording?: () => void;
51
+ onstop?: () => void;
52
+ children?: Snippet;
53
+ }
54
+
55
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
56
+
57
+ let {
58
+ value = $bindable(null),
59
+ subtitle = null,
60
+ sources = ["webcam", "upload"],
61
+ label = undefined,
62
+ show_download_button = false,
63
+ show_label = true,
64
+ webcam_options,
65
+ include_audio,
66
+ autoplay,
67
+ root,
68
+ i18n,
69
+ active_source: initial_active_source = "webcam",
70
+ handle_reset_value = () => {},
71
+ max_file_size = null,
72
+ upload,
73
+ stream_handler,
74
+ loop,
75
+ uploading = $bindable(),
76
+ upload_promise = $bindable(),
77
+ playback_position = $bindable(),
78
+ buttons = null,
79
+ on_custom_button_click = null,
80
+ onchange,
81
+ onclear,
82
+ onplay,
83
+ onpause,
84
+ onend,
85
+ ondrag,
86
+ onerror,
87
+ onupload,
88
+ onstart_recording,
89
+ onstop_recording,
90
+ onstop,
91
+ children
92
+ }: Props = $props();
93
+
94
+ let has_change_history = $state(false);
95
+ let active_source = $derived.by(() => {
96
+ return initial_active_source ?? "webcam";
97
+ });
98
+
99
+ function handle_load(detail: FileData | null): void {
100
+ value = detail;
101
+ onchange?.(detail);
102
+ if (detail) {
103
+ onupload?.(detail);
104
+ }
105
+ }
106
+
107
+ function handle_clear(): void {
108
+ value = null;
109
+ onchange?.(null);
110
+ onclear?.();
111
+ }
112
+
113
+ function handle_change(video: FileData): void {
114
+ has_change_history = true;
115
+ onchange?.(video);
116
+ }
117
+
118
+ function handle_capture({
119
+ detail
120
+ }: CustomEvent<FileData | any | null>): void {
121
+ onchange?.(detail);
122
+ }
123
+
124
+ let dragging = $state(false);
125
+
126
+ $effect(() => {
127
+ ondrag?.(dragging);
128
+ });
129
+ </script>
130
+
131
+ <BlockLabel {show_label} Icon={Video} label={label || "Video"} />
132
+ <div data-testid="video" class="video-container">
133
+ {#if value === null || value?.url === undefined}
134
+ <div class="upload-container">
135
+ {#if active_source === "upload"}
136
+ <Upload
137
+ bind:upload_promise
138
+ bind:dragging
139
+ bind:uploading
140
+ filetype="video/x-m4v,video/*"
141
+ onload={handle_load}
142
+ {max_file_size}
143
+ onerror={(detail) => onerror?.(detail)}
144
+ {root}
145
+ {upload}
146
+ {stream_handler}
147
+ aria_label={i18n("video.drop_to_upload")}
148
+ >
149
+ {#if children}
150
+ {@render children()}
151
+ {/if}
152
+ </Upload>
153
+ {:else if active_source === "webcam"}
154
+ <Webcam
155
+ {root}
156
+ mirror_webcam={webcam_options.mirror}
157
+ webcam_constraints={webcam_options.constraints}
158
+ {include_audio}
159
+ mode="video"
160
+ on:error={({ detail }) => onerror?.(detail)}
161
+ on:capture={handle_capture}
162
+ on:start_recording={() => onstart_recording?.()}
163
+ on:stop_recording={() => onstop_recording?.()}
164
+ {i18n}
165
+ {upload}
166
+ stream_every={1}
167
+ />
168
+ {/if}
169
+ </div>
170
+ {:else if value?.url}
171
+ {#key value?.url}
172
+ <Player
173
+ {upload}
174
+ {root}
175
+ interactive
176
+ {autoplay}
177
+ src={value.url}
178
+ subtitle={subtitle?.url}
179
+ is_stream={false}
180
+ onplay={() => onplay?.()}
181
+ onpause={() => onpause?.()}
182
+ onstop={() => onstop?.()}
183
+ onend={() => onend?.()}
184
+ onerror={(error) => onerror?.(error)}
185
+ mirror={webcam_options.mirror && active_source === "webcam"}
186
+ {label}
187
+ {handle_change}
188
+ {handle_reset_value}
189
+ {loop}
190
+ {value}
191
+ {i18n}
192
+ {show_download_button}
193
+ {handle_clear}
194
+ {has_change_history}
195
+ bind:playback_position
196
+ />
197
+ {/key}
198
+ {:else if value.size}
199
+ <div class="file-name">{value.orig_name || value.url}</div>
200
+ <div class="file-size">
201
+ {prettyBytes(value.size)}
202
+ </div>
203
+ {/if}
204
+
205
+ <SelectSource {sources} bind:active_source {handle_clear} />
206
+ </div>
207
+
208
+ <style>
209
+ .file-name {
210
+ padding: var(--size-6);
211
+ font-size: var(--text-xxl);
212
+ word-break: break-all;
213
+ }
214
+
215
+ .file-size {
216
+ padding: var(--size-2);
217
+ font-size: var(--text-xl);
218
+ }
219
+
220
+ .upload-container {
221
+ height: 100%;
222
+ width: 100%;
223
+ }
224
+
225
+ .video-container {
226
+ display: flex;
227
+ height: 100%;
228
+ flex-direction: column;
229
+ justify-content: center;
230
+ align-items: center;
231
+ }
232
+ </style>
6.8.1/video/shared/Player.svelte ADDED
@@ -0,0 +1,433 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, onDestroy } from "svelte";
3
+ import { Play, Pause, Maximize, Undo } from "@gradio/icons";
4
+ import Video from "./Video.svelte";
5
+ import VideoControls from "./VideoControls.svelte";
6
+ import VolumeControl from "./VolumeControl.svelte";
7
+ import VolumeLevels from "../../audio/shared/VolumeLevels.svelte";
8
+ import type { FileData, Client } from "@gradio/client";
9
+ import { prepare_files } from "@gradio/client";
10
+ import { format_time } from "@gradio/utils";
11
+ import type { I18nFormatter } from "@gradio/utils";
12
+
13
+ interface Props {
14
+ root?: string;
15
+ src: string;
16
+ subtitle?: string | null;
17
+ mirror: boolean;
18
+ autoplay: boolean;
19
+ loop: boolean;
20
+ label?: string;
21
+ interactive?: boolean;
22
+ handle_change?: (video: FileData) => void;
23
+ handle_reset_value?: () => void;
24
+ upload: Client["upload"];
25
+ is_stream?: boolean;
26
+ i18n: I18nFormatter;
27
+ show_download_button?: boolean;
28
+ value?: FileData | null;
29
+ handle_clear?: () => void;
30
+ has_change_history?: boolean;
31
+ playback_position?: number;
32
+ onplay?: () => void;
33
+ onpause?: () => void;
34
+ onstop?: () => void;
35
+ onend?: () => void;
36
+ onerror?: (error: string) => void;
37
+ onloadstart?: () => void;
38
+ onloadeddata?: () => void;
39
+ onloadedmetadata?: () => void;
40
+ }
41
+
42
+ let {
43
+ root = "",
44
+ src,
45
+ subtitle = null,
46
+ mirror,
47
+ autoplay,
48
+ loop,
49
+ label = "test",
50
+ interactive = false,
51
+ handle_change = () => {},
52
+ handle_reset_value = () => {},
53
+ upload,
54
+ is_stream = undefined,
55
+ i18n,
56
+ show_download_button = false,
57
+ value = null,
58
+ handle_clear = () => {},
59
+ has_change_history = false,
60
+ playback_position = $bindable(),
61
+ onplay,
62
+ onpause,
63
+ onstop,
64
+ onend,
65
+ onerror,
66
+ onloadstart,
67
+ onloadeddata,
68
+ onloadedmetadata
69
+ }: Props = $props();
70
+
71
+ let time = $state(0);
72
+ let duration = $state<number>(0);
73
+ let paused = $state(true);
74
+ let video = $state<HTMLVideoElement>();
75
+ let processingVideo = $state(false);
76
+ let show_volume_slider = $state(false);
77
+ let current_volume = $state(1);
78
+ let is_fullscreen = $state(false);
79
+
80
+ function handleMove(e: TouchEvent | MouseEvent): void {
81
+ if (!duration) return;
82
+
83
+ if (e.type === "click") {
84
+ handle_click(e as MouseEvent);
85
+ return;
86
+ }
87
+
88
+ if (e.type !== "touchmove" && !((e as MouseEvent).buttons & 1)) return;
89
+
90
+ const clientX =
91
+ e.type === "touchmove"
92
+ ? (e as TouchEvent).touches[0].clientX
93
+ : (e as MouseEvent).clientX;
94
+ const { left, right } = (
95
+ e.currentTarget as HTMLProgressElement
96
+ ).getBoundingClientRect();
97
+ time = (duration * (clientX - left)) / (right - left);
98
+ }
99
+
100
+ async function play_pause(): Promise<void> {
101
+ if (!video) return;
102
+ if (document.fullscreenElement != video) {
103
+ const isPlaying =
104
+ video.currentTime > 0 &&
105
+ !video.paused &&
106
+ !video.ended &&
107
+ video.readyState > video.HAVE_CURRENT_DATA;
108
+
109
+ if (!isPlaying) {
110
+ await video.play();
111
+ } else video.pause();
112
+ }
113
+ }
114
+
115
+ function handle_click(e: MouseEvent): void {
116
+ if (!duration) return;
117
+ const { left, right } = (
118
+ e.currentTarget as HTMLProgressElement
119
+ ).getBoundingClientRect();
120
+ time = (duration * (e.clientX - left)) / (right - left);
121
+ }
122
+
123
+ function handle_end(): void {
124
+ onstop?.();
125
+ onend?.();
126
+ }
127
+
128
+ const handle_trim_video = async (videoBlob: Blob): Promise<void> => {
129
+ let _video_blob = new File([videoBlob], "video.mp4");
130
+ const val = await prepare_files([_video_blob]);
131
+ let value = ((await upload(val, root))?.filter(Boolean) as FileData[])[0];
132
+
133
+ handle_change(value);
134
+ };
135
+
136
+ function open_full_screen(): void {
137
+ if (!video) return;
138
+ if (!is_fullscreen) {
139
+ video.requestFullscreen();
140
+ } else {
141
+ document.exitFullscreen();
142
+ }
143
+ }
144
+
145
+ function handleFullscreenChange(): void {
146
+ is_fullscreen = document.fullscreenElement === video;
147
+ if (video) {
148
+ video.controls = is_fullscreen;
149
+ }
150
+ }
151
+
152
+ let last_synced_volume = 1;
153
+ let previous_video: HTMLVideoElement | undefined;
154
+ // Tolerance for floating-point comparison of volume values
155
+ const VOLUME_EPSILON = 0.001;
156
+
157
+ function handleVolumeChange(): void {
158
+ if (video && Math.abs(video.volume - last_synced_volume) > VOLUME_EPSILON) {
159
+ current_volume = video.volume;
160
+ last_synced_volume = video.volume;
161
+ }
162
+ }
163
+
164
+ onMount(() => {
165
+ document.addEventListener("fullscreenchange", handleFullscreenChange);
166
+ return () => {
167
+ document.removeEventListener("fullscreenchange", handleFullscreenChange);
168
+ };
169
+ });
170
+
171
+ onDestroy(() => {
172
+ if (video) {
173
+ video.removeEventListener("volumechange", handleVolumeChange);
174
+ }
175
+ });
176
+
177
+ $effect(() => {
178
+ if (video && video !== previous_video) {
179
+ if (previous_video) {
180
+ previous_video.removeEventListener("volumechange", handleVolumeChange);
181
+ }
182
+ video.addEventListener("volumechange", handleVolumeChange);
183
+ previous_video = video;
184
+ }
185
+ });
186
+
187
+ $effect(() => {
188
+ playback_position = time;
189
+ });
190
+
191
+ $effect(() => {
192
+ if (playback_position !== time && video) {
193
+ video.currentTime = playback_position;
194
+ }
195
+ });
196
+
197
+ $effect(() => {
198
+ if (video && !is_fullscreen) {
199
+ if (Math.abs(video.volume - current_volume) > VOLUME_EPSILON) {
200
+ video.volume = current_volume;
201
+ last_synced_volume = current_volume;
202
+ }
203
+ video.controls = false;
204
+ }
205
+ });
206
+
207
+ $effect(() => {
208
+ if (video && is_fullscreen) {
209
+ last_synced_volume = video.volume;
210
+ }
211
+ });
212
+ </script>
213
+
214
+ <div class="wrap">
215
+ <div class="mirror-wrap" class:mirror>
216
+ <Video
217
+ {src}
218
+ preload="auto"
219
+ {autoplay}
220
+ {loop}
221
+ {is_stream}
222
+ controls={is_fullscreen}
223
+ onclick={play_pause}
224
+ onplay={() => onplay?.()}
225
+ onpause={() => onpause?.()}
226
+ onerror={(error) => onerror?.(error)}
227
+ onended={handle_end}
228
+ bind:currentTime={time}
229
+ bind:duration
230
+ bind:paused
231
+ bind:node={video}
232
+ data-testid={`${label}-player`}
233
+ {processingVideo}
234
+ onloadstart={() => onloadstart?.()}
235
+ onloadeddata={() => onloadeddata?.()}
236
+ onloadedmetadata={() => onloadedmetadata?.()}
237
+ >
238
+ <track kind="captions" src={subtitle} default />
239
+ </Video>
240
+ </div>
241
+
242
+ <div class="controls">
243
+ <div class="inner">
244
+ <span
245
+ role="button"
246
+ tabindex="0"
247
+ class="icon"
248
+ aria-label="play-pause-replay-button"
249
+ onclick={play_pause}
250
+ onkeydown={play_pause}
251
+ >
252
+ {#if time === duration}
253
+ <Undo />
254
+ {:else if paused}
255
+ <Play />
256
+ {:else}
257
+ <Pause />
258
+ {/if}
259
+ </span>
260
+
261
+ <span class="time">{format_time(time)} / {format_time(duration)}</span>
262
+
263
+ <!-- TODO: implement accessible video timeline for 4.0 -->
264
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
265
+ <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
266
+ <progress
267
+ value={time / duration || 0}
268
+ onmousemove={handleMove}
269
+ ontouchmove={(e) => {
270
+ e.preventDefault();
271
+ handleMove(e);
272
+ }}
273
+ onclick={(e) => {
274
+ e.stopPropagation();
275
+ e.preventDefault();
276
+ handle_click(e);
277
+ }}
278
+ ></progress>
279
+
280
+ <div class="volume-control-wrapper">
281
+ <button
282
+ class="icon volume-button"
283
+ style:color={show_volume_slider ? "var(--color-accent)" : "white"}
284
+ aria-label="Adjust volume"
285
+ onclick={() => (show_volume_slider = !show_volume_slider)}
286
+ >
287
+ <VolumeLevels currentVolume={current_volume} />
288
+ </button>
289
+
290
+ {#if show_volume_slider}
291
+ <VolumeControl bind:current_volume bind:show_volume_slider />
292
+ {/if}
293
+ </div>
294
+
295
+ {#if !show_volume_slider}
296
+ <div
297
+ role="button"
298
+ tabindex="0"
299
+ class="icon"
300
+ aria-label="full-screen"
301
+ onclick={open_full_screen}
302
+ onkeypress={open_full_screen}
303
+ >
304
+ <Maximize />
305
+ </div>
306
+ {/if}
307
+ </div>
308
+ </div>
309
+ </div>
310
+ {#if interactive}
311
+ <VideoControls
312
+ videoElement={video}
313
+ showRedo
314
+ {handle_trim_video}
315
+ {handle_reset_value}
316
+ bind:processingVideo
317
+ {value}
318
+ {i18n}
319
+ {show_download_button}
320
+ {handle_clear}
321
+ {has_change_history}
322
+ />
323
+ {/if}
324
+
325
+ <style lang="postcss">
326
+ span {
327
+ text-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
328
+ }
329
+
330
+ progress {
331
+ margin-right: var(--size-3);
332
+ border-radius: var(--radius-sm);
333
+ width: var(--size-full);
334
+ height: var(--size-2);
335
+ }
336
+
337
+ progress::-webkit-progress-bar {
338
+ border-radius: 2px;
339
+ background-color: rgba(255, 255, 255, 0.2);
340
+ overflow: hidden;
341
+ }
342
+
343
+ progress::-webkit-progress-value {
344
+ background-color: rgba(255, 255, 255, 0.9);
345
+ }
346
+
347
+ .mirror {
348
+ transform: scaleX(-1);
349
+ }
350
+
351
+ .mirror-wrap {
352
+ position: relative;
353
+ height: 100%;
354
+ width: 100%;
355
+ }
356
+
357
+ .controls {
358
+ position: absolute;
359
+ bottom: 0;
360
+ opacity: 0;
361
+ transition: 500ms;
362
+ margin: var(--size-2);
363
+ border-radius: var(--radius-md);
364
+ background: var(--color-grey-800);
365
+ padding: var(--size-2) var(--size-1);
366
+ width: calc(100% - 0.375rem * 2);
367
+ width: calc(100% - var(--size-2) * 2);
368
+ z-index: 10;
369
+ }
370
+ .wrap:hover .controls {
371
+ opacity: 1;
372
+ }
373
+ :global(:fullscreen) .controls {
374
+ display: none;
375
+ }
376
+
377
+ .inner {
378
+ display: flex;
379
+ justify-content: space-between;
380
+ align-items: center;
381
+ padding-right: var(--size-2);
382
+ padding-left: var(--size-2);
383
+ width: var(--size-full);
384
+ height: var(--size-full);
385
+ }
386
+
387
+ .icon {
388
+ display: flex;
389
+ justify-content: center;
390
+ cursor: pointer;
391
+ width: var(--size-6);
392
+ color: white;
393
+ }
394
+
395
+ .volume-control-wrapper {
396
+ position: relative;
397
+ display: flex;
398
+ align-items: center;
399
+ margin-right: var(--spacing-md);
400
+ }
401
+
402
+ .volume-button {
403
+ display: flex;
404
+ justify-content: center;
405
+ align-items: center;
406
+ cursor: pointer;
407
+ width: var(--size-6);
408
+ color: white;
409
+ border: none;
410
+ background: none;
411
+ padding: 0;
412
+ }
413
+
414
+ .time {
415
+ flex-shrink: 0;
416
+ margin-right: var(--size-3);
417
+ margin-left: var(--size-3);
418
+ color: white;
419
+ font-size: var(--text-sm);
420
+ font-family: var(--font-mono);
421
+ }
422
+ .wrap {
423
+ position: relative;
424
+ background-color: var(--background-fill-secondary);
425
+ height: var(--size-full);
426
+ width: var(--size-full);
427
+ border-radius: var(--radius-xl);
428
+ }
429
+ .wrap :global(video) {
430
+ height: var(--size-full);
431
+ width: var(--size-full);
432
+ }
433
+ </style>
6.8.1/video/shared/Video.svelte ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { HTMLVideoAttributes } from "svelte/elements";
3
+ import { loaded } from "./utils";
4
+ import type { Snippet } from "svelte";
5
+
6
+ import Hls from "hls.js";
7
+
8
+ interface Props {
9
+ src?: HTMLVideoAttributes["src"];
10
+ muted?: HTMLVideoAttributes["muted"];
11
+ playsinline?: HTMLVideoAttributes["playsinline"];
12
+ preload?: HTMLVideoAttributes["preload"];
13
+ autoplay?: HTMLVideoAttributes["autoplay"];
14
+ controls?: HTMLVideoAttributes["controls"];
15
+ currentTime?: number;
16
+ duration?: number;
17
+ paused?: boolean;
18
+ node?: HTMLVideoElement;
19
+ loop: boolean;
20
+ is_stream: boolean;
21
+ processingVideo?: boolean;
22
+ onloadeddata?: () => void;
23
+ onclick?: () => void;
24
+ onplay?: () => void;
25
+ onpause?: () => void;
26
+ onended?: () => void;
27
+ onmouseover?: () => void;
28
+ onmouseout?: () => void;
29
+ onfocus?: () => void;
30
+ onblur?: () => void;
31
+ onerror?: (error: string) => void;
32
+ onloadstart?: () => void;
33
+ onloadedmetadata?: () => void;
34
+ "data-testid"?: string;
35
+ children?: Snippet;
36
+ }
37
+
38
+ let {
39
+ src = undefined,
40
+ muted = undefined,
41
+ playsinline = undefined,
42
+ preload = undefined,
43
+ autoplay = undefined,
44
+ controls = undefined,
45
+ currentTime = $bindable(undefined),
46
+ duration = $bindable(undefined),
47
+ paused = $bindable(undefined),
48
+ node = $bindable(undefined),
49
+ loop,
50
+ is_stream,
51
+ processingVideo = false,
52
+ onloadeddata,
53
+ onclick,
54
+ onplay,
55
+ onpause,
56
+ onended,
57
+ onmouseover,
58
+ onmouseout,
59
+ onfocus,
60
+ onblur,
61
+ onerror,
62
+ onloadstart,
63
+ onloadedmetadata,
64
+ "data-testid": dataTestid,
65
+ children
66
+ }: Props = $props();
67
+
68
+ let stream_active = $state(false);
69
+
70
+ function load_stream(
71
+ src: string | null | undefined,
72
+ is_stream: boolean,
73
+ node: HTMLVideoElement
74
+ ): void {
75
+ if (!src || !is_stream) return;
76
+
77
+ if (Hls.isSupported() && !stream_active) {
78
+ const hls = new Hls({
79
+ maxBufferLength: 1, // 0.5 seconds (500 ms)
80
+ maxMaxBufferLength: 1, // Maximum max buffer length in seconds
81
+ lowLatencyMode: true // Enable low latency mode
82
+ });
83
+ hls.loadSource(src);
84
+ hls.attachMedia(node);
85
+ hls.on(Hls.Events.MANIFEST_PARSED, function () {
86
+ (node as HTMLVideoElement).play();
87
+ });
88
+ hls.on(Hls.Events.ERROR, function (event, data) {
89
+ console.error("HLS error:", event, data);
90
+ if (data.fatal) {
91
+ switch (data.type) {
92
+ case Hls.ErrorTypes.NETWORK_ERROR:
93
+ console.error(
94
+ "Fatal network error encountered, trying to recover"
95
+ );
96
+ hls.startLoad();
97
+ break;
98
+ case Hls.ErrorTypes.MEDIA_ERROR:
99
+ console.error("Fatal media error encountered, trying to recover");
100
+ hls.recoverMediaError();
101
+ break;
102
+ default:
103
+ console.error("Fatal error, cannot recover");
104
+ hls.destroy();
105
+ break;
106
+ }
107
+ }
108
+ });
109
+ stream_active = true;
110
+ }
111
+ }
112
+
113
+ $effect(() => {
114
+ src;
115
+ stream_active = false;
116
+ });
117
+
118
+ $effect(() => {
119
+ if (node && src && is_stream) {
120
+ load_stream(src, is_stream, node);
121
+ }
122
+ });
123
+ </script>
124
+
125
+ <!--
126
+ The spread operator with `$$props` or `$$restProps` can't be used here
127
+ to pass props from the parent component to the <video> element
128
+ because of its unexpected behavior: https://github.com/sveltejs/svelte/issues/7404
129
+ For example, if we add {...$$props} or {...$$restProps}, the boolean props aside it like `controls` will be compiled as string "true" or "false" on the actual DOM.
130
+ Then, even when `controls` is false, the compiled DOM would be `<video controls="false">` which is equivalent to `<video controls>` since the string "false" is even truthy.
131
+ -->
132
+ <div class:hidden={!processingVideo} class="overlay">
133
+ <span class="load-wrap">
134
+ <span class="loader" />
135
+ </span>
136
+ </div>
137
+ <video
138
+ {src}
139
+ {muted}
140
+ {playsinline}
141
+ {preload}
142
+ {autoplay}
143
+ {controls}
144
+ {loop}
145
+ onloadeddata={() => onloadeddata?.()}
146
+ onclick={() => onclick?.()}
147
+ onplay={() => onplay?.()}
148
+ onpause={() => onpause?.()}
149
+ onended={() => onended?.()}
150
+ onmouseover={() => onmouseover?.()}
151
+ onmouseout={() => onmouseout?.()}
152
+ onfocus={() => onfocus?.()}
153
+ onblur={() => onblur?.()}
154
+ onerror={() => onerror?.("Video not playable")}
155
+ onloadstart={() => onloadstart?.()}
156
+ onloadedmetadata={() => onloadedmetadata?.()}
157
+ bind:currentTime
158
+ bind:duration
159
+ bind:paused
160
+ bind:this={node}
161
+ use:loaded={{ autoplay: autoplay ?? false }}
162
+ data-testid={dataTestid}
163
+ crossorigin="anonymous"
164
+ >
165
+ {#if children}
166
+ {@render children()}
167
+ {/if}
168
+ </video>
169
+
170
+ <style>
171
+ .overlay {
172
+ position: absolute;
173
+ background-color: rgba(0, 0, 0, 0.4);
174
+ width: 100%;
175
+ height: 100%;
176
+ }
177
+
178
+ .hidden {
179
+ display: none;
180
+ }
181
+
182
+ .load-wrap {
183
+ display: flex;
184
+ justify-content: center;
185
+ align-items: center;
186
+ height: 100%;
187
+ }
188
+
189
+ .loader {
190
+ display: flex;
191
+ position: relative;
192
+ background-color: var(--border-color-accent-subdued);
193
+ animation: shadowPulse 2s linear infinite;
194
+ box-shadow:
195
+ -24px 0 var(--border-color-accent-subdued),
196
+ 24px 0 var(--border-color-accent-subdued);
197
+ margin: var(--spacing-md);
198
+ border-radius: 50%;
199
+ width: 10px;
200
+ height: 10px;
201
+ scale: 0.5;
202
+ }
203
+
204
+ @keyframes shadowPulse {
205
+ 33% {
206
+ box-shadow:
207
+ -24px 0 var(--border-color-accent-subdued),
208
+ 24px 0 #fff;
209
+ background: #fff;
210
+ }
211
+ 66% {
212
+ box-shadow:
213
+ -24px 0 #fff,
214
+ 24px 0 #fff;
215
+ background: var(--border-color-accent-subdued);
216
+ }
217
+ 100% {
218
+ box-shadow:
219
+ -24px 0 #fff,
220
+ 24px 0 var(--border-color-accent-subdued);
221
+ background: #fff;
222
+ }
223
+ }
224
+ </style>
6.8.1/video/shared/VideoControls.svelte ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Undo, Trim, Clear } from "@gradio/icons";
3
+ import VideoTimeline from "./VideoTimeline.svelte";
4
+ import { trimVideo } from "./utils";
5
+ import { FFmpeg } from "@ffmpeg/ffmpeg";
6
+ import loadFfmpeg from "./utils";
7
+ import { onMount } from "svelte";
8
+ import { format_time } from "@gradio/utils";
9
+ import { IconButton } from "@gradio/atoms";
10
+ import { ModifyUpload } from "@gradio/upload";
11
+ import type { FileData } from "@gradio/client";
12
+
13
+ interface Props {
14
+ videoElement?: HTMLVideoElement;
15
+ showRedo?: boolean;
16
+ interactive?: boolean;
17
+ mode?: string;
18
+ handle_reset_value: () => void;
19
+ handle_trim_video: (videoBlob: Blob) => void;
20
+ processingVideo?: boolean;
21
+ i18n: (key: string) => string;
22
+ value?: FileData | null;
23
+ show_download_button?: boolean;
24
+ handle_clear?: () => void;
25
+ has_change_history?: boolean;
26
+ }
27
+
28
+ let {
29
+ videoElement = undefined,
30
+ showRedo = false,
31
+ interactive = true,
32
+ mode = $bindable(""),
33
+ handle_reset_value,
34
+ handle_trim_video,
35
+ processingVideo = $bindable(false),
36
+ i18n,
37
+ value = null,
38
+ show_download_button = false,
39
+ handle_clear = () => {},
40
+ has_change_history = false
41
+ }: Props = $props();
42
+
43
+ let ffmpeg: FFmpeg;
44
+
45
+ onMount(async () => {
46
+ ffmpeg = await loadFfmpeg();
47
+ });
48
+
49
+ $effect(() => {
50
+ if (mode === "edit" && trimmedDuration === null && videoElement) {
51
+ trimmedDuration = videoElement.duration;
52
+ }
53
+ });
54
+
55
+ let trimmedDuration = $state<number | null>(null);
56
+ let dragStart = $state(0);
57
+ let dragEnd = $state(0);
58
+
59
+ let loadingTimeline = $state(false);
60
+
61
+ const toggleTrimmingMode = (): void => {
62
+ if (mode === "edit") {
63
+ mode = "";
64
+ if (videoElement) {
65
+ trimmedDuration = videoElement.duration;
66
+ }
67
+ } else {
68
+ mode = "edit";
69
+ }
70
+ };
71
+ </script>
72
+
73
+ <div class="container" class:hidden={mode !== "edit"}>
74
+ {#if mode === "edit" && videoElement}
75
+ <div class="timeline-wrapper">
76
+ <VideoTimeline
77
+ {videoElement}
78
+ bind:dragStart
79
+ bind:dragEnd
80
+ bind:trimmedDuration
81
+ bind:loadingTimeline
82
+ />
83
+ </div>
84
+ {/if}
85
+
86
+ <div class="controls" data-testid="waveform-controls">
87
+ {#if mode === "edit" && trimmedDuration !== null}
88
+ <time
89
+ aria-label="duration of selected region in seconds"
90
+ class:hidden={loadingTimeline}>{format_time(trimmedDuration)}</time
91
+ >
92
+ <div class="edit-buttons">
93
+ <button
94
+ class:hidden={loadingTimeline}
95
+ class="text-button"
96
+ onclick={() => {
97
+ if (!videoElement) return;
98
+ mode = "";
99
+ processingVideo = true;
100
+ trimVideo(ffmpeg, dragStart, dragEnd, videoElement)
101
+ .then((videoBlob) => {
102
+ handle_trim_video(videoBlob);
103
+ })
104
+ .then(() => {
105
+ processingVideo = false;
106
+ });
107
+ }}>Trim</button
108
+ >
109
+ <button
110
+ class="text-button"
111
+ class:hidden={loadingTimeline}
112
+ onclick={toggleTrimmingMode}>Cancel</button
113
+ >
114
+ </div>
115
+ {:else}
116
+ <div />
117
+ {/if}
118
+ </div>
119
+ </div>
120
+
121
+ <ModifyUpload
122
+ {i18n}
123
+ onclear={() => handle_clear()}
124
+ download={show_download_button ? value?.url : null}
125
+ >
126
+ {#if showRedo && mode === ""}
127
+ <IconButton
128
+ Icon={Undo}
129
+ label="Reset video to initial value"
130
+ disabled={processingVideo || !has_change_history}
131
+ onclick={() => {
132
+ handle_reset_value();
133
+ mode = "";
134
+ }}
135
+ />
136
+ {/if}
137
+
138
+ {#if interactive && mode === ""}
139
+ <IconButton
140
+ Icon={Trim}
141
+ label="Trim video to selection"
142
+ disabled={processingVideo}
143
+ onclick={toggleTrimmingMode}
144
+ />
145
+ {/if}
146
+ </ModifyUpload>
147
+
148
+ <style>
149
+ .container {
150
+ width: 100%;
151
+ }
152
+ time {
153
+ color: var(--color-accent);
154
+ font-weight: bold;
155
+ padding-left: var(--spacing-xs);
156
+ }
157
+
158
+ .timeline-wrapper {
159
+ display: flex;
160
+ align-items: center;
161
+ justify-content: center;
162
+ width: 100%;
163
+ }
164
+
165
+ .text-button {
166
+ border: 1px solid var(--neutral-400);
167
+ border-radius: var(--radius-sm);
168
+ font-weight: 300;
169
+ font-size: var(--size-3);
170
+ text-align: center;
171
+ color: var(--neutral-400);
172
+ height: var(--size-5);
173
+ font-weight: bold;
174
+ padding: 0 5px;
175
+ margin-left: 5px;
176
+ }
177
+
178
+ .text-button:hover,
179
+ .text-button:focus {
180
+ color: var(--color-accent);
181
+ border-color: var(--color-accent);
182
+ }
183
+
184
+ .controls {
185
+ display: flex;
186
+ justify-content: space-between;
187
+ align-items: center;
188
+ margin: var(--spacing-lg);
189
+ overflow: hidden;
190
+ }
191
+
192
+ .edit-buttons {
193
+ display: flex;
194
+ gap: var(--spacing-sm);
195
+ }
196
+
197
+ @media (max-width: 320px) {
198
+ .controls {
199
+ flex-direction: column;
200
+ align-items: flex-start;
201
+ }
202
+
203
+ .edit-buttons {
204
+ margin-top: var(--spacing-sm);
205
+ }
206
+
207
+ .controls * {
208
+ margin: var(--spacing-sm);
209
+ }
210
+
211
+ .controls .text-button {
212
+ margin-left: 0;
213
+ }
214
+ }
215
+
216
+ .container {
217
+ display: flex;
218
+ flex-direction: column;
219
+ }
220
+
221
+ .hidden {
222
+ display: none;
223
+ }
224
+ </style>
6.8.1/video/shared/VideoPreview.svelte ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+ import {
4
+ BlockLabel,
5
+ Empty,
6
+ IconButton,
7
+ ShareButton,
8
+ IconButtonWrapper,
9
+ DownloadLink
10
+ } from "@gradio/atoms";
11
+ import type { FileData, Client } from "@gradio/client";
12
+ import { Video, Download } from "@gradio/icons";
13
+ import { uploadToHuggingFace } from "@gradio/utils";
14
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
15
+
16
+ import Player from "./Player.svelte";
17
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
18
+
19
+ interface Props {
20
+ value?: FileData | null;
21
+ subtitle?: FileData | null;
22
+ label?: string;
23
+ show_label?: boolean;
24
+ autoplay: boolean;
25
+ buttons?: (string | CustomButtonType)[] | null;
26
+ on_custom_button_click?: ((id: number) => void) | null;
27
+ loop: boolean;
28
+ i18n: I18nFormatter;
29
+ upload: Client["upload"];
30
+ display_icon_button_wrapper_top_corner?: boolean;
31
+ playback_position?: number;
32
+ onplay?: () => void;
33
+ onpause?: () => void;
34
+ onend?: () => void;
35
+ onstop?: () => void;
36
+ onload?: () => void;
37
+ onchange?: (value: FileData) => void;
38
+ onerror?: (error: string) => void;
39
+ onshare?: (detail: unknown) => void;
40
+ }
41
+
42
+ let {
43
+ value = $bindable(null),
44
+ subtitle = null,
45
+ label = undefined,
46
+ show_label = true,
47
+ autoplay,
48
+ buttons = null,
49
+ on_custom_button_click = null,
50
+ loop,
51
+ i18n,
52
+ upload,
53
+ display_icon_button_wrapper_top_corner = false,
54
+ playback_position = $bindable(),
55
+ onplay,
56
+ onpause,
57
+ onend,
58
+ onstop,
59
+ onload,
60
+ onchange,
61
+ onerror,
62
+ onshare
63
+ }: Props = $props();
64
+
65
+ let old_value = $state<FileData | null>(null);
66
+ let old_subtitle = $state<FileData | null>(null);
67
+
68
+ $effect(() => {
69
+ if (value) {
70
+ onchange?.(value);
71
+ }
72
+ });
73
+
74
+ $effect(() => {
75
+ async function updateValue(): Promise<void> {
76
+ // needed to bust subtitle caching issues on Chrome
77
+ if (
78
+ value !== old_value &&
79
+ subtitle !== old_subtitle &&
80
+ old_subtitle !== null
81
+ ) {
82
+ old_value = value;
83
+ value = null;
84
+ await tick();
85
+ value = old_value;
86
+ }
87
+ old_value = value;
88
+ old_subtitle = subtitle;
89
+ }
90
+ updateValue();
91
+ });
92
+ </script>
93
+
94
+ <BlockLabel {show_label} Icon={Video} label={label || "Video"} />
95
+ {#if !value || value.url === undefined}
96
+ <Empty unpadded_box={true} size="large"><Video /></Empty>
97
+ {:else}
98
+ {#key value.url}
99
+ <Player
100
+ src={value.url}
101
+ subtitle={subtitle?.url}
102
+ is_stream={value.is_stream}
103
+ {autoplay}
104
+ onplay={() => onplay?.()}
105
+ onpause={() => onpause?.()}
106
+ onstop={() => onstop?.()}
107
+ onend={() => onend?.()}
108
+ onloadedmetadata={() => {
109
+ // Deal with `<video>`'s `loadedmetadata` event as `VideoPreview`'s `load` event
110
+ // to represent not only the video is loaded but also the metadata is loaded
111
+ // so its dimensions (w/h) are known. This is used for Chatbot's auto scroll.
112
+ onload?.();
113
+ }}
114
+ mirror={false}
115
+ {label}
116
+ {loop}
117
+ interactive={false}
118
+ {upload}
119
+ {i18n}
120
+ bind:playback_position
121
+ />
122
+ {/key}
123
+ <div data-testid="download-div">
124
+ <IconButtonWrapper
125
+ display_top_corner={display_icon_button_wrapper_top_corner}
126
+ buttons={buttons ?? ["download", "share"]}
127
+ {on_custom_button_click}
128
+ >
129
+ {#if buttons?.some((btn) => typeof btn === "string" && btn === "download")}
130
+ <DownloadLink
131
+ href={value.is_stream
132
+ ? value.url?.replace("playlist.m3u8", "playlist-file")
133
+ : value.url}
134
+ download={value.orig_name || value.path}
135
+ >
136
+ <IconButton Icon={Download} label="Download" />
137
+ </DownloadLink>
138
+ {/if}
139
+ {#if buttons?.some((btn) => typeof btn === "string" && btn === "share")}
140
+ <ShareButton
141
+ {i18n}
142
+ onerror={(detail) => onerror?.(detail)}
143
+ onshare={(detail) => onshare?.(detail)}
144
+ {value}
145
+ formatter={async (value) => {
146
+ if (!value) return "";
147
+ let url = await uploadToHuggingFace(value.data, "url");
148
+ return url;
149
+ }}
150
+ />
151
+ {/if}
152
+ </IconButtonWrapper>
153
+ </div>
154
+ {/if}
6.8.1/video/shared/VideoTimeline.svelte ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, onDestroy } from "svelte";
3
+
4
+ interface Props {
5
+ videoElement: HTMLVideoElement;
6
+ trimmedDuration?: number | null;
7
+ dragStart?: number;
8
+ dragEnd?: number;
9
+ loadingTimeline?: boolean;
10
+ }
11
+
12
+ let {
13
+ videoElement,
14
+ trimmedDuration = $bindable(null),
15
+ dragStart = $bindable(0),
16
+ dragEnd = $bindable(0),
17
+ loadingTimeline = $bindable(false)
18
+ }: Props = $props();
19
+
20
+ let thumbnails = $state<string[]>([]);
21
+ let numberOfThumbnails = 10;
22
+ let intervalId: ReturnType<typeof setInterval> | undefined;
23
+ let videoDuration: number;
24
+
25
+ let leftHandlePosition = $state(0);
26
+ let rightHandlePosition = $state(100);
27
+
28
+ let dragging = $state<string | null>(null);
29
+
30
+ const startDragging = (side: string | null): void => {
31
+ dragging = side;
32
+ };
33
+
34
+ let loadingTimelineValue = $derived(thumbnails.length !== numberOfThumbnails);
35
+
36
+ $effect(() => {
37
+ loadingTimeline = loadingTimelineValue;
38
+ });
39
+
40
+ const stopDragging = (): void => {
41
+ dragging = null;
42
+ };
43
+
44
+ const drag = (event: { clientX: number }, distance?: number): void => {
45
+ if (dragging) {
46
+ const timeline = document.getElementById("timeline");
47
+
48
+ if (!timeline) return;
49
+
50
+ const rect = timeline.getBoundingClientRect();
51
+ let newPercentage = ((event.clientX - rect.left) / rect.width) * 100;
52
+
53
+ if (distance) {
54
+ // Move handle based on arrow key press
55
+ newPercentage =
56
+ dragging === "left"
57
+ ? leftHandlePosition + distance
58
+ : rightHandlePosition + distance;
59
+ } else {
60
+ // Move handle based on mouse drag
61
+ newPercentage = ((event.clientX - rect.left) / rect.width) * 100;
62
+ }
63
+
64
+ newPercentage = Math.max(0, Math.min(newPercentage, 100)); // Keep within 0 and 100
65
+
66
+ if (dragging === "left") {
67
+ leftHandlePosition = Math.min(newPercentage, rightHandlePosition);
68
+
69
+ // Calculate the new time and set it for the videoElement
70
+ const newTimeLeft = (leftHandlePosition / 100) * videoDuration;
71
+ videoElement.currentTime = newTimeLeft;
72
+
73
+ dragStart = newTimeLeft;
74
+ } else if (dragging === "right") {
75
+ rightHandlePosition = Math.max(newPercentage, leftHandlePosition);
76
+
77
+ const newTimeRight = (rightHandlePosition / 100) * videoDuration;
78
+ videoElement.currentTime = newTimeRight;
79
+
80
+ dragEnd = newTimeRight;
81
+ }
82
+
83
+ const startTime = (leftHandlePosition / 100) * videoDuration;
84
+ const endTime = (rightHandlePosition / 100) * videoDuration;
85
+ trimmedDuration = endTime - startTime;
86
+
87
+ leftHandlePosition = leftHandlePosition;
88
+ rightHandlePosition = rightHandlePosition;
89
+ }
90
+ };
91
+
92
+ const moveHandle = (e: KeyboardEvent): void => {
93
+ if (dragging) {
94
+ // Calculate the movement distance as a percentage of the video duration
95
+ const distance = (1 / videoDuration) * 100;
96
+
97
+ if (e.key === "ArrowLeft") {
98
+ drag({ clientX: 0 }, -distance);
99
+ } else if (e.key === "ArrowRight") {
100
+ drag({ clientX: 0 }, distance);
101
+ }
102
+ }
103
+ };
104
+
105
+ const generateThumbnail = (): void => {
106
+ const canvas = document.createElement("canvas");
107
+ const ctx = canvas.getContext("2d");
108
+ if (!ctx) return;
109
+
110
+ canvas.width = videoElement.videoWidth;
111
+ canvas.height = videoElement.videoHeight;
112
+
113
+ ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
114
+
115
+ const thumbnail: string = canvas.toDataURL("image/jpeg", 0.7);
116
+ thumbnails = [...thumbnails, thumbnail];
117
+ };
118
+
119
+ onMount(() => {
120
+ const loadMetadata = (): void => {
121
+ videoDuration = videoElement.duration;
122
+
123
+ const interval = videoDuration / numberOfThumbnails;
124
+ let captures = 0;
125
+
126
+ const onSeeked = (): void => {
127
+ generateThumbnail();
128
+ captures++;
129
+
130
+ if (captures < numberOfThumbnails) {
131
+ videoElement.currentTime += interval;
132
+ } else {
133
+ videoElement.removeEventListener("seeked", onSeeked);
134
+ }
135
+ };
136
+
137
+ videoElement.addEventListener("seeked", onSeeked);
138
+ videoElement.currentTime = 0;
139
+ };
140
+
141
+ if (videoElement.readyState >= 1) {
142
+ loadMetadata();
143
+ } else {
144
+ videoElement.addEventListener("loadedmetadata", loadMetadata);
145
+ }
146
+ });
147
+
148
+ onDestroy(() => {
149
+ window.removeEventListener("mousemove", drag);
150
+ window.removeEventListener("mouseup", stopDragging);
151
+ window.removeEventListener("keydown", moveHandle);
152
+
153
+ if (intervalId !== undefined) {
154
+ clearInterval(intervalId);
155
+ }
156
+ });
157
+
158
+ onMount(() => {
159
+ window.addEventListener("mousemove", drag);
160
+ window.addEventListener("mouseup", stopDragging);
161
+ window.addEventListener("keydown", moveHandle);
162
+ });
163
+ </script>
164
+
165
+ <div class="container">
166
+ {#if loadingTimelineValue}
167
+ <div class="load-wrap">
168
+ <span aria-label="loading timeline" class="loader" />
169
+ </div>
170
+ {:else}
171
+ <div id="timeline" class="thumbnail-wrapper">
172
+ <button
173
+ aria-label="start drag handle for trimming video"
174
+ class="handle left"
175
+ onmousedown={() => startDragging("left")}
176
+ onblur={stopDragging}
177
+ onkeydown={(e) => {
178
+ if (e.key === "ArrowLeft" || e.key == "ArrowRight") {
179
+ startDragging("left");
180
+ }
181
+ }}
182
+ style="left: {leftHandlePosition}%;"
183
+ ></button>
184
+
185
+ <div
186
+ class="opaque-layer"
187
+ style="left: {leftHandlePosition}%; right: {100 - rightHandlePosition}%"
188
+ ></div>
189
+
190
+ {#each thumbnails as thumbnail, i (i)}
191
+ <img src={thumbnail} alt={`frame-${i}`} draggable="false" />
192
+ {/each}
193
+ <button
194
+ aria-label="end drag handle for trimming video"
195
+ class="handle right"
196
+ onmousedown={() => startDragging("right")}
197
+ onblur={stopDragging}
198
+ onkeydown={(e) => {
199
+ if (e.key === "ArrowLeft" || e.key == "ArrowRight") {
200
+ startDragging("right");
201
+ }
202
+ }}
203
+ style="left: {rightHandlePosition}%;"
204
+ ></button>
205
+ </div>
206
+ {/if}
207
+ </div>
208
+
209
+ <style>
210
+ .load-wrap {
211
+ display: flex;
212
+ justify-content: center;
213
+ align-items: center;
214
+ height: 100%;
215
+ }
216
+ .loader {
217
+ display: flex;
218
+ position: relative;
219
+ background-color: var(--border-color-accent-subdued);
220
+ animation: shadowPulse 2s linear infinite;
221
+ box-shadow:
222
+ -24px 0 var(--border-color-accent-subdued),
223
+ 24px 0 var(--border-color-accent-subdued);
224
+ margin: var(--spacing-md);
225
+ border-radius: 50%;
226
+ width: 10px;
227
+ height: 10px;
228
+ scale: 0.5;
229
+ }
230
+
231
+ @keyframes shadowPulse {
232
+ 33% {
233
+ box-shadow:
234
+ -24px 0 var(--border-color-accent-subdued),
235
+ 24px 0 #fff;
236
+ background: #fff;
237
+ }
238
+ 66% {
239
+ box-shadow:
240
+ -24px 0 #fff,
241
+ 24px 0 #fff;
242
+ background: var(--border-color-accent-subdued);
243
+ }
244
+ 100% {
245
+ box-shadow:
246
+ -24px 0 #fff,
247
+ 24px 0 var(--border-color-accent-subdued);
248
+ background: #fff;
249
+ }
250
+ }
251
+
252
+ .container {
253
+ display: flex;
254
+ flex-direction: column;
255
+ align-items: center;
256
+ justify-content: center;
257
+ margin: var(--spacing-lg) var(--spacing-lg) 0 var(--spacing-lg);
258
+ }
259
+
260
+ #timeline {
261
+ display: flex;
262
+ height: var(--size-10);
263
+ flex: 1;
264
+ position: relative;
265
+ }
266
+
267
+ img {
268
+ flex: 1 1 auto;
269
+ min-width: 0;
270
+ object-fit: cover;
271
+ height: var(--size-12);
272
+ border: 1px solid var(--block-border-color);
273
+ user-select: none;
274
+ z-index: 1;
275
+ }
276
+
277
+ .handle {
278
+ width: 3px;
279
+ background-color: var(--color-accent);
280
+ cursor: ew-resize;
281
+ height: var(--size-12);
282
+ z-index: 3;
283
+ position: absolute;
284
+ }
285
+
286
+ .opaque-layer {
287
+ background-color: rgba(230, 103, 40, 0.25);
288
+ border: 1px solid var(--color-accent);
289
+ height: var(--size-12);
290
+ position: absolute;
291
+ z-index: 2;
292
+ }
293
+ </style>
6.8.1/video/shared/VolumeControl.svelte ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount } from "svelte";
3
+
4
+ interface Props {
5
+ current_volume?: number;
6
+ show_volume_slider?: boolean;
7
+ }
8
+
9
+ let {
10
+ current_volume = $bindable(1),
11
+ show_volume_slider = $bindable(false)
12
+ }: Props = $props();
13
+
14
+ let volume_element: HTMLInputElement | undefined = $state();
15
+
16
+ onMount(() => {
17
+ adjustSlider();
18
+ });
19
+
20
+ const adjustSlider = (): void => {
21
+ let slider = volume_element;
22
+ if (!slider) return;
23
+
24
+ slider.style.background = `linear-gradient(to right, white ${
25
+ current_volume * 100
26
+ }%, rgba(255, 255, 255, 0.3) ${current_volume * 100}%)`;
27
+ };
28
+
29
+ $effect(() => {
30
+ current_volume;
31
+ adjustSlider();
32
+ });
33
+ </script>
34
+
35
+ <input
36
+ bind:this={volume_element}
37
+ id="volume"
38
+ class="volume-slider"
39
+ type="range"
40
+ min="0"
41
+ max="1"
42
+ step="0.01"
43
+ value={current_volume}
44
+ onfocusout={() => (show_volume_slider = false)}
45
+ oninput={(e) => {
46
+ if (e.target instanceof HTMLInputElement) {
47
+ current_volume = parseFloat(e.target.value);
48
+ }
49
+ }}
50
+ />
51
+
52
+ <style>
53
+ .volume-slider {
54
+ -webkit-appearance: none;
55
+ appearance: none;
56
+ width: var(--size-20);
57
+ accent-color: var(--color-accent);
58
+ height: 4px;
59
+ cursor: pointer;
60
+ outline: none;
61
+ border-radius: 15px;
62
+ background-color: rgba(255, 255, 255, 0.3);
63
+ margin-left: var(--spacing-sm);
64
+ }
65
+
66
+ input[type="range"]::-webkit-slider-thumb {
67
+ -webkit-appearance: none;
68
+ appearance: none;
69
+ height: 15px;
70
+ width: 15px;
71
+ background-color: white;
72
+ border-radius: 50%;
73
+ border: none;
74
+ transition: 0.2s ease-in-out;
75
+ }
76
+
77
+ input[type="range"]::-moz-range-thumb {
78
+ height: 15px;
79
+ width: 15px;
80
+ background-color: white;
81
+ border-radius: 50%;
82
+ border: none;
83
+ transition: 0.2s ease-in-out;
84
+ }
85
+ </style>
6.8.1/video/shared/index.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export { default as Video } from "./Video.svelte";
6.8.1/video/shared/types.ts ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ export interface SubtitleData {
2
+ start: number;
3
+ end: number;
4
+ text: string;
5
+ }
6.8.1/video/shared/utils.ts ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { toBlobURL } from "@ffmpeg/util";
2
+ import { FFmpeg } from "@ffmpeg/ffmpeg";
3
+ import { lookup } from "mrmime";
4
+
5
+ export const prettyBytes = (bytes: number): string => {
6
+ let units = ["B", "KB", "MB", "GB", "PB"];
7
+ let i = 0;
8
+ while (bytes > 1024) {
9
+ bytes /= 1024;
10
+ i++;
11
+ }
12
+ let unit = units[i];
13
+ return bytes.toFixed(1) + " " + unit;
14
+ };
15
+
16
+ export const playable = (): boolean => {
17
+ // TODO: Fix this
18
+ // let video_element = document.createElement("video");
19
+ // let mime_type = mime.lookup(filename);
20
+ // return video_element.canPlayType(mime_type) != "";
21
+ return true; // FIX BEFORE COMMIT - mime import causing issues
22
+ };
23
+
24
+ export function loaded(
25
+ node: HTMLVideoElement,
26
+ { autoplay }: { autoplay: boolean }
27
+ ): any {
28
+ async function handle_playback(): Promise<void> {
29
+ if (!autoplay) return;
30
+ await node.play();
31
+ }
32
+
33
+ node.addEventListener("loadeddata", handle_playback);
34
+
35
+ return {
36
+ destroy(): void {
37
+ node.removeEventListener("loadeddata", handle_playback);
38
+ }
39
+ };
40
+ }
41
+
42
+ export default async function loadFfmpeg(): Promise<FFmpeg> {
43
+ const ffmpeg = new FFmpeg();
44
+ const baseURL = "https://unpkg.com/@ffmpeg/core@0.12.4/dist/esm";
45
+
46
+ await ffmpeg.load({
47
+ coreURL: await toBlobURL(`${baseURL}/ffmpeg-core.js`, "text/javascript"),
48
+ wasmURL: await toBlobURL(`${baseURL}/ffmpeg-core.wasm`, "application/wasm")
49
+ });
50
+
51
+ return ffmpeg;
52
+ }
53
+
54
+ export function blob_to_data_url(blob: Blob): Promise<string> {
55
+ return new Promise((fulfill, reject) => {
56
+ let reader = new FileReader();
57
+ reader.onerror = reject;
58
+ reader.onload = () => fulfill(reader.result as string);
59
+ reader.readAsDataURL(blob);
60
+ });
61
+ }
62
+
63
+ export async function trimVideo(
64
+ ffmpeg: FFmpeg,
65
+ startTime: number,
66
+ endTime: number,
67
+ videoElement: HTMLVideoElement
68
+ ): Promise<any> {
69
+ const videoUrl = videoElement.src;
70
+ const mimeType = lookup(videoElement.src) || "video/mp4";
71
+ const blobUrl = await toBlobURL(videoUrl, mimeType);
72
+ const response = await fetch(blobUrl);
73
+ const vidBlob = await response.blob();
74
+ const type = getVideoExtensionFromMimeType(mimeType) || "mp4";
75
+ const inputName = `input.${type}`;
76
+ const outputName = `output.${type}`;
77
+
78
+ try {
79
+ if (startTime === 0 && endTime === 0) {
80
+ return vidBlob;
81
+ }
82
+
83
+ await ffmpeg.writeFile(
84
+ inputName,
85
+ new Uint8Array(await vidBlob.arrayBuffer())
86
+ );
87
+
88
+ let command = [
89
+ "-i",
90
+ inputName,
91
+ ...(startTime !== 0 ? ["-ss", startTime.toString()] : []),
92
+ ...(endTime !== 0 ? ["-to", endTime.toString()] : []),
93
+ "-c:a",
94
+ "copy",
95
+ outputName
96
+ ];
97
+
98
+ await ffmpeg.exec(command);
99
+ const outputData = await ffmpeg.readFile(outputName);
100
+ const outputBlob = new Blob([outputData], {
101
+ type: `video/${type}`
102
+ });
103
+
104
+ return outputBlob;
105
+ } catch (error) {
106
+ console.error("Error initializing FFmpeg:", error);
107
+ return vidBlob;
108
+ }
109
+ }
110
+
111
+ const getVideoExtensionFromMimeType = (mimeType: string): string | null => {
112
+ const videoMimeToExtensionMap: { [key: string]: string } = {
113
+ "video/mp4": "mp4",
114
+ "video/webm": "webm",
115
+ "video/ogg": "ogv",
116
+ "video/quicktime": "mov",
117
+ "video/x-msvideo": "avi",
118
+ "video/x-matroska": "mkv",
119
+ "video/mpeg": "mpeg",
120
+ "video/3gpp": "3gp",
121
+ "video/3gpp2": "3g2",
122
+ "video/h261": "h261",
123
+ "video/h263": "h263",
124
+ "video/h264": "h264",
125
+ "video/jpeg": "jpgv",
126
+ "video/jpm": "jpm",
127
+ "video/mj2": "mj2",
128
+ "video/mpv": "mpv",
129
+ "video/vnd.ms-playready.media.pyv": "pyv",
130
+ "video/vnd.uvvu.mp4": "uvu",
131
+ "video/vnd.vivo": "viv",
132
+ "video/x-f4v": "f4v",
133
+ "video/x-fli": "fli",
134
+ "video/x-flv": "flv",
135
+ "video/x-m4v": "m4v",
136
+ "video/x-ms-asf": "asf",
137
+ "video/x-ms-wm": "wm",
138
+ "video/x-ms-wmv": "wmv",
139
+ "video/x-ms-wmx": "wmx",
140
+ "video/x-ms-wvx": "wvx",
141
+ "video/x-sgi-movie": "movie",
142
+ "video/x-smv": "smv"
143
+ };
144
+
145
+ return videoMimeToExtensionMap[mimeType] || null;
146
+ };
147
+
148
+ export interface WebcamOptions {
149
+ mirror: boolean;
150
+ constraints: Record<string, any>;
151
+ }
6.8.1/video/types.ts ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { LoadingStatus } from "@gradio/statustracker";
3
+ import type { CustomButton } from "@gradio/utils";
4
+ import type { WebcamOptions } from "./shared/utils";
5
+
6
+ export interface VideoProps {
7
+ value: FileData | null;
8
+ height: number | undefined;
9
+ width: number | undefined;
10
+ autoplay: boolean;
11
+ buttons: ("share" | "download" | "fullscreen" | CustomButton)[];
12
+ sources:
13
+ | ["webcam"]
14
+ | ["upload"]
15
+ | ["webcam", "upload"]
16
+ | ["upload", "webcam"];
17
+ webcam_options: WebcamOptions;
18
+ include_audio: boolean;
19
+ loop: boolean;
20
+ webcam_constraints: object;
21
+ subtitles: FileData | null;
22
+ playback_position: number;
23
+ }
24
+
25
+ export interface VideoEvents {
26
+ change: never;
27
+ clear: never;
28
+ play: never;
29
+ pause: never;
30
+ upload: never;
31
+ stop: never;
32
+ end: never;
33
+ start_recording: never;
34
+ stop_recording: never;
35
+ input: any;
36
+ clear_status: LoadingStatus;
37
+ share: any;
38
+ error: any;
39
+ warning: any;
40
+ custom_button_click: { id: number };
41
+ }