Streampunk / beamcoder

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

Audio Encoder Error: Specified sample format -1 is invalid or not supported / No codec provided to avcodec_open2() #32

Closed josiahbryan closed 4 years ago

josiahbryan commented 4 years ago

No matter what I try, when I try encoding an audio stream along with a video, I get this:

[aac @ 0x102808e00] Specified sample format -1 is invalid or not supported
[NULL @ 0x102808e00] No codec provided to avcodec_open2()

TL;DR: See the gist at https://gist.github.com/josiahbryan/5c149b97c8f350c7ff10a66a42ff8d2f - just run it and you (should) get those errors above.

I'm using the encoder/muxer params right from beamcoder. For example:

const mxs = beamcoder.muxers();
const muxerSpecs = mxs.mp4;
const mux = beamcoder.muxer({ format_name: muxerSpecs.name });

const encoders = beamcoder.encoders();
const audioEncoderSpecs = encoders[muxerSpecs.audio_codec];

const audioEncParams = {
    name:          muxerSpecs.audio_codec,
    sampleFormat:  audioEncoderSpecs.sample_fmts[0],
    channelLayout: 'stereo',
    sampleRate:    audioEncoderSpecs.supported_samplerates ? audioEncoderSpecs.supported_samplerates[0] : 44100,
    timeBase:     [1, 90000],
};

const audioEncoder = beamcoder.encoder({
    name:           audioEncParams.name,
    format:         audioEncParams.sampleFormat,
    channel_layout: audioEncParams.channelLayout,
    time_base:      audioEncParams.timeBase,
    sample_rate:    audioEncParams.sampleRate,
}); 

const audioStream = mux.newStream({
    name:        audioEncParams.name,
    time_base:   audioEncParams.timeBase,
    interleaved: false
});

Object.assign(audioStream.codecpar, { // Object.assign copies over all properties
    channels:       2,
    sample_rate:    audioEncParams.sampleRate,
    format:         audioEncParams.sampleFormat,
    channel_layout: audioEncParams.channelLayout,
    bit_rate:       audioEncParams.sampleRate * 4, //48000*4
    frame_size: 1024,
});

const audioFrame = ...;

let packets = await audioEncoder.encode(audioFrame);

As soon as the code hits the audioEncoder.encode(), the console shows: [aac @ 0x102808e00] Specified sample format -1 is invalid or not supported (the first time) - subsequent frames give the No codec provided to avcodec_open2() errors.

Self-contained small example that reproduces this problem is https://gist.github.com/josiahbryan/5c149b97c8f350c7ff10a66a42ff8d2f

Any ideas on how to fix this? Please? :)

scriptorian commented 4 years ago

Hi, thanks for your interest in beamcoder. Your problem is that the audioEncoder property for sample format is called 'sample_fmt', not 'format'. The codecpar property is correctly 'format'. We have chosen to follow the naming of the underlying FFmpeg properties for AVCodecContext and AVCodecParameters respectively so in this case it does end up being confusing. Thanks for providing the gist - a big help in tracking it down quickly.

josiahbryan commented 4 years ago

That worked perfectly - so sorry to bother you, thanks so much for your help!

On Sun, Jan 26, 2020 at 1:24 PM Simon Rogers notifications@github.com wrote:

Hi, thanks for your interest in beamcoder. Your problem is that the audioEncoder property for sample format is called 'sample_fmt', not 'format'. The codecpar property is correctly 'format'. We have chosen to follow the naming of the underlying FFmpeg properties for AVCodecContext and AVCodecParameters respectively so in this case it does end up being confusing. Thanks for providing the gist - a big help in tracking it down quickly.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Streampunk/beamcoder/issues/32?email_source=notifications&email_token=ABEZELDIVMQIVBBKZ57WBVDQ7XIOTA5CNFSM4KLECZ52YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJ52NGY#issuecomment-578528923, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABEZELF2LKWL5PAWEDHFSYDQ7XIOTANCNFSM4KLECZ5Q .

-- Josiah Bryan 765-215-0511 www.josiahbryan.com https://www.josiahbryan.com/?utm_source=sig josiahbryan@gmail.com