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

Memory error #745

Open
tiuvi opened this issue May 3, 2024 · 2 comments
Open

Memory error #745

tiuvi opened this issue May 3, 2024 · 2 comments

Comments

@tiuvi
Copy link

tiuvi commented May 3, 2024

Describe the bug

The file memory is doubled in Chrome, if the file occupies 400mb, the memory increases by 800mb.
To Play
-> use ffmpeg.writeFile(fileName, array400mb);
Expected behavior
I imagine the problem is:
Browser -> worker (file 400mb) -> webasembly (file 400mb)

Wasm does not use the file system, only memory, could you give me some clue to see where this copy is made to see if we can pass the file to webasembly by ranges while the worker file is deleted and when the file is formed in webassembly send them by range to the worker and finally pass it to the browser.

I think it would be optimal, let's see if I wanted to replicate the file system with ffmpeg when wasm does not use the file system, I think it was a mistake.

@stri8ed
Copy link

stri8ed commented Jun 3, 2024

Instead of copying the file to the worker, you can transfer a reference to it like so:

await this.ffmpeg.mount("WORKERFS", { files: [file]}, '/some-dir');

See here

@tiuvi
Copy link
Author

tiuvi commented Jun 24, 2024

Thank you very much, it worked extremely well, although it does not work with the -i option.

I reduced the memory from 1gb to 150mb, much more than half the file.
I think it would be necessary to obtain the readFile directly from a Javascript File api, so the output would also be reduced a lot, although it is not my case, I leave my example in case it can help someone since there is no documentation.

```js
globalThis[nameGlobal]['fileCutVideo'] = async function (args) {

    console.log("llego hasta filecutvideo", args)
    const {
        initMs,
        endMs,
        urlBlob
    } = args;

    const file = urlBlob;

    var urlBlobOut, err = null;
    try {

        var { err } = await loadFfmpeg();
        if (err !== null) {
            return ({ err })
        }

        const directory = await ffmpeg.listDir("/");

        if (!directory.find(item => item.name === "mounted")) {

            await $ffmpeg.ffmpeg.createDir('/mounted');

        }

        const directoryMounted = await ffmpeg.listDir("/mounted");
        if (directoryMounted.find(item => item.name === file.name)) {

            return ({ err: `Este ${file.name} archivo se esta procesando` })
        } else {
            await $ffmpeg.ffmpeg.mount("WORKERFS", { files: [urlBlob] }, '/mounted');
        }

        const output = await $ffmpeg.ffmpeg.exec([
            '-ss', formatMilliseconds(initMs),
            '-to', formatMilliseconds(endMs),
            '-i', "/mounted/" + file.name,
            '-c:v', 'copy',
            '-c:a', 'copy',
            '/output.mp4'
        ]);

        if (output === 0) {

            const isUnMount = await $ffmpeg.ffmpeg.unmount("/mounted")

            const data = await $ffmpeg.ffmpeg.readFile('/output.mp4');

            const isDeleteOutput = await ffmpeg.deleteFile('/output.mp4');

            const newBlob = new Blob([data.buffer], { type: file.type })

            urlBlobOut = URL.createObjectURL(newBlob);

        } else {

            const originaldelete = await $ffmpeg.ffmpeg.deleteFile("/mounted/" + file.name);

            this.err = "Tiempo de espera de 30 segundos agotado";
        }
        console.log(urlBlobOut);
        await terminateFfmpeg();

        return ({ urlBlobOut, err: err });

    } catch (error) {

        await terminateFfmpeg();

        return ({ err: error });

    }


}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants