ffmpeg AVStream::codecpar为空
我试图用libav制作一个简单的音频解码器,但遇到了一个问题。 我无法从AVStream获取AVCODECODEC参数,codepar始终为空 这是我的密码ffmpeg AVStream::codecpar为空,ffmpeg,libav,Ffmpeg,Libav,我试图用libav制作一个简单的音频解码器,但遇到了一个问题。 我无法从AVStream获取AVCODECODEC参数,codepar始终为空 这是我的密码 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> extern "C" { #include <libavcodec/avcodec.h> #inclu
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/channel_layout.h>
#include <libswresample/swresample.h>
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s input_file > output_file\n", argv[0]);
exit(1);
}
const int out_channels = 2, out_samples = 512, sample_rate = 44100;
int max_buffer_size;
// register supported formats and codecs
av_register_all();
// allocate empty format context
// provides methods for reading input packets
AVFormatContext* fmt_ctx = avformat_alloc_context();
// determine input file type and initialize format context
if (avformat_open_input(&fmt_ctx, argv[1], NULL, NULL) != 0) {
fprintf(stderr, "error: avformat_open_input()\n");
exit(1);
}
// determine supported codecs for input file streams and add
// them to format context
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "error: avformat_find_stream_info()\n");
exit(1);
}
AVCodec* codec = NULL;
int stream = 0;
// find audio stream in format context
fprintf(stderr, "%d\n", fmt_ctx->nb_streams);
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
AVCodecParameters* avCodecParameters = NULL;
avCodecParameters = fmt_ctx->streams[i]->codecpar;
if(!avCodecParameters){
fprintf(stderr, "error: CodecParameters\n");// always fail here
exit(1);
}
if(avCodecParameters->codec_type == AVMEDIA_TYPE_AUDIO){
stream = i;
codec = avcodec_find_decoder(avCodecParameters->codec_id);
if (!codec) {
fprintf(stderr, "error: avcodec_find_decoder()\n");
exit(1);
}
break;
}
}
if (stream == fmt_ctx->nb_streams) {
fprintf(stderr, "error: no audio stream found\n");
exit(1);
}
return 0;
}
有人有同样的问题吗
如何解决
谢谢 不要在
上退出!avCodecParameters
第一个流可能为空。第二个流可能就是您正在寻找的。您是如何解决的?请确保没有冲突的库版本。Cmake在交叉编译方面做得不好,我从标准路径而不是自定义路径中引入了旧库(libavcodec等)。我没有意识到这一点,只是碰巧在我的嵌入式系统上有两套库。当我运行时,我以为它正在使用我更新的库,但事实并非如此。我的错误是我的AVStream*中的AVCodecParameters*为空,导致segfault。请不要在上退出!avCodecParameters
第一个流可能为空。第二个流可能就是您正在寻找的。您是如何解决的?请确保没有冲突的库版本。Cmake在交叉编译方面做得不好,我从标准路径而不是自定义路径中引入了旧库(libavcodec等)。我没有意识到这一点,只是碰巧在我的嵌入式系统上有两套库。当我运行时,我以为它正在使用我更新的库,但事实并非如此。我的错误是我的AVStream*中的AVCODECADEPARAMETERS*为空,导致segfault。
1
error: CodecParameters