You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

封装含G711U音频的AVI文件时avformat_write_header返回错误码-22的问题求助

封装含G711U音频的AVI文件时avformat_write_header返回错误码-22的问题求助

我正在尝试从音视频流生成AVI文件,当音频是AAC格式时一切正常,但无法封装G711U(PCM μ-law)音频。调用avformat_write_header()时返回错误码-22,想请教哪里出了问题,以及需要为这个函数提供哪些必要的数据?

以下是相关代码片段:

av_register_all();
avcodec_register_all();
av_log_set_callback(nullptr);

av_format_context_ = avformat_alloc_context();
if (!av_format_context_)
{
  std::string error_msg = "Failed to allocate avformat context";
  log_->Error(error_msg);
  throw std::runtime_error(error_msg.c_str());
}

av_format_context_->oformat = av_guess_format("mp4", filename.c_str(), NULL);
strcpy(av_format_context_->filename, filename.c_str());

AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_PCM_MULAW);
av_format_context_->oformat->audio_codec = codec->id;

audio_stream_ = avformat_new_stream(av_format_context_, codec);
if (audio_stream_ == nullptr)
{
  log_->Error("Can not create audiostream.");
  return false;
}

AVCodecContext& codec_context = *audio_stream_->codec;
if (avcodec_copy_context(audio_stream_->codec, av_format_context_->streams[1]->codec) != 0)
{
  log_->Error("Failed to Copy Context");
  return false;
}

audio_stream_->time_base = { 1, codec_context.sample_rate };

if (av_format_context_->oformat->flags & AVFMT_GLOBALHEADER)
  codec_context.flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

log_->Info("Init output {}", filename);
av_dump_format(av_format_context_, 0, filename.c_str(), 1);

int ret = avio_open(&av_format_context_->pb, filename.c_str(), AVIO_FLAG_WRITE);
if (ret < 0)
{
  log_->Error("Can not open file for writing.");
  return false;
}

ret = avformat_write_header(av_format_context_, NULL);
if (ret < 0)
{
  log_->Error("Can not write header.");
  return false;
}

备注:内容来源于stack exchange,提问作者denn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:09:30