致命错误:libavcodec/avcodec.h没有终止此类文件或目录编译

致命错误:libavcodec/avcodec.h没有终止此类文件或目录编译,c,ffmpeg,C,Ffmpeg,我正在尝试使用gcc执行tutorial01.c,我将gcc和tutorial01.c与libavcodec和libavformat及其关联文件放在同一个文件夹中,这会导致此错误 致命错误:libavcodec/avcodec.h没有终止此类文件或目录编译 当我通过ubuntu 12.04中的终端运行gcc-o tutorial01 tutorial01.c-lavformat-lavcodec-lz时 代码是 #include libavcodec/avcodec.h #include li

我正在尝试使用gcc执行tutorial01.c,我将gcc和tutorial01.c与libavcodec和libavformat及其关联文件放在同一个文件夹中,这会导致此错误

致命错误:libavcodec/avcodec.h没有终止此类文件或目录编译

当我通过ubuntu 12.04中的终端运行
gcc-o tutorial01 tutorial01.c-lavformat-lavcodec-lz

代码是

#include  libavcodec/avcodec.h
#include libavformat/avformat.h
#include stdio.h

void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
 {
  FILE *pFile;
  char szFilename[32];
  int  y;

  // Open file
  sprintf(szFilename, "frame%d.ppm", iFrame);
  pFile=fopen(szFilename, "wb");
  if(pFile==NULL)
    return;

  // Write header
  fprintf(pFile, "P6\n%d %d\n255\n", width, height);
  // Write pixel data
  for(y=0; y<height; y++)
    fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);

  // Close file
  fclose(pFile);
}

int main(int argc, char *argv[])
 {
  AVFormatContext *pFormatCtx;
  int             i, videoStream;
  AVCodecContext  *pCodecCtx;
  AVCodec         *pCodec;
  AVFrame         *pFrame; 
  AVFrame         *pFrameRGB;
  AVPacket        packet;
  int             frameFinished;
  int             numBytes;
  uint8_t         *buffer;

  if(argc < 2) 
  {
    printf("Please provide a movie file\n");
    return -1;
  }

  // Register all formats and codecs
  av_register_all();
  // Open video file
  if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
    return -1; // Couldn't open file

  // Retrieve stream information
  if(av_find_stream_info(pFormatCtx)<0)
    return -1; // Couldn't find stream information

  // Dump information about file onto standard error
  dump_format(pFormatCtx, 0, argv[1], 0);
  // Find the first video stream
  videoStream=-1;
  for(i=0; i<pFormatCtx->nb_streams; i++)
    if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
    {
      videoStream=i;
      break;
    }
  if(videoStream==-1)
    return -1; // Didn't find a video stream

  // Get a pointer to the codec context for the video stream
  pCodecCtx=pFormatCtx->streams[videoStream]->codec;
  // Find the decoder for the video stream
  pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  if(pCodec==NULL) 
  {
    fprintf(stderr, "Unsupported codec!\n");
    return -1; // Codec not found
  }

  // Open codec
  if(avcodec_open(pCodecCtx, pCodec)<0)
    return -1; // Could not open codec

  // Allocate video frame
  pFrame=avcodec_alloc_frame();

  // Allocate an AVFrame structure
  pFrameRGB=avcodec_alloc_frame();

  if(pFrameRGB==NULL)
    return -1;

  // Determine required buffer size and allocate buffer              
 numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
                  pCodecCtx->height);

  buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

  // Assign appropriate parts of buffer to image planes in pFrameRGB
  // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
  // of AVPicture
  avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
         pCodecCtx->width, pCodecCtx->height);

  // Read frames and save first five frames to disk
  i=0;
  while(av_read_frame(pFormatCtx, &packet)>=0)
 {
    // Is this a packet from the video stream?
    if(packet.stream_index==videoStream) 
    {
      // Decode video frame
      avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
               packet.data, packet.size);

      // Did we get a video frame?
      if(frameFinished) 
      {
        // Convert the image from its native format to RGB




    img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
                    (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
                    pCodecCtx->height);




    // Save the frame to disk

    if(++i<=5)
  SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, 
            i);
      }
   }

    // Free the packet that was allocated by av_read_frame
    av_free_packet(&packet);
  }

  // Free the RGB image
  av_free(buffer);
  av_free(pFrameRGB);

  // Free the YUV frame
  av_free(pFrame);

 // Close the codec
  avcodec_close(pCodecCtx);


  // Close the video file
  av_close_input_file(pFormatCtx);


  return 0;

}
#包括libavcodec/avcodec.h
#包括libavformat/avformat.h
#包括stdio.h
无效存储框(AVFrame*pFrame,int-width,int-height,int-iFrame)
{
文件*pFile;
char-szFilename[32];
int-y;
//打开文件
sprintf(szFilename,“帧%d.ppm”,iFrame);
pFile=fopen(szFilename,“wb”);
if(pFile==NULL)
返回;
//写头
fprintf(pFile,“P6\n%d%d\n255\n”,宽度、高度);
//写入像素数据
用于(y=0;ydata[0]+y*pFrame->线宽[0],1,宽度*3,pFile);
//关闭文件
fclose(pFile);
}
int main(int argc,char*argv[])
{
AVFormatContext*pFormatCtx;
inti,视频流;
AVCodecContext*pCodecCtx;
AVCodec*pCodec;
AVFrame*pFrame;
AVFrame*pFrameRGB;
数据包;
int框架完成;
整数单位;
uint8_t*缓冲器;
如果(argc<2)
{
printf(“请提供电影文件\n”);
返回-1;
}
//注册所有格式和编解码器
av_寄存器_all();
//打开视频文件
如果(av打开输入文件(&pFormatCtx,argv[1],NULL,0,NULL)!=0)
return-1;//无法打开文件
//检索流信息
如果(av)查找流信息(pFormatCtx)流[视频流]->编解码器;
//查找视频流的解码器
pCodec=avcodec\u find\u解码器(pCodecCtx->codec\u id);
if(pCodec==NULL)
{
fprintf(stderr,“不支持的编解码器!\n”);
return-1;//找不到编解码器
}
//开放式编解码器
如果(avcodec_打开(PCODECTX,pCodec)宽度,
pCodecCtx->高度);
缓冲区=(uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
//将缓冲区的适当部分指定给pFrameRGB中的图像平面
//请注意,pFrameRGB是一个AVFrame,但AVFrame是一个超集
//AVPicture的应用
avpicture_填充((avpicture*)pFrameRGB、缓冲器、PIX_FMT_RGB24、,
PCODECTX->宽度,PCODECTX->高度);
//读取帧并将前五帧保存到磁盘
i=0;
而(av_读取_帧(pFormatCtx和数据包)>=0)
{
//这是来自视频流的数据包吗?
if(数据包流\索引==视频流)
{
//解码视频帧
avcodec_解码_视频(pCodecCtx、pFrame和frameFinished,
数据包(packet.data,packet.size);
//我们有录像带吗?
如果(框架完成)
{
//将图像从本机格式转换为RGB
img_转换((AVPicture*)pFrameRGB、PIX_FMT_RGB24、,
(AVPicture*)pFrame,PCODECTX->pix_fmt,PCODECTX->宽度,
pCodecCtx->高度);
//将帧保存到磁盘
如果(++I宽度,PCODECTX->高度,
i) );
}
}
//释放av_read_帧分配的数据包
av_免费_数据包(&数据包);
}
//释放RGB图像
无AVU(缓冲区);
无AVU(pFrameRGB);
//释放YUV框架
无AVU(pFrame);
//关闭编解码器
avcodec_关闭(PCODECTX);
//关闭视频文件
av_关闭_输入_文件(pFormatCtx);
返回0;
}

您需要将libavcodec的路径和libavformat include文件添加到命令行。找到include/目录并添加

-Ipath/to/include
对于每个相关的include文件


您还需要使用-L对库目录执行相同的操作。

该错误是由于路径中的错误而发生的

-L/home/yourpath/ffmpeg\u build/lib/

-I/home/yourpath/ffmpeg\u build/include/

ffmpeg_build–将在其中生成文件并安装库

例如:

创建文件“execute.sh”

现在打开文件并粘贴以下代码:

g++ -Wno-format-zero-length -Wno-write-strings -L/home/yourpath/ffmpeg_build/lib/ -I/home/yourpath/ffmpeg_build/include/ -o output program.cpp -lavcodec -lavformat -lavutil -lavdevice -lswresample -lswscale -lm -lva -lpthread -lvorbis -lvpx -lopus -lz -lpostproc -ldl -lfdk-aac -lmp3lame -lvorbisenc -lvorbisfile -lx264 -ltheora -ltheoraenc -ltheoradec -ldl -lrt -lx265 -lbz2
和类型:sh execute.sh和binary将以“output”的名称创建 然后键入./output作为输出


<>注释:上面的C++,对于C代码更改G++到GCC

< P>这取决于你如何安装FFMPEG。 通常,您可以通过3个步骤编译和安装:

  • ./configure--prefix=/yourpath/ffmpeg
  • 制造
  • 安装
  • 如果您没有配置“-prefix”参数,它将安装在您的默认路径上。(在我的Ubuntu上,我发现ffmpeg已安装在“/usr/local/”)

    正确安装ffmpeg后,可以使用以下命令编译代码:

    gcc testFfmpeg.c -I/yourpath/ffmpeg/include/ -L/yourpath/ffmpeg/lib/
    


    请使用代码格式(四个前导空格)对于你的代码,请!你编译并构建了libavcodec和libavformat吗?或者你正在为你的平台使用预构建的开发包吗?我使用了预构建的开发包。如果你这样做了,你是如何解决的?你能详细说明吗?假设我到mk文件的路径是:D:\ffmpeg\jni..然后我运行了一个NDK构建来编译它,但我得到了这个错误,因为没有这样的file。如何添加此路径?
    gcc testFfmpeg.c -I/usr/local/include/ -L/usr/local/lib/