gradio-pr-bot commited on
Commit
85890a7
·
verified ·
1 Parent(s): 794b36c

Upload folder using huggingface_hub

Browse files
6.0.1/video/Example.svelte ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ export let type: "gallery" | "table";
7
+ export let selected = false;
8
+ export let value: null | FileData = null;
9
+ export let loop: boolean;
10
+ let video: HTMLVideoElement;
11
+
12
+ async function init(): Promise<void> {
13
+ video.muted = true;
14
+ video.playsInline = true;
15
+ video.controls = false;
16
+ video.setAttribute("muted", "");
17
+
18
+ await video.play();
19
+ video.pause();
20
+ }
21
+ </script>
22
+
23
+ {#if value}
24
+ {#if playable()}
25
+ <div
26
+ class="container"
27
+ class:table={type === "table"}
28
+ class:gallery={type === "gallery"}
29
+ class:selected
30
+ >
31
+ <Video
32
+ muted
33
+ playsinline
34
+ bind:node={video}
35
+ on:loadeddata={init}
36
+ on:mouseover={video.play.bind(video)}
37
+ on:mouseout={video.pause.bind(video)}
38
+ src={value?.url}
39
+ is_stream={false}
40
+ {loop}
41
+ />
42
+ </div>
43
+ {:else}
44
+ <div>{value}</div>
45
+ {/if}
46
+ {/if}
47
+
48
+ <style>
49
+ .container {
50
+ flex: none;
51
+ max-width: none;
52
+ }
53
+ .container :global(video) {
54
+ width: var(--size-full);
55
+ height: var(--size-full);
56
+ object-fit: cover;
57
+ }
58
+
59
+ .container:hover,
60
+ .container.selected {
61
+ border-color: var(--border-color-accent);
62
+ }
63
+ .container.table {
64
+ margin: 0 auto;
65
+ border: 2px solid var(--border-color-primary);
66
+ border-radius: var(--radius-lg);
67
+ overflow: hidden;
68
+ width: var(--size-20);
69
+ height: var(--size-20);
70
+ object-fit: cover;
71
+ }
72
+
73
+ .container.gallery {
74
+ height: var(--size-20);
75
+ max-height: var(--size-20);
76
+ object-fit: cover;
77
+ }
78
+ </style>
6.0.1/video/Index.svelte ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 }: CustomEvent<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 }: CustomEvent<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
+ show_share_button={(gradio.props.buttons || []).includes("share")}
104
+ show_download_button={(gradio.props.buttons || ["download"]).includes(
105
+ "download"
106
+ )}
107
+ on:play={() => gradio.dispatch("play")}
108
+ on:pause={() => gradio.dispatch("pause")}
109
+ on:stop={() => gradio.dispatch("stop")}
110
+ on:end={() => gradio.dispatch("end")}
111
+ on:share={({ detail }) => gradio.dispatch("share", detail)}
112
+ on:error={({ detail }) => gradio.dispatch("error", detail)}
113
+ i18n={gradio.i18n}
114
+ upload={(...args) => gradio.shared.client.upload(...args)}
115
+ />
116
+ </Block>
117
+ {:else}
118
+ <Block
119
+ visible={gradio.shared.visible}
120
+ variant={gradio.props.value === null && active_source === "upload"
121
+ ? "dashed"
122
+ : "solid"}
123
+ border_mode={dragging ? "focus" : "base"}
124
+ padding={false}
125
+ elem_id={gradio.shared.elem_id}
126
+ elem_classes={gradio.shared.elem_classes}
127
+ height={gradio.props.height || undefined}
128
+ width={gradio.props.width}
129
+ container={gradio.shared.container}
130
+ scale={gradio.shared.scale}
131
+ min_width={gradio.shared.min_width}
132
+ allow_overflow={false}
133
+ >
134
+ <StatusTracker
135
+ autoscroll={gradio.shared.autoscroll}
136
+ i18n={gradio.i18n}
137
+ {...gradio.shared.loading_status}
138
+ on_clear_status={() =>
139
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
140
+ />
141
+
142
+ <Video
143
+ bind:upload_promise
144
+ value={gradio.props.value}
145
+ subtitle={gradio.props.subtitles}
146
+ on:change={handle_change}
147
+ on:drag={({ detail }) => (dragging = detail)}
148
+ on:error={handle_error}
149
+ bind:uploading
150
+ label={gradio.shared.label}
151
+ show_label={gradio.shared.show_label}
152
+ show_download_button={(gradio.props.buttons || []).includes("download")}
153
+ sources={gradio.props.sources}
154
+ {active_source}
155
+ webcam_options={gradio.props.webcam_options}
156
+ include_audio={gradio.props.include_audio}
157
+ autoplay={gradio.props.autoplay}
158
+ root={gradio.shared.root}
159
+ loop={gradio.props.loop}
160
+ {handle_reset_value}
161
+ on:clear={() => {
162
+ gradio.props.value = null;
163
+ gradio.dispatch("clear");
164
+ }}
165
+ on:play={() => gradio.dispatch("play")}
166
+ on:pause={() => gradio.dispatch("pause")}
167
+ on:upload={() => gradio.dispatch("upload")}
168
+ on:stop={() => gradio.dispatch("stop")}
169
+ on:end={() => gradio.dispatch("end")}
170
+ on:start_recording={() => gradio.dispatch("start_recording")}
171
+ on:stop_recording={() => gradio.dispatch("stop_recording")}
172
+ i18n={gradio.i18n}
173
+ max_file_size={gradio.shared.max_file_size}
174
+ upload={(...args) => gradio.shared.client.upload(...args)}
175
+ stream_handler={(...args) => gradio.shared.client.stream(...args)}
176
+ >
177
+ <UploadText i18n={gradio.i18n} type="video" />
178
+ </Video>
179
+ </Block>
180
+ {/if}
6.0.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.0.1/video/package.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/video",
3
+ "version": "0.17.0",
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.43.4"
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.0.1/video/shared/InteractiveVideo.svelte ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } from "svelte";
3
+ import { Upload, ModifyUpload } from "@gradio/upload";
4
+ import type { FileData, Client } from "@gradio/client";
5
+ import { BlockLabel } from "@gradio/atoms";
6
+ import { Webcam } from "@gradio/image";
7
+ import { Video } from "@gradio/icons";
8
+ import type { WebcamOptions } from "./utils";
9
+ import { prettyBytes, playable } from "./utils";
10
+ import Player from "./Player.svelte";
11
+ import type { I18nFormatter } from "@gradio/utils";
12
+ import { SelectSource } from "@gradio/atoms";
13
+
14
+ export let value: FileData | null = null;
15
+ export let subtitle: FileData | null = null;
16
+ export let sources:
17
+ | ["webcam"]
18
+ | ["upload"]
19
+ | ["webcam", "upload"]
20
+ | ["upload", "webcam"] = ["webcam", "upload"];
21
+ export let label: string | undefined = undefined;
22
+ export let show_download_button = false;
23
+ export let show_label = true;
24
+ export let webcam_options: WebcamOptions;
25
+ export let include_audio: boolean;
26
+ export let autoplay: boolean;
27
+ export let root: string;
28
+ export let i18n: I18nFormatter;
29
+ export let active_source: "webcam" | "upload" = "webcam";
30
+ export let handle_reset_value: () => void = () => {};
31
+ export let max_file_size: number | null = null;
32
+ export let upload: Client["upload"];
33
+ export let stream_handler: Client["stream"];
34
+ export let loop: boolean;
35
+ export let uploading = false;
36
+ export let upload_promise: Promise<any> | null = null;
37
+
38
+ let has_change_history = false;
39
+
40
+ const dispatch = createEventDispatcher<{
41
+ change: FileData | null;
42
+ clear?: never;
43
+ play?: never;
44
+ pause?: never;
45
+ end?: never;
46
+ drag: boolean;
47
+ error: string;
48
+ upload: FileData;
49
+ start_recording?: never;
50
+ stop_recording?: never;
51
+ }>();
52
+
53
+ function handle_load({ detail }: CustomEvent<FileData | null>): void {
54
+ value = detail;
55
+ dispatch("change", detail);
56
+ dispatch("upload", detail!);
57
+ }
58
+
59
+ function handle_clear(): void {
60
+ value = null;
61
+ dispatch("change", null);
62
+ dispatch("clear");
63
+ }
64
+
65
+ function handle_change(video: FileData): void {
66
+ has_change_history = true;
67
+ dispatch("change", video);
68
+ }
69
+
70
+ function handle_capture({
71
+ detail
72
+ }: CustomEvent<FileData | any | null>): void {
73
+ dispatch("change", detail);
74
+ }
75
+
76
+ let dragging = false;
77
+ $: dispatch("drag", dragging);
78
+ </script>
79
+
80
+ <BlockLabel {show_label} Icon={Video} label={label || "Video"} />
81
+ <div data-testid="video" class="video-container">
82
+ {#if value === null || value?.url === undefined}
83
+ <div class="upload-container">
84
+ {#if active_source === "upload"}
85
+ <Upload
86
+ bind:upload_promise
87
+ bind:dragging
88
+ bind:uploading
89
+ filetype="video/x-m4v,video/*"
90
+ on:load={handle_load}
91
+ {max_file_size}
92
+ on:error={({ detail }) => dispatch("error", detail)}
93
+ {root}
94
+ {upload}
95
+ {stream_handler}
96
+ aria_label={i18n("video.drop_to_upload")}
97
+ >
98
+ <slot />
99
+ </Upload>
100
+ {:else if active_source === "webcam"}
101
+ <Webcam
102
+ {root}
103
+ mirror_webcam={webcam_options.mirror}
104
+ webcam_constraints={webcam_options.constraints}
105
+ {include_audio}
106
+ mode="video"
107
+ on:error
108
+ on:capture={handle_capture}
109
+ on:start_recording
110
+ on:stop_recording
111
+ {i18n}
112
+ {upload}
113
+ stream_every={1}
114
+ />
115
+ {/if}
116
+ </div>
117
+ {:else if value?.url}
118
+ {#key value?.url}
119
+ <Player
120
+ {upload}
121
+ {root}
122
+ interactive
123
+ {autoplay}
124
+ src={value.url}
125
+ subtitle={subtitle?.url}
126
+ is_stream={false}
127
+ on:play
128
+ on:pause
129
+ on:stop
130
+ on:end
131
+ on:error
132
+ mirror={webcam_options.mirror && active_source === "webcam"}
133
+ {label}
134
+ {handle_change}
135
+ {handle_reset_value}
136
+ {loop}
137
+ {value}
138
+ {i18n}
139
+ {show_download_button}
140
+ {handle_clear}
141
+ {has_change_history}
142
+ />
143
+ {/key}
144
+ {:else if value.size}
145
+ <div class="file-name">{value.orig_name || value.url}</div>
146
+ <div class="file-size">
147
+ {prettyBytes(value.size)}
148
+ </div>
149
+ {/if}
150
+
151
+ <SelectSource {sources} bind:active_source {handle_clear} />
152
+ </div>
153
+
154
+ <style>
155
+ .file-name {
156
+ padding: var(--size-6);
157
+ font-size: var(--text-xxl);
158
+ word-break: break-all;
159
+ }
160
+
161
+ .file-size {
162
+ padding: var(--size-2);
163
+ font-size: var(--text-xl);
164
+ }
165
+
166
+ .upload-container {
167
+ height: 100%;
168
+ width: 100%;
169
+ }
170
+
171
+ .video-container {
172
+ display: flex;
173
+ height: 100%;
174
+ flex-direction: column;
175
+ justify-content: center;
176
+ align-items: center;
177
+ }
178
+ </style>
6.0.1/video/shared/Player.svelte ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } 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 type { FileData, Client } from "@gradio/client";
7
+ import { prepare_files } from "@gradio/client";
8
+ import { format_time } from "@gradio/utils";
9
+ import type { I18nFormatter } from "@gradio/utils";
10
+
11
+ export let root = "";
12
+ export let src: string;
13
+ export let subtitle: string | null = null;
14
+ export let mirror: boolean;
15
+ export let autoplay: boolean;
16
+ export let loop: boolean;
17
+ export let label = "test";
18
+ export let interactive = false;
19
+ export let handle_change: (video: FileData) => void = () => {};
20
+ export let handle_reset_value: () => void = () => {};
21
+ export let upload: Client["upload"];
22
+ export let is_stream: boolean | undefined;
23
+ export let i18n: I18nFormatter;
24
+ export let show_download_button = false;
25
+ export let value: FileData | null = null;
26
+ export let handle_clear: () => void = () => {};
27
+ export let has_change_history = false;
28
+
29
+ const dispatch = createEventDispatcher<{
30
+ play: undefined;
31
+ pause: undefined;
32
+ stop: undefined;
33
+ end: undefined;
34
+ clear: undefined;
35
+ }>();
36
+
37
+ let time = 0;
38
+ let duration: number;
39
+ let paused = true;
40
+ let video: HTMLVideoElement;
41
+ let processingVideo = false;
42
+
43
+ function handleMove(e: TouchEvent | MouseEvent): void {
44
+ if (!duration) return;
45
+
46
+ if (e.type === "click") {
47
+ handle_click(e as MouseEvent);
48
+ return;
49
+ }
50
+
51
+ if (e.type !== "touchmove" && !((e as MouseEvent).buttons & 1)) return;
52
+
53
+ const clientX =
54
+ e.type === "touchmove"
55
+ ? (e as TouchEvent).touches[0].clientX
56
+ : (e as MouseEvent).clientX;
57
+ const { left, right } = (
58
+ e.currentTarget as HTMLProgressElement
59
+ ).getBoundingClientRect();
60
+ time = (duration * (clientX - left)) / (right - left);
61
+ }
62
+
63
+ async function play_pause(): Promise<void> {
64
+ if (document.fullscreenElement != video) {
65
+ const isPlaying =
66
+ video.currentTime > 0 &&
67
+ !video.paused &&
68
+ !video.ended &&
69
+ video.readyState > video.HAVE_CURRENT_DATA;
70
+
71
+ if (!isPlaying) {
72
+ await video.play();
73
+ } else video.pause();
74
+ }
75
+ }
76
+
77
+ function handle_click(e: MouseEvent): void {
78
+ const { left, right } = (
79
+ e.currentTarget as HTMLProgressElement
80
+ ).getBoundingClientRect();
81
+ time = (duration * (e.clientX - left)) / (right - left);
82
+ }
83
+
84
+ function handle_end(): void {
85
+ dispatch("stop");
86
+ dispatch("end");
87
+ }
88
+
89
+ const handle_trim_video = async (videoBlob: Blob): Promise<void> => {
90
+ let _video_blob = new File([videoBlob], "video.mp4");
91
+ const val = await prepare_files([_video_blob]);
92
+ let value = ((await upload(val, root))?.filter(Boolean) as FileData[])[0];
93
+
94
+ handle_change(value);
95
+ };
96
+
97
+ function open_full_screen(): void {
98
+ video.requestFullscreen();
99
+ }
100
+
101
+ $: time = time || 0;
102
+ $: duration = duration || 0;
103
+ </script>
104
+
105
+ <div class="wrap">
106
+ <div class="mirror-wrap" class:mirror>
107
+ <Video
108
+ {src}
109
+ preload="auto"
110
+ {autoplay}
111
+ {loop}
112
+ {is_stream}
113
+ on:click={play_pause}
114
+ on:play
115
+ on:pause
116
+ on:error
117
+ on:ended={handle_end}
118
+ bind:currentTime={time}
119
+ bind:duration
120
+ bind:paused
121
+ bind:node={video}
122
+ data-testid={`${label}-player`}
123
+ {processingVideo}
124
+ on:loadstart
125
+ on:loadeddata
126
+ on:loadedmetadata
127
+ >
128
+ <track kind="captions" src={subtitle} default />
129
+ </Video>
130
+ </div>
131
+
132
+ <div class="controls">
133
+ <div class="inner">
134
+ <span
135
+ role="button"
136
+ tabindex="0"
137
+ class="icon"
138
+ aria-label="play-pause-replay-button"
139
+ on:click={play_pause}
140
+ on:keydown={play_pause}
141
+ >
142
+ {#if time === duration}
143
+ <Undo />
144
+ {:else if paused}
145
+ <Play />
146
+ {:else}
147
+ <Pause />
148
+ {/if}
149
+ </span>
150
+
151
+ <span class="time">{format_time(time)} / {format_time(duration)}</span>
152
+
153
+ <!-- TODO: implement accessible video timeline for 4.0 -->
154
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
155
+ <!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
156
+ <progress
157
+ value={time / duration || 0}
158
+ on:mousemove={handleMove}
159
+ on:touchmove|preventDefault={handleMove}
160
+ on:click|stopPropagation|preventDefault={handle_click}
161
+ />
162
+
163
+ <div
164
+ role="button"
165
+ tabindex="0"
166
+ class="icon"
167
+ aria-label="full-screen"
168
+ on:click={open_full_screen}
169
+ on:keypress={open_full_screen}
170
+ >
171
+ <Maximize />
172
+ </div>
173
+ </div>
174
+ </div>
175
+ </div>
176
+ {#if interactive}
177
+ <VideoControls
178
+ videoElement={video}
179
+ showRedo
180
+ {handle_trim_video}
181
+ {handle_reset_value}
182
+ bind:processingVideo
183
+ {value}
184
+ {i18n}
185
+ {show_download_button}
186
+ {handle_clear}
187
+ {has_change_history}
188
+ />
189
+ {/if}
190
+
191
+ <style lang="postcss">
192
+ span {
193
+ text-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
194
+ }
195
+
196
+ progress {
197
+ margin-right: var(--size-3);
198
+ border-radius: var(--radius-sm);
199
+ width: var(--size-full);
200
+ height: var(--size-2);
201
+ }
202
+
203
+ progress::-webkit-progress-bar {
204
+ border-radius: 2px;
205
+ background-color: rgba(255, 255, 255, 0.2);
206
+ overflow: hidden;
207
+ }
208
+
209
+ progress::-webkit-progress-value {
210
+ background-color: rgba(255, 255, 255, 0.9);
211
+ }
212
+
213
+ .mirror {
214
+ transform: scaleX(-1);
215
+ }
216
+
217
+ .mirror-wrap {
218
+ position: relative;
219
+ height: 100%;
220
+ width: 100%;
221
+ }
222
+
223
+ .controls {
224
+ position: absolute;
225
+ bottom: 0;
226
+ opacity: 0;
227
+ transition: 500ms;
228
+ margin: var(--size-2);
229
+ border-radius: var(--radius-md);
230
+ background: var(--color-grey-800);
231
+ padding: var(--size-2) var(--size-1);
232
+ width: calc(100% - 0.375rem * 2);
233
+ width: calc(100% - var(--size-2) * 2);
234
+ }
235
+ .wrap:hover .controls {
236
+ opacity: 1;
237
+ }
238
+
239
+ .inner {
240
+ display: flex;
241
+ justify-content: space-between;
242
+ align-items: center;
243
+ padding-right: var(--size-2);
244
+ padding-left: var(--size-2);
245
+ width: var(--size-full);
246
+ height: var(--size-full);
247
+ }
248
+
249
+ .icon {
250
+ display: flex;
251
+ justify-content: center;
252
+ cursor: pointer;
253
+ width: var(--size-6);
254
+ color: white;
255
+ }
256
+
257
+ .time {
258
+ flex-shrink: 0;
259
+ margin-right: var(--size-3);
260
+ margin-left: var(--size-3);
261
+ color: white;
262
+ font-size: var(--text-sm);
263
+ font-family: var(--font-mono);
264
+ }
265
+ .wrap {
266
+ position: relative;
267
+ background-color: var(--background-fill-secondary);
268
+ height: var(--size-full);
269
+ width: var(--size-full);
270
+ border-radius: var(--radius-xl);
271
+ }
272
+ .wrap :global(video) {
273
+ height: var(--size-full);
274
+ width: var(--size-full);
275
+ }
276
+ </style>
6.0.1/video/shared/Video.svelte ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { HTMLVideoAttributes } from "svelte/elements";
3
+ import { createEventDispatcher } from "svelte";
4
+ import { loaded } from "./utils";
5
+
6
+ import Hls from "hls.js";
7
+
8
+ export let src: HTMLVideoAttributes["src"] = undefined;
9
+
10
+ export let muted: HTMLVideoAttributes["muted"] = undefined;
11
+ export let playsinline: HTMLVideoAttributes["playsinline"] = undefined;
12
+ export let preload: HTMLVideoAttributes["preload"] = undefined;
13
+ export let autoplay: HTMLVideoAttributes["autoplay"] = undefined;
14
+ export let controls: HTMLVideoAttributes["controls"] = undefined;
15
+
16
+ export let currentTime: number | undefined = undefined;
17
+ export let duration: number | undefined = undefined;
18
+ export let paused: boolean | undefined = undefined;
19
+
20
+ export let node: HTMLVideoElement | undefined = undefined;
21
+ export let loop: boolean;
22
+ export let is_stream;
23
+
24
+ export let processingVideo = false;
25
+
26
+ let stream_active = false;
27
+
28
+ const dispatch = createEventDispatcher();
29
+
30
+ function load_stream(
31
+ src: string | null | undefined,
32
+ is_stream: boolean,
33
+ node: HTMLVideoElement
34
+ ): void {
35
+ if (!src || !is_stream) return;
36
+
37
+ if (Hls.isSupported() && !stream_active) {
38
+ const hls = new Hls({
39
+ maxBufferLength: 1, // 0.5 seconds (500 ms)
40
+ maxMaxBufferLength: 1, // Maximum max buffer length in seconds
41
+ lowLatencyMode: true // Enable low latency mode
42
+ });
43
+ hls.loadSource(src);
44
+ hls.attachMedia(node);
45
+ hls.on(Hls.Events.MANIFEST_PARSED, function () {
46
+ (node as HTMLVideoElement).play();
47
+ });
48
+ hls.on(Hls.Events.ERROR, function (event, data) {
49
+ console.error("HLS error:", event, data);
50
+ if (data.fatal) {
51
+ switch (data.type) {
52
+ case Hls.ErrorTypes.NETWORK_ERROR:
53
+ console.error(
54
+ "Fatal network error encountered, trying to recover"
55
+ );
56
+ hls.startLoad();
57
+ break;
58
+ case Hls.ErrorTypes.MEDIA_ERROR:
59
+ console.error("Fatal media error encountered, trying to recover");
60
+ hls.recoverMediaError();
61
+ break;
62
+ default:
63
+ console.error("Fatal error, cannot recover");
64
+ hls.destroy();
65
+ break;
66
+ }
67
+ }
68
+ });
69
+ stream_active = true;
70
+ }
71
+ }
72
+
73
+ $: (src, (stream_active = false));
74
+
75
+ $: if (node && src && is_stream) {
76
+ load_stream(src, is_stream, node);
77
+ }
78
+ </script>
79
+
80
+ <!--
81
+ The spread operator with `$$props` or `$$restProps` can't be used here
82
+ to pass props from the parent component to the <video> element
83
+ because of its unexpected behavior: https://github.com/sveltejs/svelte/issues/7404
84
+ 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.
85
+ 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.
86
+ -->
87
+ <div class:hidden={!processingVideo} class="overlay">
88
+ <span class="load-wrap">
89
+ <span class="loader" />
90
+ </span>
91
+ </div>
92
+ <video
93
+ {src}
94
+ {muted}
95
+ {playsinline}
96
+ {preload}
97
+ {autoplay}
98
+ {controls}
99
+ {loop}
100
+ on:loadeddata={dispatch.bind(null, "loadeddata")}
101
+ on:click={dispatch.bind(null, "click")}
102
+ on:play={dispatch.bind(null, "play")}
103
+ on:pause={dispatch.bind(null, "pause")}
104
+ on:ended={dispatch.bind(null, "ended")}
105
+ on:mouseover={dispatch.bind(null, "mouseover")}
106
+ on:mouseout={dispatch.bind(null, "mouseout")}
107
+ on:focus={dispatch.bind(null, "focus")}
108
+ on:blur={dispatch.bind(null, "blur")}
109
+ on:error={dispatch.bind(null, "error", "Video not playable")}
110
+ on:loadstart
111
+ on:loadeddata
112
+ on:loadedmetadata
113
+ bind:currentTime
114
+ bind:duration
115
+ bind:paused
116
+ bind:this={node}
117
+ use:loaded={{ autoplay: autoplay ?? false }}
118
+ data-testid={$$props["data-testid"]}
119
+ crossorigin="anonymous"
120
+ >
121
+ <slot />
122
+ </video>
123
+
124
+ <style>
125
+ .overlay {
126
+ position: absolute;
127
+ background-color: rgba(0, 0, 0, 0.4);
128
+ width: 100%;
129
+ height: 100%;
130
+ }
131
+
132
+ .hidden {
133
+ display: none;
134
+ }
135
+
136
+ .load-wrap {
137
+ display: flex;
138
+ justify-content: center;
139
+ align-items: center;
140
+ height: 100%;
141
+ }
142
+
143
+ .loader {
144
+ display: flex;
145
+ position: relative;
146
+ background-color: var(--border-color-accent-subdued);
147
+ animation: shadowPulse 2s linear infinite;
148
+ box-shadow:
149
+ -24px 0 var(--border-color-accent-subdued),
150
+ 24px 0 var(--border-color-accent-subdued);
151
+ margin: var(--spacing-md);
152
+ border-radius: 50%;
153
+ width: 10px;
154
+ height: 10px;
155
+ scale: 0.5;
156
+ }
157
+
158
+ @keyframes shadowPulse {
159
+ 33% {
160
+ box-shadow:
161
+ -24px 0 var(--border-color-accent-subdued),
162
+ 24px 0 #fff;
163
+ background: #fff;
164
+ }
165
+ 66% {
166
+ box-shadow:
167
+ -24px 0 #fff,
168
+ 24px 0 #fff;
169
+ background: var(--border-color-accent-subdued);
170
+ }
171
+ 100% {
172
+ box-shadow:
173
+ -24px 0 #fff,
174
+ 24px 0 var(--border-color-accent-subdued);
175
+ background: #fff;
176
+ }
177
+ }
178
+ </style>
6.0.1/video/shared/VideoControls.svelte ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ export let videoElement: HTMLVideoElement;
14
+
15
+ export let showRedo = false;
16
+ export let interactive = true;
17
+ export let mode = "";
18
+ export let handle_reset_value: () => void;
19
+ export let handle_trim_video: (videoBlob: Blob) => void;
20
+ export let processingVideo = false;
21
+ export let i18n: (key: string) => string;
22
+ export let value: FileData | null = null;
23
+ export let show_download_button = false;
24
+ export let handle_clear: () => void = () => {};
25
+ export let has_change_history = false;
26
+
27
+ let ffmpeg: FFmpeg;
28
+
29
+ onMount(async () => {
30
+ ffmpeg = await loadFfmpeg();
31
+ });
32
+
33
+ $: if (mode === "edit" && trimmedDuration === null && videoElement)
34
+ trimmedDuration = videoElement.duration;
35
+
36
+ let trimmedDuration: number | null = null;
37
+ let dragStart = 0;
38
+ let dragEnd = 0;
39
+
40
+ let loadingTimeline = false;
41
+
42
+ const toggleTrimmingMode = (): void => {
43
+ if (mode === "edit") {
44
+ mode = "";
45
+ trimmedDuration = videoElement.duration;
46
+ } else {
47
+ mode = "edit";
48
+ }
49
+ };
50
+ </script>
51
+
52
+ <div class="container" class:hidden={mode !== "edit"}>
53
+ {#if mode === "edit"}
54
+ <div class="timeline-wrapper">
55
+ <VideoTimeline
56
+ {videoElement}
57
+ bind:dragStart
58
+ bind:dragEnd
59
+ bind:trimmedDuration
60
+ bind:loadingTimeline
61
+ />
62
+ </div>
63
+ {/if}
64
+
65
+ <div class="controls" data-testid="waveform-controls">
66
+ {#if mode === "edit" && trimmedDuration !== null}
67
+ <time
68
+ aria-label="duration of selected region in seconds"
69
+ class:hidden={loadingTimeline}>{format_time(trimmedDuration)}</time
70
+ >
71
+ <div class="edit-buttons">
72
+ <button
73
+ class:hidden={loadingTimeline}
74
+ class="text-button"
75
+ on:click={() => {
76
+ mode = "";
77
+ processingVideo = true;
78
+ trimVideo(ffmpeg, dragStart, dragEnd, videoElement)
79
+ .then((videoBlob) => {
80
+ handle_trim_video(videoBlob);
81
+ })
82
+ .then(() => {
83
+ processingVideo = false;
84
+ });
85
+ }}>Trim</button
86
+ >
87
+ <button
88
+ class="text-button"
89
+ class:hidden={loadingTimeline}
90
+ on:click={toggleTrimmingMode}>Cancel</button
91
+ >
92
+ </div>
93
+ {:else}
94
+ <div />
95
+ {/if}
96
+ </div>
97
+ </div>
98
+
99
+ <ModifyUpload
100
+ {i18n}
101
+ on:clear={() => handle_clear()}
102
+ download={show_download_button ? value?.url : null}
103
+ >
104
+ {#if showRedo && mode === ""}
105
+ <IconButton
106
+ Icon={Undo}
107
+ label="Reset video to initial value"
108
+ disabled={processingVideo || !has_change_history}
109
+ on:click={() => {
110
+ handle_reset_value();
111
+ mode = "";
112
+ }}
113
+ />
114
+ {/if}
115
+
116
+ {#if interactive && mode === ""}
117
+ <IconButton
118
+ Icon={Trim}
119
+ label="Trim video to selection"
120
+ disabled={processingVideo}
121
+ on:click={toggleTrimmingMode}
122
+ />
123
+ {/if}
124
+ </ModifyUpload>
125
+
126
+ <style>
127
+ .container {
128
+ width: 100%;
129
+ }
130
+ time {
131
+ color: var(--color-accent);
132
+ font-weight: bold;
133
+ padding-left: var(--spacing-xs);
134
+ }
135
+
136
+ .timeline-wrapper {
137
+ display: flex;
138
+ align-items: center;
139
+ justify-content: center;
140
+ width: 100%;
141
+ }
142
+
143
+ .text-button {
144
+ border: 1px solid var(--neutral-400);
145
+ border-radius: var(--radius-sm);
146
+ font-weight: 300;
147
+ font-size: var(--size-3);
148
+ text-align: center;
149
+ color: var(--neutral-400);
150
+ height: var(--size-5);
151
+ font-weight: bold;
152
+ padding: 0 5px;
153
+ margin-left: 5px;
154
+ }
155
+
156
+ .text-button:hover,
157
+ .text-button:focus {
158
+ color: var(--color-accent);
159
+ border-color: var(--color-accent);
160
+ }
161
+
162
+ .controls {
163
+ display: flex;
164
+ justify-content: space-between;
165
+ align-items: center;
166
+ margin: var(--spacing-lg);
167
+ overflow: hidden;
168
+ }
169
+
170
+ .edit-buttons {
171
+ display: flex;
172
+ gap: var(--spacing-sm);
173
+ }
174
+
175
+ @media (max-width: 320px) {
176
+ .controls {
177
+ flex-direction: column;
178
+ align-items: flex-start;
179
+ }
180
+
181
+ .edit-buttons {
182
+ margin-top: var(--spacing-sm);
183
+ }
184
+
185
+ .controls * {
186
+ margin: var(--spacing-sm);
187
+ }
188
+
189
+ .controls .text-button {
190
+ margin-left: 0;
191
+ }
192
+ }
193
+
194
+ .container {
195
+ display: flex;
196
+ flex-direction: column;
197
+ }
198
+
199
+ .hidden {
200
+ display: none;
201
+ }
202
+ </style>
6.0.1/video/shared/VideoPreview.svelte ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher, afterUpdate, 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
+
15
+ import Player from "./Player.svelte";
16
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
17
+
18
+ export let value: FileData | null = null;
19
+ export let subtitle: FileData | null = null;
20
+ export let label: string | undefined = undefined;
21
+ export let show_label = true;
22
+ export let autoplay: boolean;
23
+ export let show_share_button = true;
24
+ export let show_download_button = true;
25
+ export let loop: boolean;
26
+ export let i18n: I18nFormatter;
27
+ export let upload: Client["upload"];
28
+ export let display_icon_button_wrapper_top_corner = false;
29
+
30
+ let old_value: FileData | null = null;
31
+ let old_subtitle: FileData | null = null;
32
+
33
+ const dispatch = createEventDispatcher<{
34
+ change: FileData;
35
+ play: undefined;
36
+ pause: undefined;
37
+ end: undefined;
38
+ stop: undefined;
39
+ load: undefined;
40
+ }>();
41
+
42
+ $: value && dispatch("change", value);
43
+
44
+ afterUpdate(async () => {
45
+ // needed to bust subtitle caching issues on Chrome
46
+ if (
47
+ value !== old_value &&
48
+ subtitle !== old_subtitle &&
49
+ old_subtitle !== null
50
+ ) {
51
+ old_value = value;
52
+ value = null;
53
+ await tick();
54
+ value = old_value;
55
+ }
56
+ old_value = value;
57
+ old_subtitle = subtitle;
58
+ });
59
+ </script>
60
+
61
+ <BlockLabel {show_label} Icon={Video} label={label || "Video"} />
62
+ {#if !value || value.url === undefined}
63
+ <Empty unpadded_box={true} size="large"><Video /></Empty>
64
+ {:else}
65
+ {#key value.url}
66
+ <Player
67
+ src={value.url}
68
+ subtitle={subtitle?.url}
69
+ is_stream={value.is_stream}
70
+ {autoplay}
71
+ on:play
72
+ on:pause
73
+ on:stop
74
+ on:end
75
+ on:loadedmetadata={() => {
76
+ // Deal with `<video>`'s `loadedmetadata` event as `VideoPreview`'s `load` event
77
+ // to represent not only the video is loaded but also the metadata is loaded
78
+ // so its dimensions (w/h) are known. This is used for Chatbot's auto scroll.
79
+ dispatch("load");
80
+ }}
81
+ mirror={false}
82
+ {label}
83
+ {loop}
84
+ interactive={false}
85
+ {upload}
86
+ {i18n}
87
+ />
88
+ {/key}
89
+ <div data-testid="download-div">
90
+ <IconButtonWrapper
91
+ display_top_corner={display_icon_button_wrapper_top_corner}
92
+ >
93
+ {#if show_download_button}
94
+ <DownloadLink
95
+ href={value.is_stream
96
+ ? value.url?.replace("playlist.m3u8", "playlist-file")
97
+ : value.url}
98
+ download={value.orig_name || value.path}
99
+ >
100
+ <IconButton Icon={Download} label="Download" />
101
+ </DownloadLink>
102
+ {/if}
103
+ {#if show_share_button}
104
+ <ShareButton
105
+ {i18n}
106
+ on:error
107
+ on:share
108
+ {value}
109
+ formatter={async (value) => {
110
+ if (!value) return "";
111
+ let url = await uploadToHuggingFace(value.data, "url");
112
+ return url;
113
+ }}
114
+ />
115
+ {/if}
116
+ </IconButtonWrapper>
117
+ </div>
118
+ {/if}
6.0.1/video/shared/VideoTimeline.svelte ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, onDestroy } from "svelte";
3
+
4
+ export let videoElement: HTMLVideoElement;
5
+ export let trimmedDuration: number | null;
6
+ export let dragStart: number;
7
+ export let dragEnd: number;
8
+ export let loadingTimeline: boolean;
9
+
10
+ let thumbnails: string[] = [];
11
+ let numberOfThumbnails = 10;
12
+ let intervalId: ReturnType<typeof setInterval> | undefined;
13
+ let videoDuration: number;
14
+
15
+ let leftHandlePosition = 0;
16
+ let rightHandlePosition = 100;
17
+
18
+ let dragging: string | null = null;
19
+
20
+ const startDragging = (side: string | null): void => {
21
+ dragging = side;
22
+ };
23
+
24
+ $: loadingTimeline = thumbnails.length !== numberOfThumbnails;
25
+
26
+ const stopDragging = (): void => {
27
+ dragging = null;
28
+ };
29
+
30
+ const drag = (event: { clientX: number }, distance?: number): void => {
31
+ if (dragging) {
32
+ const timeline = document.getElementById("timeline");
33
+
34
+ if (!timeline) return;
35
+
36
+ const rect = timeline.getBoundingClientRect();
37
+ let newPercentage = ((event.clientX - rect.left) / rect.width) * 100;
38
+
39
+ if (distance) {
40
+ // Move handle based on arrow key press
41
+ newPercentage =
42
+ dragging === "left"
43
+ ? leftHandlePosition + distance
44
+ : rightHandlePosition + distance;
45
+ } else {
46
+ // Move handle based on mouse drag
47
+ newPercentage = ((event.clientX - rect.left) / rect.width) * 100;
48
+ }
49
+
50
+ newPercentage = Math.max(0, Math.min(newPercentage, 100)); // Keep within 0 and 100
51
+
52
+ if (dragging === "left") {
53
+ leftHandlePosition = Math.min(newPercentage, rightHandlePosition);
54
+
55
+ // Calculate the new time and set it for the videoElement
56
+ const newTimeLeft = (leftHandlePosition / 100) * videoDuration;
57
+ videoElement.currentTime = newTimeLeft;
58
+
59
+ dragStart = newTimeLeft;
60
+ } else if (dragging === "right") {
61
+ rightHandlePosition = Math.max(newPercentage, leftHandlePosition);
62
+
63
+ const newTimeRight = (rightHandlePosition / 100) * videoDuration;
64
+ videoElement.currentTime = newTimeRight;
65
+
66
+ dragEnd = newTimeRight;
67
+ }
68
+
69
+ const startTime = (leftHandlePosition / 100) * videoDuration;
70
+ const endTime = (rightHandlePosition / 100) * videoDuration;
71
+ trimmedDuration = endTime - startTime;
72
+
73
+ leftHandlePosition = leftHandlePosition;
74
+ rightHandlePosition = rightHandlePosition;
75
+ }
76
+ };
77
+
78
+ const moveHandle = (e: KeyboardEvent): void => {
79
+ if (dragging) {
80
+ // Calculate the movement distance as a percentage of the video duration
81
+ const distance = (1 / videoDuration) * 100;
82
+
83
+ if (e.key === "ArrowLeft") {
84
+ drag({ clientX: 0 }, -distance);
85
+ } else if (e.key === "ArrowRight") {
86
+ drag({ clientX: 0 }, distance);
87
+ }
88
+ }
89
+ };
90
+
91
+ const generateThumbnail = (): void => {
92
+ const canvas = document.createElement("canvas");
93
+ const ctx = canvas.getContext("2d");
94
+ if (!ctx) return;
95
+
96
+ canvas.width = videoElement.videoWidth;
97
+ canvas.height = videoElement.videoHeight;
98
+
99
+ ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
100
+
101
+ const thumbnail: string = canvas.toDataURL("image/jpeg", 0.7);
102
+ thumbnails = [...thumbnails, thumbnail];
103
+ };
104
+
105
+ onMount(() => {
106
+ const loadMetadata = (): void => {
107
+ videoDuration = videoElement.duration;
108
+
109
+ const interval = videoDuration / numberOfThumbnails;
110
+ let captures = 0;
111
+
112
+ const onSeeked = (): void => {
113
+ generateThumbnail();
114
+ captures++;
115
+
116
+ if (captures < numberOfThumbnails) {
117
+ videoElement.currentTime += interval;
118
+ } else {
119
+ videoElement.removeEventListener("seeked", onSeeked);
120
+ }
121
+ };
122
+
123
+ videoElement.addEventListener("seeked", onSeeked);
124
+ videoElement.currentTime = 0;
125
+ };
126
+
127
+ if (videoElement.readyState >= 1) {
128
+ loadMetadata();
129
+ } else {
130
+ videoElement.addEventListener("loadedmetadata", loadMetadata);
131
+ }
132
+ });
133
+
134
+ onDestroy(() => {
135
+ window.removeEventListener("mousemove", drag);
136
+ window.removeEventListener("mouseup", stopDragging);
137
+ window.removeEventListener("keydown", moveHandle);
138
+
139
+ if (intervalId !== undefined) {
140
+ clearInterval(intervalId);
141
+ }
142
+ });
143
+
144
+ onMount(() => {
145
+ window.addEventListener("mousemove", drag);
146
+ window.addEventListener("mouseup", stopDragging);
147
+ window.addEventListener("keydown", moveHandle);
148
+ });
149
+ </script>
150
+
151
+ <div class="container">
152
+ {#if loadingTimeline}
153
+ <div class="load-wrap">
154
+ <span aria-label="loading timeline" class="loader" />
155
+ </div>
156
+ {:else}
157
+ <div id="timeline" class="thumbnail-wrapper">
158
+ <button
159
+ aria-label="start drag handle for trimming video"
160
+ class="handle left"
161
+ on:mousedown={() => startDragging("left")}
162
+ on:blur={stopDragging}
163
+ on:keydown={(e) => {
164
+ if (e.key === "ArrowLeft" || e.key == "ArrowRight") {
165
+ startDragging("left");
166
+ }
167
+ }}
168
+ style="left: {leftHandlePosition}%;"
169
+ />
170
+
171
+ <div
172
+ class="opaque-layer"
173
+ style="left: {leftHandlePosition}%; right: {100 - rightHandlePosition}%"
174
+ />
175
+
176
+ {#each thumbnails as thumbnail, i (i)}
177
+ <img src={thumbnail} alt={`frame-${i}`} draggable="false" />
178
+ {/each}
179
+ <button
180
+ aria-label="end drag handle for trimming video"
181
+ class="handle right"
182
+ on:mousedown={() => startDragging("right")}
183
+ on:blur={stopDragging}
184
+ on:keydown={(e) => {
185
+ if (e.key === "ArrowLeft" || e.key == "ArrowRight") {
186
+ startDragging("right");
187
+ }
188
+ }}
189
+ style="left: {rightHandlePosition}%;"
190
+ />
191
+ </div>
192
+ {/if}
193
+ </div>
194
+
195
+ <style>
196
+ .load-wrap {
197
+ display: flex;
198
+ justify-content: center;
199
+ align-items: center;
200
+ height: 100%;
201
+ }
202
+ .loader {
203
+ display: flex;
204
+ position: relative;
205
+ background-color: var(--border-color-accent-subdued);
206
+ animation: shadowPulse 2s linear infinite;
207
+ box-shadow:
208
+ -24px 0 var(--border-color-accent-subdued),
209
+ 24px 0 var(--border-color-accent-subdued);
210
+ margin: var(--spacing-md);
211
+ border-radius: 50%;
212
+ width: 10px;
213
+ height: 10px;
214
+ scale: 0.5;
215
+ }
216
+
217
+ @keyframes shadowPulse {
218
+ 33% {
219
+ box-shadow:
220
+ -24px 0 var(--border-color-accent-subdued),
221
+ 24px 0 #fff;
222
+ background: #fff;
223
+ }
224
+ 66% {
225
+ box-shadow:
226
+ -24px 0 #fff,
227
+ 24px 0 #fff;
228
+ background: var(--border-color-accent-subdued);
229
+ }
230
+ 100% {
231
+ box-shadow:
232
+ -24px 0 #fff,
233
+ 24px 0 var(--border-color-accent-subdued);
234
+ background: #fff;
235
+ }
236
+ }
237
+
238
+ .container {
239
+ display: flex;
240
+ flex-direction: column;
241
+ align-items: center;
242
+ justify-content: center;
243
+ margin: var(--spacing-lg) var(--spacing-lg) 0 var(--spacing-lg);
244
+ }
245
+
246
+ #timeline {
247
+ display: flex;
248
+ height: var(--size-10);
249
+ flex: 1;
250
+ position: relative;
251
+ }
252
+
253
+ img {
254
+ flex: 1 1 auto;
255
+ min-width: 0;
256
+ object-fit: cover;
257
+ height: var(--size-12);
258
+ border: 1px solid var(--block-border-color);
259
+ user-select: none;
260
+ z-index: 1;
261
+ }
262
+
263
+ .handle {
264
+ width: 3px;
265
+ background-color: var(--color-accent);
266
+ cursor: ew-resize;
267
+ height: var(--size-12);
268
+ z-index: 3;
269
+ position: absolute;
270
+ }
271
+
272
+ .opaque-layer {
273
+ background-color: rgba(230, 103, 40, 0.25);
274
+ border: 1px solid var(--color-accent);
275
+ height: var(--size-12);
276
+ position: absolute;
277
+ z-index: 2;
278
+ }
279
+ </style>
6.0.1/video/shared/index.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export { default as Video } from "./Video.svelte";
6.0.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.0.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.0.1/video/types.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { LoadingStatus } from "@gradio/statustracker";
3
+ import type { WebcamOptions } from "./shared/utils";
4
+
5
+ export interface VideoProps {
6
+ value: FileData | null;
7
+ height: number | undefined;
8
+ width: number | undefined;
9
+ autoplay: boolean;
10
+ buttons: ("share" | "download" | "fullscreen")[];
11
+ sources:
12
+ | ["webcam"]
13
+ | ["upload"]
14
+ | ["webcam", "upload"]
15
+ | ["upload", "webcam"];
16
+ webcam_options: WebcamOptions;
17
+ include_audio: boolean;
18
+ loop: boolean;
19
+ webcam_constraints: object;
20
+ subtitles: FileData | null;
21
+ }
22
+
23
+ export interface VideoEvents {
24
+ change: never;
25
+ clear: never;
26
+ play: never;
27
+ pause: never;
28
+ upload: never;
29
+ stop: never;
30
+ end: never;
31
+ start_recording: never;
32
+ stop_recording: never;
33
+ clear_status: LoadingStatus;
34
+ share: any;
35
+ error: any;
36
+ warning: any;
37
+ }