Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Change time format #7274

Merged
merged 17 commits into from Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/mean-aliens-warn.md
@@ -0,0 +1,8 @@
---
"@gradio/audio": patch
"@gradio/utils": patch
"@gradio/video": patch
"gradio": patch
---

feat:chore: Change time format (thanks @jjshoots for the independent contribution).
15 changes: 4 additions & 11 deletions js/audio/player/AudioPlayer.svelte
@@ -1,7 +1,7 @@
<script lang="ts">
import { onMount } from "svelte";
import { Music } from "@gradio/icons";
import type { I18nFormatter } from "@gradio/utils";
import { format_time, type I18nFormatter } from "@gradio/utils";
import WaveSurfer from "wavesurfer.js";
import { skip_audio, process_audio } from "../shared/utils";
import WaveformControls from "../shared/WaveformControls.svelte";
Expand Down Expand Up @@ -47,13 +47,6 @@
end: undefined;
}>();

const formatTime = (seconds: number): string => {
const minutes = Math.floor(seconds / 60);
const secondsRemainder = Math.round(seconds) % 60;
const paddedSeconds = `0${secondsRemainder}`.slice(-2);
return `${minutes}:${paddedSeconds}`;
};

const create_waveform = (): void => {
waveform = WaveSurfer.create({
container: container,
Expand All @@ -75,13 +68,13 @@

$: waveform?.on("decode", (duration: any) => {
audio_duration = duration;
durationRef && (durationRef.textContent = formatTime(duration));
durationRef && (durationRef.textContent = format_time(duration));
});

$: waveform?.on(
"timeupdate",
(currentTime: any) =>
timeRef && (timeRef.textContent = formatTime(currentTime))
timeRef && (timeRef.textContent = format_time(currentTime))
);

$: waveform?.on("ready", () => {
Expand Down Expand Up @@ -170,7 +163,7 @@
<time bind:this={timeRef} id="time">0:00</time>
<div>
{#if mode === "edit" && trimDuration > 0}
<time id="trim-duration">{formatTime(trimDuration)}</time>
<time id="trim-duration">{format_time(trimDuration)}</time>
{/if}
<time bind:this={durationRef} id="duration">0:00</time>
</div>
Expand Down
8 changes: 1 addition & 7 deletions js/audio/recorder/AudioRecorder.svelte
Expand Up @@ -9,6 +9,7 @@
import WaveformRecordControls from "../shared/WaveformRecordControls.svelte";
import RecordPlugin from "wavesurfer.js/dist/plugins/record.js";
import type { WaveformOptions } from "../shared/types";
import { format_time } from "@gradio/utils";

export let mode: string;
export let i18n: I18nFormatter;
Expand Down Expand Up @@ -61,13 +62,6 @@
edit: undefined;
}>();

const format_time = (seconds: number): string => {
const minutes = Math.floor(seconds / 60);
const secondsRemainder = Math.round(seconds) % 60;
const paddedSeconds = `0${secondsRemainder}`.slice(-2);
return `${minutes}:${paddedSeconds}`;
};

$: record?.on("record-start", () => {
start_interval();
timing = true;
Expand Down
15 changes: 15 additions & 0 deletions js/utils/src/utils.ts
Expand Up @@ -151,6 +151,21 @@ async function copy_to_clipboard(value: string): Promise<boolean> {
return copied;
}

export const format_time = (seconds: number): string => {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const seconds_remainder = Math.round(seconds) % 60;
const padded_minutes = `${minutes < 10 ? "0" : ""}${minutes}`;
const padded_seconds = `${
seconds_remainder < 10 ? "0" : ""
}${seconds_remainder}`;

if (hours > 0) {
return `${hours}:${padded_minutes}:${padded_seconds}`;
}
return `${minutes}:${padded_seconds}`;
};

export type I18nFormatter = any;
export class Gradio<T extends Record<string, any> = Record<string, any>> {
#id: number;
Expand Down
13 changes: 2 additions & 11 deletions js/video/shared/Player.svelte
Expand Up @@ -5,6 +5,7 @@
import VideoControls from "./VideoControls.svelte";
import type { FileData } from "@gradio/client";
import { prepare_files, upload } from "@gradio/client";
import { format_time } from "@gradio/utils";

export let root = "";
export let src: string;
Expand Down Expand Up @@ -70,16 +71,6 @@
time = (duration * (e.clientX - left)) / (right - left);
}

function format(seconds: number): string {
if (isNaN(seconds) || !isFinite(seconds)) return "...";

const minutes = Math.floor(seconds / 60);
let _seconds: number | string = Math.floor(seconds % 60);
if (_seconds < 10) _seconds = `0${_seconds}`;

return `${minutes}:${_seconds}`;
}

function handle_end(): void {
dispatch("stop");
dispatch("end");
Expand Down Expand Up @@ -138,7 +129,7 @@
{/if}
</span>

<span class="time">{format(time)} / {format(duration)}</span>
<span class="time">{format_time(time)} / {format_time(duration)}</span>

<!-- TODO: implement accessible video timeline for 4.0 -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
Expand Down
15 changes: 2 additions & 13 deletions js/video/shared/VideoControls.svelte
Expand Up @@ -5,6 +5,7 @@
import { FFmpeg } from "@ffmpeg/ffmpeg";
import loadFfmpeg from "./utils";
import { onMount } from "svelte";
import { format_time } from "@gradio/utils";

export let videoElement: HTMLVideoElement;

Expand All @@ -24,18 +25,6 @@
$: if (mode === "edit" && trimmedDuration === null && videoElement)
trimmedDuration = videoElement.duration;

const formatTime = (seconds: number): string => {
const minutes = Math.floor(seconds / 60);
const secondsRemainder = Math.round(seconds) % 60;
const paddedSeconds = `0${secondsRemainder}`.slice(-2);

if (Number.isNaN(minutes) || Number.isNaN(secondsRemainder)) {
return "00:00";
}

return `${minutes}:${paddedSeconds}`;
};

let trimmedDuration: number | null = null;
let dragStart = 0;
let dragEnd = 0;
Expand Down Expand Up @@ -69,7 +58,7 @@
{#if mode === "edit" && trimmedDuration !== null}
<time
aria-label="duration of selected region in seconds"
class:hidden={loadingTimeline}>{formatTime(trimmedDuration)}</time
class:hidden={loadingTimeline}>{format_time(trimmedDuration)}</time
>
{:else}
<div />
Expand Down