Skip to content

Commit

Permalink
add spinners
Browse files Browse the repository at this point in the history
  • Loading branch information
lharries committed Jun 17, 2024
1 parent 7a56683 commit 8e0f2b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
34 changes: 28 additions & 6 deletions examples/sound-effects/video-to-sfx/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"use client";
import { Mask, masks, shadows } from "@/frostin-ui";
import Image from "next/image";
import { motion } from "framer-motion";
import { useEffect, useMemo, useRef, useState } from "react";
import { FileInput } from "@/components/ui/file-input";
import { springs } from "@/frostin-ui/utils/springs";
import { useScramble } from "use-scramble";
import AutosizeTextarea from "react-textarea-autosize";
import { Orchestrator } from "./state/orchestrator";
import { AudioPlayer } from "./state/player";
import { observer } from "mobx-react";
Expand All @@ -25,7 +23,7 @@ const HoverOverlay = ({ className }: { className?: string }) => {
};
import { convertVideoToSFX } from "@/lib/videoToSFX";
import { useMutation } from "@tanstack/react-query";
import { DownloadIcon } from "lucide-react";
import { DownloadIcon, LoaderCircle, LoaderPinwheel } from "lucide-react";
import { Button } from "@/components/ui/button";
import { mergeAndDownload } from "@/lib/mergeAndDownload";

Expand Down Expand Up @@ -132,6 +130,12 @@ const Home = observer(() => {
const [file, setFile] = useState<File | null>(null);
const [orchestrator, setOrchestrator] = useState<Orchestrator | null>(null);
const canceledRef = useRef(false);
const [isDownloading, setIsDownloading] = useState([
false,
false,
false,
false,
]);

const previewUrl = useMemo(
() => (file ? URL.createObjectURL(file) : null),
Expand Down Expand Up @@ -254,6 +258,7 @@ const Home = observer(() => {
onPause={() => {
orchestrator?.stop();
}}
muted
/>
)}
</motion.div>
Expand Down Expand Up @@ -284,14 +289,25 @@ const Home = observer(() => {
onPause={() => orchestrator.stop()}
player={player}
active={orchestrator.activeIndex === index}
onDownload={() => {
onDownload={async () => {
const url = orchestrator.getAudioUrl(index);
if (!file || !url) {
window.alert("Error downloading");
return;
}
mergeAndDownload(file, url);
setIsDownloading(prev => {
const newState = [...prev];
newState[index] = true;
return newState;
});
await mergeAndDownload(file, url);
setIsDownloading(prev => {
const newState = [...prev];
newState[index] = false;
return newState;
});
}}
isDownloading={isDownloading[index]}
/>
</div>
))}
Expand Down Expand Up @@ -343,13 +359,15 @@ const SoundEffect = observer(
onPause,
active,
onDownload,
isDownloading,
}: {
index: number;
player: AudioPlayer;
onPlay: () => void;
onPause: () => void;
active: boolean;
onDownload: () => void;
isDownloading: boolean;
}) => {
return (
<motion.button
Expand Down Expand Up @@ -404,7 +422,11 @@ const SoundEffect = observer(
key={"download" + index}
className="self-center mr-3 rounded-full bg-transparent hover:bg-white/20 active:bg-white/20 border-gray-800/15"
>
<DownloadIcon size={16} />
{isDownloading ? (
<LoaderCircle className="animate-spin" size={16} />
) : (
<DownloadIcon size={16} />
)}
</Button>
</motion.button>
);
Expand Down
11 changes: 9 additions & 2 deletions examples/sound-effects/video-to-sfx/lib/mergeAndDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ export async function mergeAndDownload(

await ffmpeg.exec(["-v", "error", "-i", "audio.mpeg", "-f", "null", "-"]);

await ffmpeg.exec([
"-i",
"input.mp4",
"-an", // This option removes the audio
"no_audio.mp4",
]);

await ffmpeg.exec([
"-v",
"verbose",
"-i",
"input.mp4",
"no_audio.mp4",
"-i",
"audio.mpeg",
"-c:v",
Expand All @@ -54,7 +61,7 @@ export async function mergeAndDownload(
"output.mp4",
]);
console.log("transcoding completed");
const data = await ffmpeg.readFile("output.mp4");
const data = (await ffmpeg.readFile("output.mp4")) as Uint8Array;
const final_url = URL.createObjectURL(
new Blob([data.buffer], { type: "video/mp4" })
);
Expand Down

0 comments on commit 8e0f2b1

Please sign in to comment.