Skip to content

Advice for taking the last 10s of audio without FFMPEG or re-encoding #140

@jmalfara

Description

@jmalfara

Hey folks. Im trying to do some processing on audio in chunks instead of reading directly off the full outputted webm file.

The reason is that I need to make sure the duration is known and for some reason the duration tags when recording are reported as N/A (https://stackoverflow.com/questions/34118013/how-to-determine-webm-duration-using-ffprobe and #136). Seems like the common solution is to re-encode the file to fix the duration, which doesn't seem very efficient if you want to process new data every 10s or so.

What i'm trying to do is process the latest chunks of audio every 10s so the entire file isn't useful to me. This is what I have so far but i'm definitely missing something important with webm. Im guessing its probably because of the missing header information but not sure.

    let buffer: Buffer = Buffer.from([])
    const writableStream = new Stream.Writable({
      write(chunk, encoding, callback) {
        buffer = Buffer.concat([chunk, buffer])
        callback();
      }
    });

    const stream = await getStream(page, {
      audio: true,
      video: false,
    });
    stream.pipe(writableStream);

    // let fromTime = 0;
    const intervalTime = 10 * 1000;
    const reportInterval = setInterval(() => {
      // Write a webm file thats 10s long. This will be used for processing.
      writeFile(`./chunks/example.webm`, buffer).catch((error) => {
        console.log(error)
      })
      buffer = Buffer.from([])
    }, intervalTime);

In this case example.webm refused to load anything.

I've seen the example with the discord stream but it doesn't seem very applicable. I feel like this is a good usecase for people who don't need to record the raw audio. If anyone has pointers on WebM or relatively simple documentation please lmk. Any advice is appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions