Upload folder using huggingface_hub
Browse files- 5.49.1/video/Example.svelte +78 -0
- 5.49.1/video/Index.svelte +223 -0
- 5.49.1/video/index.ts +7 -0
- 5.49.1/video/package.json +58 -0
- 5.49.1/video/shared/InteractiveVideo.svelte +176 -0
- 5.49.1/video/shared/Player.svelte +276 -0
- 5.49.1/video/shared/Video.svelte +178 -0
- 5.49.1/video/shared/VideoControls.svelte +202 -0
- 5.49.1/video/shared/VideoPreview.svelte +118 -0
- 5.49.1/video/shared/VideoTimeline.svelte +279 -0
- 5.49.1/video/shared/index.ts +1 -0
- 5.49.1/video/shared/utils.ts +151 -0
5.49.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: { video: FileData; subtitles: FileData | null } | 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?.video.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>
|
5.49.1/video/Index.svelte
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script lang="ts">
|
| 4 |
+
import type { Gradio, ShareData } from "@gradio/utils";
|
| 5 |
+
|
| 6 |
+
import type { FileData } from "@gradio/client";
|
| 7 |
+
import { Block, UploadText } from "@gradio/atoms";
|
| 8 |
+
import StaticVideo from "./shared/VideoPreview.svelte";
|
| 9 |
+
import Video from "./shared/InteractiveVideo.svelte";
|
| 10 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 11 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
| 12 |
+
import type { WebcamOptions } from "./shared/utils";
|
| 13 |
+
export let elem_id = "";
|
| 14 |
+
export let elem_classes: string[] = [];
|
| 15 |
+
export let visible: boolean | "hidden" = true;
|
| 16 |
+
export let value: { video: FileData; subtitles: FileData | null } | null =
|
| 17 |
+
null;
|
| 18 |
+
let old_value: { video: FileData; subtitles: FileData | null } | null = null;
|
| 19 |
+
|
| 20 |
+
export let label: string;
|
| 21 |
+
export let sources:
|
| 22 |
+
| ["webcam"]
|
| 23 |
+
| ["upload"]
|
| 24 |
+
| ["webcam", "upload"]
|
| 25 |
+
| ["upload", "webcam"];
|
| 26 |
+
export let root: string;
|
| 27 |
+
export let show_label: boolean;
|
| 28 |
+
export let loading_status: LoadingStatus;
|
| 29 |
+
export let height: number | undefined;
|
| 30 |
+
export let width: number | undefined;
|
| 31 |
+
|
| 32 |
+
export let container = false;
|
| 33 |
+
export let scale: number | null = null;
|
| 34 |
+
export let min_width: number | undefined = undefined;
|
| 35 |
+
export let autoplay = false;
|
| 36 |
+
export let show_share_button = true;
|
| 37 |
+
export let show_download_button: boolean;
|
| 38 |
+
export let gradio: Gradio<{
|
| 39 |
+
change: never;
|
| 40 |
+
clear: never;
|
| 41 |
+
play: never;
|
| 42 |
+
pause: never;
|
| 43 |
+
upload: never;
|
| 44 |
+
stop: never;
|
| 45 |
+
end: never;
|
| 46 |
+
start_recording: never;
|
| 47 |
+
stop_recording: never;
|
| 48 |
+
share: ShareData;
|
| 49 |
+
error: string;
|
| 50 |
+
warning: string;
|
| 51 |
+
clear_status: LoadingStatus;
|
| 52 |
+
}>;
|
| 53 |
+
export let interactive: boolean;
|
| 54 |
+
export let webcam_options: WebcamOptions;
|
| 55 |
+
export let include_audio: boolean;
|
| 56 |
+
export let loop = false;
|
| 57 |
+
export let input_ready: boolean;
|
| 58 |
+
let uploading = false;
|
| 59 |
+
$: input_ready = !uploading;
|
| 60 |
+
|
| 61 |
+
let _video: FileData | null = null;
|
| 62 |
+
let _subtitle: FileData | null = null;
|
| 63 |
+
|
| 64 |
+
let active_source: "webcam" | "upload";
|
| 65 |
+
|
| 66 |
+
let initial_value: { video: FileData; subtitles: FileData | null } | null =
|
| 67 |
+
value;
|
| 68 |
+
|
| 69 |
+
$: if (value && initial_value === null) {
|
| 70 |
+
initial_value = value;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
const handle_reset_value = (): void => {
|
| 74 |
+
if (initial_value === null || value === initial_value) {
|
| 75 |
+
return;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
value = initial_value;
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
$: if (sources && !active_source) {
|
| 82 |
+
active_source = sources[0];
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
$: {
|
| 86 |
+
if (value != null) {
|
| 87 |
+
_video = value.video;
|
| 88 |
+
_subtitle = value.subtitles;
|
| 89 |
+
} else {
|
| 90 |
+
_video = null;
|
| 91 |
+
_subtitle = null;
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
let dragging = false;
|
| 96 |
+
|
| 97 |
+
$: {
|
| 98 |
+
if (JSON.stringify(value) !== JSON.stringify(old_value)) {
|
| 99 |
+
old_value = value;
|
| 100 |
+
gradio.dispatch("change");
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
function handle_change({ detail }: CustomEvent<FileData | null>): void {
|
| 105 |
+
if (detail != null) {
|
| 106 |
+
value = { video: detail, subtitles: null } as {
|
| 107 |
+
video: FileData;
|
| 108 |
+
subtitles: FileData | null;
|
| 109 |
+
} | null;
|
| 110 |
+
} else {
|
| 111 |
+
value = null;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
function handle_error({ detail }: CustomEvent<string>): void {
|
| 116 |
+
const [level, status] = detail.includes("Invalid file type")
|
| 117 |
+
? ["warning", "complete"]
|
| 118 |
+
: ["error", "error"];
|
| 119 |
+
loading_status = loading_status || {};
|
| 120 |
+
loading_status.status = status as LoadingStatus["status"];
|
| 121 |
+
loading_status.message = detail;
|
| 122 |
+
gradio.dispatch(level as "error" | "warning", detail);
|
| 123 |
+
}
|
| 124 |
+
</script>
|
| 125 |
+
|
| 126 |
+
{#if !interactive}
|
| 127 |
+
<Block
|
| 128 |
+
{visible}
|
| 129 |
+
variant={value === null && active_source === "upload" ? "dashed" : "solid"}
|
| 130 |
+
border_mode={dragging ? "focus" : "base"}
|
| 131 |
+
padding={false}
|
| 132 |
+
{elem_id}
|
| 133 |
+
{elem_classes}
|
| 134 |
+
{height}
|
| 135 |
+
{width}
|
| 136 |
+
{container}
|
| 137 |
+
{scale}
|
| 138 |
+
{min_width}
|
| 139 |
+
allow_overflow={false}
|
| 140 |
+
>
|
| 141 |
+
<StatusTracker
|
| 142 |
+
autoscroll={gradio.autoscroll}
|
| 143 |
+
i18n={gradio.i18n}
|
| 144 |
+
{...loading_status}
|
| 145 |
+
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
|
| 146 |
+
/>
|
| 147 |
+
|
| 148 |
+
<StaticVideo
|
| 149 |
+
value={_video}
|
| 150 |
+
subtitle={_subtitle}
|
| 151 |
+
{label}
|
| 152 |
+
{show_label}
|
| 153 |
+
{autoplay}
|
| 154 |
+
{loop}
|
| 155 |
+
{show_share_button}
|
| 156 |
+
{show_download_button}
|
| 157 |
+
on:play={() => gradio.dispatch("play")}
|
| 158 |
+
on:pause={() => gradio.dispatch("pause")}
|
| 159 |
+
on:stop={() => gradio.dispatch("stop")}
|
| 160 |
+
on:end={() => gradio.dispatch("end")}
|
| 161 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 162 |
+
on:error={({ detail }) => gradio.dispatch("error", detail)}
|
| 163 |
+
i18n={gradio.i18n}
|
| 164 |
+
upload={(...args) => gradio.client.upload(...args)}
|
| 165 |
+
/>
|
| 166 |
+
</Block>
|
| 167 |
+
{:else}
|
| 168 |
+
<Block
|
| 169 |
+
{visible}
|
| 170 |
+
variant={value === null && active_source === "upload" ? "dashed" : "solid"}
|
| 171 |
+
border_mode={dragging ? "focus" : "base"}
|
| 172 |
+
padding={false}
|
| 173 |
+
{elem_id}
|
| 174 |
+
{elem_classes}
|
| 175 |
+
{height}
|
| 176 |
+
{width}
|
| 177 |
+
{container}
|
| 178 |
+
{scale}
|
| 179 |
+
{min_width}
|
| 180 |
+
allow_overflow={false}
|
| 181 |
+
>
|
| 182 |
+
<StatusTracker
|
| 183 |
+
autoscroll={gradio.autoscroll}
|
| 184 |
+
i18n={gradio.i18n}
|
| 185 |
+
{...loading_status}
|
| 186 |
+
on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
|
| 187 |
+
/>
|
| 188 |
+
|
| 189 |
+
<Video
|
| 190 |
+
value={_video}
|
| 191 |
+
subtitle={_subtitle}
|
| 192 |
+
on:change={handle_change}
|
| 193 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 194 |
+
on:error={handle_error}
|
| 195 |
+
bind:uploading
|
| 196 |
+
{label}
|
| 197 |
+
{show_label}
|
| 198 |
+
{show_download_button}
|
| 199 |
+
{sources}
|
| 200 |
+
{active_source}
|
| 201 |
+
{webcam_options}
|
| 202 |
+
{include_audio}
|
| 203 |
+
{autoplay}
|
| 204 |
+
{root}
|
| 205 |
+
{loop}
|
| 206 |
+
{handle_reset_value}
|
| 207 |
+
on:clear={() => gradio.dispatch("clear")}
|
| 208 |
+
on:play={() => gradio.dispatch("play")}
|
| 209 |
+
on:pause={() => gradio.dispatch("pause")}
|
| 210 |
+
on:upload={() => gradio.dispatch("upload")}
|
| 211 |
+
on:stop={() => gradio.dispatch("stop")}
|
| 212 |
+
on:end={() => gradio.dispatch("end")}
|
| 213 |
+
on:start_recording={() => gradio.dispatch("start_recording")}
|
| 214 |
+
on:stop_recording={() => gradio.dispatch("stop_recording")}
|
| 215 |
+
i18n={gradio.i18n}
|
| 216 |
+
max_file_size={gradio.max_file_size}
|
| 217 |
+
upload={(...args) => gradio.client.upload(...args)}
|
| 218 |
+
stream_handler={(...args) => gradio.client.stream(...args)}
|
| 219 |
+
>
|
| 220 |
+
<UploadText i18n={gradio.i18n} type="video" />
|
| 221 |
+
</Video>
|
| 222 |
+
</Block>
|
| 223 |
+
{/if}
|
5.49.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;
|
5.49.1/video/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/video",
|
| 3 |
+
"version": "0.16.0",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"@ffmpeg/ffmpeg": "^0.12.7",
|
| 11 |
+
"@ffmpeg/util": "^0.12.1",
|
| 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.5.13",
|
| 20 |
+
"mrmime": "^2.0.0"
|
| 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": "^4.0.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 |
+
}
|
5.49.1/video/shared/InteractiveVideo.svelte
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 37 |
+
let has_change_history = false;
|
| 38 |
+
|
| 39 |
+
const dispatch = createEventDispatcher<{
|
| 40 |
+
change: FileData | null;
|
| 41 |
+
clear?: never;
|
| 42 |
+
play?: never;
|
| 43 |
+
pause?: never;
|
| 44 |
+
end?: never;
|
| 45 |
+
drag: boolean;
|
| 46 |
+
error: string;
|
| 47 |
+
upload: FileData;
|
| 48 |
+
start_recording?: never;
|
| 49 |
+
stop_recording?: never;
|
| 50 |
+
}>();
|
| 51 |
+
|
| 52 |
+
function handle_load({ detail }: CustomEvent<FileData | null>): void {
|
| 53 |
+
value = detail;
|
| 54 |
+
dispatch("change", detail);
|
| 55 |
+
dispatch("upload", detail!);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
function handle_clear(): void {
|
| 59 |
+
value = null;
|
| 60 |
+
dispatch("change", null);
|
| 61 |
+
dispatch("clear");
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function handle_change(video: FileData): void {
|
| 65 |
+
has_change_history = true;
|
| 66 |
+
dispatch("change", video);
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
function handle_capture({
|
| 70 |
+
detail
|
| 71 |
+
}: CustomEvent<FileData | any | null>): void {
|
| 72 |
+
dispatch("change", detail);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
let dragging = false;
|
| 76 |
+
$: dispatch("drag", dragging);
|
| 77 |
+
</script>
|
| 78 |
+
|
| 79 |
+
<BlockLabel {show_label} Icon={Video} label={label || "Video"} />
|
| 80 |
+
<div data-testid="video" class="video-container">
|
| 81 |
+
{#if value === null || value.url === undefined}
|
| 82 |
+
<div class="upload-container">
|
| 83 |
+
{#if active_source === "upload"}
|
| 84 |
+
<Upload
|
| 85 |
+
bind:dragging
|
| 86 |
+
bind:uploading
|
| 87 |
+
filetype="video/x-m4v,video/*"
|
| 88 |
+
on:load={handle_load}
|
| 89 |
+
{max_file_size}
|
| 90 |
+
on:error={({ detail }) => dispatch("error", detail)}
|
| 91 |
+
{root}
|
| 92 |
+
{upload}
|
| 93 |
+
{stream_handler}
|
| 94 |
+
aria_label={i18n("video.drop_to_upload")}
|
| 95 |
+
>
|
| 96 |
+
<slot />
|
| 97 |
+
</Upload>
|
| 98 |
+
{:else if active_source === "webcam"}
|
| 99 |
+
<Webcam
|
| 100 |
+
{root}
|
| 101 |
+
mirror_webcam={webcam_options.mirror}
|
| 102 |
+
webcam_constraints={webcam_options.constraints}
|
| 103 |
+
{include_audio}
|
| 104 |
+
mode="video"
|
| 105 |
+
on:error
|
| 106 |
+
on:capture={handle_capture}
|
| 107 |
+
on:start_recording
|
| 108 |
+
on:stop_recording
|
| 109 |
+
{i18n}
|
| 110 |
+
{upload}
|
| 111 |
+
stream_every={1}
|
| 112 |
+
/>
|
| 113 |
+
{/if}
|
| 114 |
+
</div>
|
| 115 |
+
{:else if value?.url}
|
| 116 |
+
{#key value?.url}
|
| 117 |
+
<Player
|
| 118 |
+
{upload}
|
| 119 |
+
{root}
|
| 120 |
+
interactive
|
| 121 |
+
{autoplay}
|
| 122 |
+
src={value.url}
|
| 123 |
+
subtitle={subtitle?.url}
|
| 124 |
+
is_stream={false}
|
| 125 |
+
on:play
|
| 126 |
+
on:pause
|
| 127 |
+
on:stop
|
| 128 |
+
on:end
|
| 129 |
+
on:error
|
| 130 |
+
mirror={webcam_options.mirror && active_source === "webcam"}
|
| 131 |
+
{label}
|
| 132 |
+
{handle_change}
|
| 133 |
+
{handle_reset_value}
|
| 134 |
+
{loop}
|
| 135 |
+
{value}
|
| 136 |
+
{i18n}
|
| 137 |
+
{show_download_button}
|
| 138 |
+
{handle_clear}
|
| 139 |
+
{has_change_history}
|
| 140 |
+
/>
|
| 141 |
+
{/key}
|
| 142 |
+
{:else if value.size}
|
| 143 |
+
<div class="file-name">{value.orig_name || value.url}</div>
|
| 144 |
+
<div class="file-size">
|
| 145 |
+
{prettyBytes(value.size)}
|
| 146 |
+
</div>
|
| 147 |
+
{/if}
|
| 148 |
+
|
| 149 |
+
<SelectSource {sources} bind:active_source {handle_clear} />
|
| 150 |
+
</div>
|
| 151 |
+
|
| 152 |
+
<style>
|
| 153 |
+
.file-name {
|
| 154 |
+
padding: var(--size-6);
|
| 155 |
+
font-size: var(--text-xxl);
|
| 156 |
+
word-break: break-all;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.file-size {
|
| 160 |
+
padding: var(--size-2);
|
| 161 |
+
font-size: var(--text-xl);
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.upload-container {
|
| 165 |
+
height: 100%;
|
| 166 |
+
width: 100%;
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
.video-container {
|
| 170 |
+
display: flex;
|
| 171 |
+
height: 100%;
|
| 172 |
+
flex-direction: column;
|
| 173 |
+
justify-content: center;
|
| 174 |
+
align-items: center;
|
| 175 |
+
}
|
| 176 |
+
</style>
|
5.49.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>
|
5.49.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>
|
5.49.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>
|
5.49.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}
|
5.49.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>
|
5.49.1/video/shared/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
export { default as Video } from "./Video.svelte";
|
5.49.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 |
+
}
|