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

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

Closed
josiahbryan opened this issue Jan 24, 2020 · 2 comments

Comments

@josiahbryan
Copy link

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
Copy link
Contributor

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
Copy link
Author

josiahbryan commented Jan 26, 2020 via email

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