FFmpeg-wasm/FFmpeg.wasm

Getting video frame memory leak

Opened this issue · 0 comments

I'm getting the following errors on my NodeJS server when trying to get a video's frame:
Error [RuntimeError]: operation does not support unaligned accesses
Error [RuntimeError]: memory access out of bounds

It seems like the memory is not being cleared between consequtive operations, so memory that remains from a previous operation adds up to the current one, until it crashes.

It only happens when running on AWS (linux), locally (windows 10) it works as expected. I can increase the instance size on AWS, but it seems like a memory leak so it might not be the right way to tackle this.

I'm using @ffmpeg.wasm/main version 0.12.0

  1. Is there a way to properly clear the memory in between operations? (operation = getting a new frame from a new video)

  2. Is there a way to extract frames from videos without consuming so much memory?

Here's how I'm doing it right now:

private async getVideoFrameBuffer(
    time: string,
    name: string,
    buffer: Buffer,
  ) {
    this.ffmpeg.FS(
      'writeFile',
      name,
      new Uint8Array(buffer.buffer, 0, buffer.buffer.byteLength),
    );

    const frameName = name + '-frame.jpeg';

    await this.ffmpeg.run(
      '-i',
      name,
      '-ss',
      time,
      '-frames:v',
      '1',
      frameName,
    );

    const frameBuffer: Uint8Array = this.ffmpeg.FS('readFile', frameName);

    this.ffmpeg.FS('unlink', name);
    this.ffmpeg.FS('unlink', frameName);

    return frameBuffer;
  }