c++ - 如何打开AVCodec?

标签 c++ macos encoding ffmpeg libx264

我厌倦了搜索关于这个主题的解决方案。有人可以帮忙吗?

类型:

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(M​​PEG-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/

相关文章:

java - 如何使用 Runtime.getRuntime().exec() 打开名称中带有空格的应用程序?

macos - 启动 Postgres 服务器

encoding - 操作系统如何知道文件的编码

java - 使用 Java 编写不同字符编码的 XML

c++ - vector 清除与调整大小

java - 如何找到 2 个单链表的公共(public)节点?

c++ - 如何在 Windows 窗体应用程序中删除现有矩形之一

c++ - libcoi_device.so.0 未找到 Intel 19.0.4 OpenMP 5.0 卸载编译错误

macos - Composer 不适用于oh-my-zsh

jquery - multipart/form-data 使用什么编码?是否可以使用 JQuery 对数据进行编码?