forked from gradio-app/gradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoControls.svelte
208 lines (185 loc) 路 4.07 KB
/
VideoControls.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<script lang="ts">
import { Undo, Trim } from "@gradio/icons";
import VideoTimeline from "./VideoTimeline.svelte";
import { trimVideo } from "./utils";
import { FFmpeg } from "@ffmpeg/ffmpeg";
import loadFfmpeg from "./utils";
import { onMount } from "svelte";
import { format_time } from "@gradio/utils";
export let videoElement: HTMLVideoElement;
export let showRedo = false;
export let interactive = true;
export let mode = "";
export let handle_reset_value: () => void;
export let handle_trim_video: (videoBlob: Blob) => void;
export let processingVideo = false;
let ffmpeg: FFmpeg;
onMount(async () => {
ffmpeg = await loadFfmpeg();
});
$: if (mode === "edit" && trimmedDuration === null && videoElement)
trimmedDuration = videoElement.duration;
let trimmedDuration: number | null = null;
let dragStart = 0;
let dragEnd = 0;
let loadingTimeline = false;
const toggleTrimmingMode = (): void => {
if (mode === "edit") {
mode = "";
trimmedDuration = videoElement.duration;
} else {
mode = "edit";
}
};
</script>
<div class="container">
{#if mode === "edit"}
<div class="timeline-wrapper">
<VideoTimeline
{videoElement}
bind:dragStart
bind:dragEnd
bind:trimmedDuration
bind:loadingTimeline
/>
</div>
{/if}
<div class="controls" data-testid="waveform-controls">
{#if mode === "edit" && trimmedDuration !== null}
<time
aria-label="duration of selected region in seconds"
class:hidden={loadingTimeline}>{format_time(trimmedDuration)}</time
>
{:else}
<div />
{/if}
<div class="settings-wrapper">
{#if showRedo && mode === ""}
<button
class="action icon"
disabled={processingVideo}
aria-label="Reset video to initial value"
on:click={() => {
handle_reset_value();
mode = "";
}}
>
<Undo />
</button>
{/if}
{#if interactive}
{#if mode === ""}
<button
disabled={processingVideo}
class="action icon"
aria-label="Trim video to selection"
on:click={toggleTrimmingMode}
>
<Trim />
</button>
{:else}
<button
class:hidden={loadingTimeline}
class="text-button"
on:click={() => {
mode = "";
processingVideo = true;
trimVideo(ffmpeg, dragStart, dragEnd, videoElement)
.then((videoBlob) => {
handle_trim_video(videoBlob);
})
.then(() => {
processingVideo = false;
});
}}>Trim</button
>
<button
class="text-button"
class:hidden={loadingTimeline}
on:click={toggleTrimmingMode}>Cancel</button
>
{/if}
{/if}
</div>
</div>
</div>
<style>
.container {
width: 100%;
}
time {
color: var(--color-accent);
font-weight: bold;
padding-left: var(--spacing-xs);
}
.timeline-wrapper {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.settings-wrapper {
display: flex;
justify-self: self-end;
}
.text-button {
border: 1px solid var(--neutral-400);
border-radius: var(--radius-sm);
font-weight: 300;
font-size: var(--size-3);
text-align: center;
color: var(--neutral-400);
height: var(--size-5);
font-weight: bold;
padding: 0 5px;
margin-left: 5px;
}
.hidden {
display: none;
}
.text-button:hover,
.text-button:focus {
color: var(--color-accent);
border-color: var(--color-accent);
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr;
margin: var(--spacing-lg);
overflow: hidden;
text-align: left;
}
@media (max-width: 320px) {
.controls {
display: flex;
flex-wrap: wrap;
}
.controls * {
margin: var(--spacing-sm);
}
.controls .text-button {
margin-left: 0;
}
}
.action {
width: var(--size-5);
width: var(--size-5);
color: var(--neutral-400);
margin-left: var(--spacing-md);
}
.action:disabled {
cursor: not-allowed;
color: var(--border-color-accent-subdued);
}
.action:disabled:hover {
color: var(--border-color-accent-subdued);
}
.icon:hover,
.icon:focus {
color: var(--color-accent);
}
.container {
display: flex;
flex-direction: column;
}
</style>