我厌倦了搜索关于这个主题的解决方案。有人可以帮忙吗?
类型:
AVOutputFormat* m_outFormat;
AVFormatContext* m_formatContext;
AVCodecContext* m_videoCodecContext;
AVCodec* m_videoCodec;
代码:
avcodec_register_all();
av_register_all();
m_outFormat = av_guess_format(NULL,filePath().toUtf8().constData(),NULL);
//filePath ended like ".mp4"
if (!m_outFormat)
return; //all is fine
avformat_alloc_output_context2(&m_formatContext,NULL,NULL,filePath().toUtf8().constData());
m_formatContext->oformat->video_id = CODEC_ID_H264;
m_outFormat=m_formatContext->oformat;
////////////////////////////////////////////////////////////////////
m_videoCodec=avcodec_find_encoder(CODEC_ID_H264);
m_videoStream = avformat_new_stream(m_formatContext,m_videoCodec);
if (m_videoStream)
return; //all is fine
m_videoCodecContext = avcodec_alloc_context3(m_videoCodec);
m_videoCodecContext->codec_id = CODEC_ID_H264;
m_videoCodecContext->width = 1280;
m_videoCodecContext->height = 720;
m_videoCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
m_videoCodecContext->pix_fmt = PIX_FMT_YUV420P;
av_codec_open2(M_videoCodecContext,m_videoCodec,NULL);
我收到一个错误:[libx264 @.....] 编解码器类型或 ID 错误。
av_codec_open2(..) 返回(-22 错误)。我在哪里做错了?更多信息:- 最后一个 ffmpeg
- Mac 操作系统 x 10.10
- 已安装 libx264
- 在
av_guess_format(...)
之后,我在 m_outFormat 中得到了 audio_codec = CODEC_ID_H264、video_codec = CODEC_ID_NONE、long_name MP4(MPEG-4 Part 14)。 - 在
avformat_alloc_context3(...)
之后,我在 m_formatContext 中得到了 audio_codec_id = video_codec_id = CODEC_ID_NONE。 - 在
avcodec_find_encoder(CODEC_ID_H264)
之后,我在 m_videoCodec 中得到了 name = "libx264", id = CODEC_ID_MPEG1VIDEO。如果您可以说,您到底需要什么,我可以分享更多信息。
最佳答案
我找到了答案......太愚蠢了......
我有旧的头文件,来自旧的 ffmpeg 源,但是有来自新源的库..所以我替换了新的头文件和所有错误都消失了!我不更改上面的代码。
关于c++ - 如何打开AVCodec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193048/