admin管理员组文章数量:1025227
I'm trying to make an avi file from a video/audio stream. When audio is AAC it works fine. But I cannot pack G711U audio.
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;
}
Here avformat_write_header() returns -22. What's wrong here? Which data should I supply to this function?
I'm trying to make an avi file from a video/audio stream. When audio is AAC it works fine. But I cannot pack G711U audio.
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;
}
Here avformat_write_header() returns -22. What's wrong here? Which data should I supply to this function?
Share Improve this question asked Nov 19, 2024 at 13:27 denndenn 3553 silver badges16 bronze badges 2 |1 Answer
Reset to default 1In your code you tried to create MP4 container format output file instead AVI: when you've provided short name in av_guess_format as first parameter, it has more weight for decide output format than file name extension (https://www.ffmpeg./doxygen/0.6/libavformat_2utils_8c-source.html#l00198).
MP4 container does not support PCM data including G.711. Please look on this page for details https://en.wikipedia./wiki/Comparison_of_video_container_formats
I'm trying to make an avi file from a video/audio stream. When audio is AAC it works fine. But I cannot pack G711U audio.
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;
}
Here avformat_write_header() returns -22. What's wrong here? Which data should I supply to this function?
I'm trying to make an avi file from a video/audio stream. When audio is AAC it works fine. But I cannot pack G711U audio.
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;
}
Here avformat_write_header() returns -22. What's wrong here? Which data should I supply to this function?
Share Improve this question asked Nov 19, 2024 at 13:27 denndenn 3553 silver badges16 bronze badges 2-
If you are on a POSIX system, AVERROR wraps a POSIX error code as a negative value. POSIX error 22 is
EINVAL
"invalid argument" on my system. – Eljay Commented Nov 19, 2024 at 13:44 - Ok, what's wrong with the argument? – denn Commented Nov 19, 2024 at 14:13
1 Answer
Reset to default 1In your code you tried to create MP4 container format output file instead AVI: when you've provided short name in av_guess_format as first parameter, it has more weight for decide output format than file name extension (https://www.ffmpeg./doxygen/0.6/libavformat_2utils_8c-source.html#l00198).
MP4 container does not support PCM data including G.711. Please look on this page for details https://en.wikipedia./wiki/Comparison_of_video_container_formats
版权声明:本文标题:c++ - avformat_write_header return error code when trying to write video with G711U audio avi file - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745559138a2156055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
EINVAL
"invalid argument" on my system. – Eljay Commented Nov 19, 2024 at 13:44