Spaces:
Running
Running
File size: 5,690 Bytes
bd5fab7 |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
<!--
@component
This component provides editing controls for a video, such as trimming.
It appears below the main player when in `interactive` mode. It uses FFmpeg
(loaded in the browser via WebAssembly) to perform the trimming operation.
-->
<script lang="ts">
import { Undo, Trim, Clear } 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";
import { IconButton } from "@gradio/atoms";
import { ModifyUpload } from "@gradio/upload";
import type { FileData } from "@gradio/client";
// ------------------
// Props
// ------------------
/** A direct reference to the HTML <video> element to be controlled. */
export let videoElement: HTMLVideoElement;
/** If true, shows the 'Undo' button. */
export let showRedo = false;
export let interactive = true;
/** The current editing mode (e.g., 'edit' for trimming). */
export let mode = "";
/** A callback function to reset the video to its original state. */
export let handle_reset_value: () => void;
/** A callback function to handle the new, trimmed video blob. */
export let handle_trim_video: (videoBlob: Blob) => void;
/** Two-way bound prop to indicate if the video is currently being processed by FFmpeg. */
export let processingVideo = false;
export let i18n: (key: string) => string;
export let value: FileData | null = null;
export let show_download_button = false;
export let handle_clear: () => void = () => {};
/** True if the video has a previous state to revert to. */
export let has_change_history = false;
// -----------------
// Internal State
// -----------------
/** The FFmpeg instance. */
let ffmpeg: FFmpeg;
/** The duration of the selected trim region. */
let trimmedDuration: number | null = null;
/** The start time of the trim, in seconds. */
let dragStart = 0;
/** The end time of the trim, in seconds. */
let dragEnd = 0;
/** A flag to show a loading state while the timeline generates thumbnails. */
let loadingTimeline = false;
/** Load the FFmpeg library when the component is first mounted. */
onMount(async () => {
ffmpeg = await loadFfmpeg();
});
/** When entering edit mode, initialize the trimmed duration to the full video duration. */
$: if (mode === "edit" && trimmedDuration === null && videoElement)
trimmedDuration = videoElement.duration;
/** Toggles the video trimming UI on and off. */
const toggleTrimmingMode = (): void => {
if (mode === "edit") {
mode = "";
trimmedDuration = videoElement.duration;
} else {
mode = "edit";
}
};
</script>
<!-- The trimming UI, which is only visible when mode is 'edit'. -->
<div class="container" class:hidden={mode !== "edit"}>
{#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
>
<div class="edit-buttons">
<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
>
</div>
{:else}
<div />
{/if}
</div>
</div>
<!-- Standard controls like Clear, Download, Undo, and Trim. -->
<ModifyUpload
{i18n}
on:clear={() => handle_clear()}
download={show_download_button ? value?.url : null}
>
{#if showRedo && mode === ""}
<IconButton
Icon={Undo}
label="Reset video to initial value"
disabled={processingVideo || !has_change_history}
on:click={() => {
handle_reset_value();
mode = "";
}}
/>
{/if}
{#if interactive && mode === ""}
<IconButton
Icon={Trim}
label="Trim video to selection"
disabled={processingVideo}
on:click={toggleTrimmingMode}
/>
{/if}
</ModifyUpload>
<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%;
}
.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;
}
.text-button:hover,
.text-button:focus {
color: var(--color-accent);
border-color: var(--color-accent);
}
.controls {
display: flex;
justify-content: space-between;
align-items: center;
margin: var(--spacing-lg);
overflow: hidden;
}
.edit-buttons {
display: flex;
gap: var(--spacing-sm);
}
@media (max-width: 320px) {
.controls {
flex-direction: column;
align-items: flex-start;
}
.edit-buttons {
margin-top: var(--spacing-sm);
}
.controls * {
margin: var(--spacing-sm);
}
.controls .text-button {
margin-left: 0;
}
}
.container {
display: flex;
flex-direction: column;
}
.hidden {
display: none;
}
</style> |