Skip to content

Commit

Permalink
fetchFile of not-ffmpeg-util
Browse files Browse the repository at this point in the history
does nothing
in case you are running svelte<5?
  • Loading branch information
stylehouse committed Mar 27, 2024
1 parent 3f3de26 commit 5ac7344
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/lib/Zone.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { FFmpeg } from "@ffmpeg/ffmpeg";
// @ts-ignore
// import type { LogEvent } from '@ffmpeg/ffmpeg/dist/esm/types';
import { fetchFile } from "@ffmpeg/util";
// import { fetchFile } from "@ffmpeg/util";
import {fetchFile,toBlobURL} from '$lib/not-ffmpeg-util';
import { onMount } from "svelte";
let message;
Expand Down Expand Up @@ -43,19 +44,6 @@
console.error(err);
}
}
async function toBlobURL(url, mimeType) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
}
const blob = await response.arrayBuffer();
return URL.createObjectURL(new Blob([blob], { type: mimeType }));
} catch (err) {
console.error(`Error loading ${url}: ${err.message}`);
throw err;
}
}
onMount(() => loadFFmpeg());
async function transcode(file) {
Expand Down
59 changes: 59 additions & 0 deletions src/lib/not-ffmpeg-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export async function toBlobURL(url, mimeType) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error ${response.status}`);
}
const blob = await response.arrayBuffer();
return URL.createObjectURL(new Blob([blob], { type: mimeType }));
} catch (err) {
console.error(`Error loading ${url}: ${err.message}`);
throw err;
}
}



export const atob = (b64) => Buffer.from(b64, "base64").toString("binary");

const readFromBlobOrFile = (blob) => new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = () => {
const { result } = fileReader;
if (result instanceof ArrayBuffer) {
resolve(new Uint8Array(result));
}
else {
resolve(new Uint8Array());
}
};
fileReader.onerror = (event) => {
reject(Error(`File could not be read! Code=${event?.target?.error?.code || -1}`));
};
fileReader.readAsArrayBuffer(blob);
});
export const fetchFile = async (file) => {
let data;
if (typeof file === "string") {
/* From base64 format */
if (/data:_data\/([a-zA-Z]*);base64,([^"]*)/.test(file)) {
data = atob(file.split(",")[1])
.split("")
.map((c) => c.charCodeAt(0));
/* From remote server/URL */
}
else {
data = await (await fetch(file)).arrayBuffer();
}
}
else if (file instanceof URL) {
data = await (await fetch(file)).arrayBuffer();
}
else if (file instanceof File || file instanceof Blob) {
data = await readFromBlobOrFile(file);
}
else {
return new Uint8Array();
}
return new Uint8Array(data);
};

0 comments on commit 5ac7344

Please sign in to comment.