Streampunk / beamcoder

Node.js native bindings to FFmpeg.
GNU General Public License v3.0
399 stars 76 forks source link

how to configure opus encoder? #80

Open vinnitu opened 2 years ago

vinnitu commented 2 years ago
const beamcoder = require('beamcoder');

async function run() {
  let encoder = beamcoder.encoder({
    name: 'opus',
  });

  let demuxer = await beamcoder.demuxer('./f.mp3');
  let decoder = await beamcoder.decoder({ demuxer: demuxer, stream_index: 0 });

  for ( let x = 0 ; x < 100 ; x++ ) {
    let packet = await demuxer.read();
    let frames = await decoder.decode(packet);
    for (let frame in frames) {
        const p = encoder.encode(frame);
        console.log(p);
    }
  }
}

run();
Promise {
  <rejected> Error: In file ../src/encode.cc on line 330, found error: All passed values as arguments must be of type frame.
      at run (/home/user/project/index.js:26:27) {
    code: '5001'
  }
}
Bahlinc-Dev commented 2 years ago

First of all, the decoded frames are located in in "frames" sub property, and not the root object, so you would need to change your encode line to: const p = encoder.encode(frame.frames). Also you need to specify additional opus specific encoding parameters on the encoder like this: (example)

let encoder = beamcoder.encoder({ name: 'libopus', bit_rate: 128000, time_base: [1, 48000], sample_rate: 48000, channels: 1, channel_layout: 'mono', interleaved: false, sample_fmt: 'flt', });

Additionally, if the source "sample_fmt" is not in either s16 or flt bit format (the only possible for opus) you will need a filter in between the decode and encode steps to convert the source format to the destination format defined in the decoder, it's not automatic.