@gradio/video 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @gradio/video
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Features
6
+
7
+ - [#6726](https://github.com/gradio-app/gradio/pull/6726) [`21cfb0a`](https://github.com/gradio-app/gradio/commit/21cfb0acc309bb1a392f4d8a8e42f6be864c5978) - Remove the styles from the Image/Video primitive components and Fix the container styles. Thanks [@whitphx](https://github.com/whitphx)!
8
+ - [#6398](https://github.com/gradio-app/gradio/pull/6398) [`67ddd40`](https://github.com/gradio-app/gradio/commit/67ddd40b4b70d3a37cb1637c33620f8d197dbee0) - Lite v4. Thanks [@whitphx](https://github.com/whitphx)!
9
+
10
+ ### Fixes
11
+
12
+ - [#6698](https://github.com/gradio-app/gradio/pull/6698) [`798eca5`](https://github.com/gradio-app/gradio/commit/798eca524d44289c536c47eec7c4fdce9fe81905) - Fit video media within Video component. Thanks [@hannahblair](https://github.com/hannahblair)!
13
+
14
+ ## 0.1.9
15
+
16
+ ### Fixes
17
+
18
+ - [#6566](https://github.com/gradio-app/gradio/pull/6566) [`d548202`](https://github.com/gradio-app/gradio/commit/d548202d2b5bd8a99e3ebc5bf56820b0282ce0f5) - Improve video trimming and error handling. Thanks [@hannahblair](https://github.com/hannahblair)!
19
+
3
20
  ## 0.1.8
4
21
 
5
22
  ### Patch Changes
@@ -228,4 +245,4 @@ Thanks [@pngwn](https://github.com/pngwn)!
228
245
  - @gradio/image@0.1.0
229
246
  - @gradio/utils@0.0.2
230
247
  - @gradio/atoms@0.0.2
231
- - @gradio/upload@0.0.2
248
+ - @gradio/upload@0.0.2
package/Example.svelte CHANGED
@@ -43,10 +43,13 @@
43
43
  <style>
44
44
  .container {
45
45
  flex: none;
46
- border: 2px solid var(--border-color-primary);
47
- border-radius: var(--radius-lg);
48
46
  max-width: none;
49
47
  }
48
+ .container :global(video) {
49
+ width: var(--size-full);
50
+ height: var(--size-full);
51
+ object-fit: cover;
52
+ }
50
53
 
51
54
  .container:hover,
52
55
  .container.selected {
@@ -54,6 +57,9 @@
54
57
  }
55
58
  .container.table {
56
59
  margin: 0 auto;
60
+ border: 2px solid var(--border-color-primary);
61
+ border-radius: var(--radius-lg);
62
+ overflow: hidden;
57
63
  width: var(--size-20);
58
64
  height: var(--size-20);
59
65
  object-fit: cover;
@@ -3,30 +3,14 @@
3
3
  import Video from "./Index.svelte";
4
4
  import { format } from "svelte-i18n";
5
5
  import { get } from "svelte/store";
6
+ import { userEvent, within } from "@storybook/testing-library";
6
7
  </script>
7
8
 
8
- <Meta
9
- title="Components/Video"
10
- component={Video}
11
- argTypes={{
12
- video: {
13
- control: "text",
14
- description:
15
- "A path or URL for the default value that Video component is going to take. Can also be a tuple consisting of (video filepath, subtitle filepath). If a subtitle file is provided, it should be of type .srt or .vtt. Or can be callable, in which case the function will be called whenever the app loads to set the initial value of the component.",
16
- name: "value"
17
- },
18
- autoplay: {
19
- control: [true, false],
20
- description: "Whether to autoplay the video on load",
21
- name: "autoplay",
22
- value: true
23
- }
24
- }}
25
- />
9
+ <Meta title="Components/Video" component={Video} />
26
10
 
27
11
  <div>
28
12
  <Template let:args>
29
- <Video {...args} i18n={get(format)} />
13
+ <Video i18n={get(format)} {...args} />
30
14
  </Template>
31
15
  </div>
32
16
 
@@ -59,6 +43,23 @@
59
43
  width: 400
60
44
  }}
61
45
  />
46
+ <Story
47
+ name="Static video with vertical video"
48
+ args={{
49
+ value: {
50
+ video: {
51
+ path: "https://gradio-static-files.s3.us-west-2.amazonaws.com/world_vertical.mp4",
52
+ url: "https://gradio-static-files.s3.us-west-2.amazonaws.com/world_vertical.mp4",
53
+ orig_name: "world_vertical.mp4"
54
+ }
55
+ },
56
+ label: "world video",
57
+ show_label: true,
58
+ interactive: false,
59
+ height: 200,
60
+ width: 400
61
+ }}
62
+ />
62
63
 
63
64
  <Story
64
65
  name="Upload video"
@@ -72,3 +73,26 @@
72
73
  value: null
73
74
  }}
74
75
  />
76
+
77
+ <Story
78
+ name="Trim video"
79
+ args={{
80
+ value: {
81
+ video: {
82
+ path: "https://gradio-static-files.s3.us-west-2.amazonaws.com/world.mp4",
83
+ url: "https://gradio-static-files.s3.us-west-2.amazonaws.com/world.mp4",
84
+ orig_name: "world.mp4"
85
+ }
86
+ },
87
+ label: "world video",
88
+ show_label: true,
89
+ interactive: "true",
90
+ sources: ["upload"],
91
+ width: 400
92
+ }}
93
+ play={async ({ canvasElement }) => {
94
+ const canvas = within(canvasElement);
95
+ const trimButton = canvas.getByLabelText("Trim video to selection");
96
+ userEvent.click(trimButton);
97
+ }}
98
+ />
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/video",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "description": "Gradio UI packages",
5
5
  "type": "module",
6
6
  "author": "",
@@ -9,14 +9,14 @@
9
9
  "dependencies": {
10
10
  "@ffmpeg/ffmpeg": "^0.12.7",
11
11
  "@ffmpeg/util": "^0.12.1",
12
- "@gradio/atoms": "^0.3.0",
13
- "@gradio/client": "^0.8.2",
14
- "@gradio/icons": "^0.3.0",
15
- "@gradio/statustracker": "^0.4.0",
16
- "@gradio/image": "^0.4.1",
17
- "@gradio/upload": "^0.5.1",
12
+ "@gradio/atoms": "^0.4.0",
13
+ "@gradio/client": "^0.9.0",
14
+ "@gradio/icons": "^0.3.2",
15
+ "@gradio/statustracker": "^0.4.2",
16
+ "@gradio/image": "^0.5.0",
17
+ "@gradio/upload": "^0.5.3",
18
18
  "@gradio/utils": "^0.2.0",
19
- "@gradio/wasm": "^0.3.0"
19
+ "@gradio/wasm": "^0.4.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "mrmime": "^1.0.1"
@@ -99,7 +99,7 @@
99
99
  </script>
100
100
 
101
101
  <div class="wrap">
102
- <div class:mirror>
102
+ <div class="mirror-wrap" class:mirror>
103
103
  <Video
104
104
  {src}
105
105
  preload="auto"
@@ -199,6 +199,12 @@
199
199
  transform: scaleX(-1);
200
200
  }
201
201
 
202
+ .mirror-wrap {
203
+ position: relative;
204
+ height: 100%;
205
+ width: 100%;
206
+ }
207
+
202
208
  .controls {
203
209
  position: absolute;
204
210
  bottom: 0;
@@ -248,4 +254,8 @@
248
254
  width: var(--size-full);
249
255
  border-radius: var(--radius-xl);
250
256
  }
257
+ .wrap :global(video) {
258
+ height: var(--size-full);
259
+ width: var(--size-full);
260
+ }
251
261
  </style>
@@ -22,50 +22,69 @@
22
22
  export let processingVideo = false;
23
23
 
24
24
  const dispatch = createEventDispatcher();
25
+
26
+ let resolved_src: typeof src;
27
+
28
+ // The `src` prop can be updated before the Promise from `resolve_wasm_src` is resolved.
29
+ // In such a case, the resolved value for the old `src` has to be discarded,
30
+ // This variable `latest_src` is used to pick up only the value resolved for the latest `src` prop.
31
+ let latest_src: typeof src;
32
+ $: {
33
+ // In normal (non-Wasm) Gradio, the `<img>` element should be rendered with the passed `src` props immediately
34
+ // without waiting for `resolve_wasm_src()` to resolve.
35
+ // If it waits, a black image is displayed until the async task finishes
36
+ // and it leads to undesirable flickering.
37
+ // So set `src` to `resolved_src` here.
38
+ resolved_src = src;
39
+
40
+ latest_src = src;
41
+ const resolving_src = src;
42
+ resolve_wasm_src(resolving_src).then((s) => {
43
+ if (latest_src === resolving_src) {
44
+ resolved_src = s;
45
+ }
46
+ });
47
+ }
25
48
  </script>
26
49
 
27
- {#await resolve_wasm_src(src) then resolved_src}
28
- <!--
29
- The spread operator with `$$props` or `$$restProps` can't be used here
30
- to pass props from the parent component to the <video> element
31
- because of its unexpected behavior: https://github.com/sveltejs/svelte/issues/7404
32
- 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.
33
- 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.
50
+ <!--
51
+ The spread operator with `$$props` or `$$restProps` can't be used here
52
+ to pass props from the parent component to the <video> element
53
+ because of its unexpected behavior: https://github.com/sveltejs/svelte/issues/7404
54
+ 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.
55
+ 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.
34
56
  -->
35
- <div class:hidden={!processingVideo} class="overlay">
36
- <span class="load-wrap">
37
- <span class="loader" />
38
- </span>
39
- </div>
40
- <video
41
- src={resolved_src}
42
- {muted}
43
- {playsinline}
44
- {preload}
45
- {autoplay}
46
- {controls}
47
- on:loadeddata={dispatch.bind(null, "loadeddata")}
48
- on:click={dispatch.bind(null, "click")}
49
- on:play={dispatch.bind(null, "play")}
50
- on:pause={dispatch.bind(null, "pause")}
51
- on:ended={dispatch.bind(null, "ended")}
52
- on:mouseover={dispatch.bind(null, "mouseover")}
53
- on:mouseout={dispatch.bind(null, "mouseout")}
54
- on:focus={dispatch.bind(null, "focus")}
55
- on:blur={dispatch.bind(null, "blur")}
56
- bind:currentTime
57
- bind:duration
58
- bind:paused
59
- bind:this={node}
60
- use:loaded={{ autoplay: autoplay ?? false }}
61
- data-testid={$$props["data-testid"]}
62
- crossorigin="anonymous"
63
- >
64
- <slot />
65
- </video>
66
- {:catch error}
67
- <p style="color: red;">{error.message}</p>
68
- {/await}
57
+ <div class:hidden={!processingVideo} class="overlay">
58
+ <span class="load-wrap">
59
+ <span class="loader" />
60
+ </span>
61
+ </div>
62
+ <video
63
+ src={resolved_src}
64
+ {muted}
65
+ {playsinline}
66
+ {preload}
67
+ {autoplay}
68
+ {controls}
69
+ on:loadeddata={dispatch.bind(null, "loadeddata")}
70
+ on:click={dispatch.bind(null, "click")}
71
+ on:play={dispatch.bind(null, "play")}
72
+ on:pause={dispatch.bind(null, "pause")}
73
+ on:ended={dispatch.bind(null, "ended")}
74
+ on:mouseover={dispatch.bind(null, "mouseover")}
75
+ on:mouseout={dispatch.bind(null, "mouseout")}
76
+ on:focus={dispatch.bind(null, "focus")}
77
+ on:blur={dispatch.bind(null, "blur")}
78
+ bind:currentTime
79
+ bind:duration
80
+ bind:paused
81
+ bind:this={node}
82
+ use:loaded={{ autoplay: autoplay ?? false }}
83
+ data-testid={$$props["data-testid"]}
84
+ crossorigin="anonymous"
85
+ >
86
+ <slot />
87
+ </video>
69
88
 
70
89
  <style>
71
90
  .overlay {
@@ -121,13 +140,4 @@
121
140
  background: #fff;
122
141
  }
123
142
  }
124
-
125
- video {
126
- position: inherit;
127
- background-color: black;
128
- width: var(--size-full);
129
- height: var(--size-full);
130
- object-fit: contain;
131
- border-radius: var(--radius-xl);
132
- }
133
143
  </style>
@@ -28,6 +28,11 @@
28
28
  const minutes = Math.floor(seconds / 60);
29
29
  const secondsRemainder = Math.round(seconds) % 60;
30
30
  const paddedSeconds = `0${secondsRemainder}`.slice(-2);
31
+
32
+ if (Number.isNaN(minutes) || Number.isNaN(secondsRemainder)) {
33
+ return "00:00";
34
+ }
35
+
31
36
  return `${minutes}:${paddedSeconds}`;
32
37
  };
33
38
 
@@ -4,6 +4,7 @@
4
4
  import type { FileData } from "@gradio/client";
5
5
  import { Video, Download } from "@gradio/icons";
6
6
  import { uploadToHuggingFace } from "@gradio/utils";
7
+ import { DownloadLink } from "@gradio/wasm/svelte";
7
8
 
8
9
  import Player from "./Player.svelte";
9
10
  import type { I18nFormatter } from "js/app/src/gradio_helper";
@@ -67,13 +68,9 @@
67
68
  {/key}
68
69
  <div class="icon-buttons" data-testid="download-div">
69
70
  {#if show_download_button}
70
- <a
71
- href={value.url}
72
- target={window.__is_colab__ ? "_blank" : null}
73
- download={value.orig_name || value.path}
74
- >
71
+ <DownloadLink href={value.url} download={value.orig_name || value.path}>
75
72
  <IconButton Icon={Download} label="Download" />
76
- </a>
73
+ </DownloadLink>
77
74
  {/if}
78
75
  {#if show_share_button}
79
76
  <ShareButton
@@ -156,6 +156,7 @@
156
156
  {:else}
157
157
  <div id="timeline" class="thumbnail-wrapper">
158
158
  <button
159
+ aria-label="start drag handle for trimming video"
159
160
  class="handle left"
160
161
  on:mousedown={() => startDragging("left")}
161
162
  on:blur={stopDragging}
@@ -176,6 +177,7 @@
176
177
  <img src={thumbnail} alt={`frame-${i}`} draggable="false" />
177
178
  {/each}
178
179
  <button
180
+ aria-label="end drag handle for trimming video"
179
181
  class="handle right"
180
182
  on:mousedown={() => startDragging("right")}
181
183
  on:blur={stopDragging}
package/shared/utils.ts CHANGED
@@ -66,15 +66,19 @@ export async function trimVideo(
66
66
  endTime: number,
67
67
  videoElement: HTMLVideoElement
68
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
+
69
78
  try {
70
- const videoUrl = videoElement.src;
71
- const mimeType = lookup(videoElement.src) || "video/mp4";
72
- const blobUrl = await toBlobURL(videoUrl, mimeType);
73
- const response = await fetch(blobUrl);
74
- const vidBlob = await response.blob();
75
- const type = getVideoExtensionFromMimeType(mimeType) || "mp4";
76
- const inputName = `input.${type}`;
77
- const outputName = `output.${type}`;
79
+ if (startTime === 0 && endTime === 0) {
80
+ return vidBlob;
81
+ }
78
82
 
79
83
  await ffmpeg.writeFile(
80
84
  inputName,
@@ -84,10 +88,8 @@ export async function trimVideo(
84
88
  let command = [
85
89
  "-i",
86
90
  inputName,
87
- "-ss",
88
- startTime.toString(),
89
- "-to",
90
- endTime.toString(),
91
+ ...(startTime !== 0 ? ["-ss", startTime.toString()] : []),
92
+ ...(endTime !== 0 ? ["-to", endTime.toString()] : []),
91
93
  "-c:a",
92
94
  "copy",
93
95
  outputName
@@ -102,6 +104,7 @@ export async function trimVideo(
102
104
  return outputBlob;
103
105
  } catch (error) {
104
106
  console.error("Error initializing FFmpeg:", error);
107
+ return vidBlob;
105
108
  }
106
109
  }
107
110