Skip to content

Commit

Permalink
fix pack.mcmeta & ffmpeg loading multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
spartacus04 committed Mar 22, 2024
1 parent ef5d1ff commit 4462501
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/lib/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const qualityArgs = {

export const localFFmpegStore = writable(false);

export const prepareAudio = async (blob: Blob, data: FFmpegData, onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown) : Promise<Blob|null> => {
export const prepareAudio = async (blob: Blob, data: FFmpegData, onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown, ffmpeg: FFmpeg|null) : Promise<Blob|null> => {
const args = ['-vn', '-acodec', 'libvorbis'];

if (data.mono) args.push('-ac', '1');
Expand Down Expand Up @@ -49,8 +49,8 @@ export const prepareAudio = async (blob: Blob, data: FFmpegData, onProgress: (to

return new Blob([base64ToArrayBuffer(result)], { type: 'audio/ogg' });
} else {
const ffmpeg = new FFmpeg();
if(ffmpeg === null) return null;

if(!ffmpeg.loaded) await ffmpeg.load(crossOriginIsolated ? {
coreURL: `${baseMTURL}/ffmpeg-core.js`,
wasmURL: `${baseMTURL}/ffmpeg-core.wasm`
Expand All @@ -76,10 +76,10 @@ export const prepareAudio = async (blob: Blob, data: FFmpegData, onProgress: (to
}
}

export const loadFFmpeg = async (onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown) => {
export const loadFFmpeg = async (onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown) : Promise<FFmpeg | null> => {
const ffmpeg = new FFmpeg();

if(window.__TAURI__ || ffmpeg.loaded) return;
if(window.__TAURI__ || ffmpeg.loaded) return null;

onProgress(2, 0, 'Downloading ffmpeg-core.js', 1);

Expand All @@ -100,6 +100,8 @@ export const loadFFmpeg = async (onProgress: (total: number|undefined, done: num
});

onProgress(undefined, undefined, undefined, 1);

return ffmpeg;
}

const fetchData = async (url: string, progress: (done: boolean) => unknown) => {
Expand Down
11 changes: 6 additions & 5 deletions src/lib/resourcepack/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { getDuration, processImage } from "./utils";
import { loadFFmpeg, prepareAudio } from "$lib/ffmpeg";
import JSZip from "jszip";
import { getVersionFromTime } from "$lib/utils";
import type { FFmpeg } from "@ffmpeg/ffmpeg";

export const processResources = async (onError: (err : string) => unknown, onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown) => {
export const processResources = async (ffmpeg: FFmpeg|null, onError: (err : string) => unknown, onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown) => {
const discs = get(discsStore);

let current = 0;
Expand All @@ -31,7 +32,7 @@ export const processResources = async (onError: (err : string) => unknown, onPro
mono: disc.uploadData.mono,
normalize: disc.uploadData.normalize,
quality: disc.uploadData.quality
}, onProgress);
}, onProgress, ffmpeg);

if(!track) onError(`Failed to process track for ${disc["disc-namespace"]}`)
else {
Expand Down Expand Up @@ -77,7 +78,7 @@ export const generateResourcePack = async () : Promise<Blob> => {
// pack.mcmeta
rp.file('pack.mcmeta', JSON.stringify({
pack: {
version: resourcePackData.version,
pack_format: resourcePackData.version,
description: resourcePackData.description
}
}, null, 2));
Expand Down Expand Up @@ -302,11 +303,11 @@ export const mergeResourcePacks = async (base: Blob) : Promise<Blob> => {
}

export const outputEverything = async (onJavaRp: (rp: Blob) => unknown, onProgress: (total: number|undefined, done: number|undefined, status: string|undefined, index: number) => unknown, onBedrockRp: (rp: Blob) => unknown) => {
await loadFFmpeg(onProgress);
const ffmpeg = await loadFFmpeg(onProgress);

onProgress(3, 1, "Constructing discs", 0);

await processResources(err => {
await processResources(ffmpeg, err => {
alert(err);
}, onProgress);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/resourcepack/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const importRP = async (discStore: Writable<Disc[]>, rp: Blob, discsJson:
resourcePackStore.set({
icon: await zip.file('pack.png')?.async('blob') ?? get(resourcePackStore).icon,
description: JSON.parse(await zip.file('pack.mcmeta')!.async('text')).pack.description as string,
version: JSON.parse(await zip.file('pack.mcmeta')!.async('text')).pack.version as number,
version: JSON.parse(await zip.file('pack.mcmeta')!.async('text')).pack.pack_format as number,
packs: [{
name: 'Imported Pack',
icon: await zip.file('pack.png')?.async('blob') ?? get(resourcePackStore).icon,
Expand Down

0 comments on commit 4462501

Please sign in to comment.