找回密码
 注册
搜索
查看: 1597|回复: 3

[音频编解码] ffmpeg相关,undefined reference to `av_register_all' !!

[复制链接]
发表于 2007-9-3 15:39:10 | 显示全部楼层 |阅读模式
问题:提示undefined reference to `av_register_all ' 等多个函数。

   1 编译成功ffmpeg之后,在C:\msys\local中生成bin,include,lib这3个文件夹子,其中lib文件夹也已经包含 ffmpeg下的avcodec-51.lib、avformat-51.lib、avutil-49.lib这三个文件。
   2 在local文件夹中建立test.c,输入(网上找的):
#include  <stdio.h >
#include "include\ffmpeg\avcodec.h"
#include "include\ffmpeg\avformat.h"
#include "include\ffmpeg\avutil.h"
#include  <windows.h >
#define INBUF_SIZE 4096
//extern void print_error(const char *filename, int err);
void main()
{
const char *filename="1.3gp";
const char *filenameout="2.3gp";
AVCodec *decodecodec;
AVCodec *encodecodec;
int bytesDecoded=0;
AVCodecContext *decodec= NULL;
AVCodecContext *encodec= NULL;
AVFormatContext *pFormatCtx;
AVFormatContext *pFormatCtxForOutput;
AVOutputFormat *pOutputFormat;
AVStream *video_st=NULL;
unsigned char fgcolor[3]={255,128,128};
int frame, outbuf_size = 400000, audioStream, position=0,  got_picture, len,i, frametosave=250, out_size, videoStream, err;
AVFrame *picture;
AVPacket        packet;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE],  *outbuf_ptr;
int audiolen = 0, audiosize = 0;
char buf[1024];
short tmp = 0;
outbuf_ptr = (unsigned char *) malloc(outbuf_size);
/*×&cent;&sup2;á&Euml;ù&Oacute;&ETH;&micro;&Auml;&frac12;&acirc;&Acirc;&euml;±à&Acirc;&euml;&iquest;&acirc;*/
av_register_all();
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

/*&acute;ò&iquest;&ordf;&ETH;è&Ograve;&ordf;&frac12;&acirc;&Acirc;&euml;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;*/
err=av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL);
if(err!=0)
{
   fprintf(stderr,"Can 't open source file\n");
      //print_error(filename, err);
      exit(1);
}
/*&raquo;&ntilde;&Egrave;&iexcl;&para;à&Atilde;&frac12;&Igrave;&aring;&Icirc;&Auml;&frac14;&thorn;&micro;&Auml;&ETH;&Aring;&Iuml;&cent;*/
if(av_find_stream_info(pFormatCtx) <0)
{
     fprintf(stderr,"Can 't find stream info\n");
     exit(1);
}
   
    /*if (av_set_parameters(pFormatCtx, NULL)  < 0)
{
        exit(1);
    }
dump_format(pFormatCtx, 0, filename, 1); */

/*&sup2;é&Otilde;&Ograve;&Icirc;&Auml;&frac14;&thorn;&Ouml;&ETH;&Ecirc;&Ccedil;·&ntilde;&Oacute;&ETH;&Ecirc;&Oacute;&AElig;&micro;*/
videoStream=-1;
for(i=0; i <pFormatCtx- >nb_streams; i++)
if(pFormatCtx- >streams- >codec- >codec_type==CODEC_TYPE_VIDEO)
{
   videoStream=i;
   break;
}
if(videoStream==-1)
{
     fprintf(stderr,"Not found video stream\n");
    exit(1);
}
printf("Video decoding\n");
audioStream = -1;
for(i=0; i <pFormatCtx- >nb_streams; i++)
{
  if(pFormatCtx- >streams- >codec- >codec_type == CODEC_TYPE_AUDIO)
  {
   audioStream = i;
   break;
  }
}

/*&Ntilde;°&Otilde;&Ograve;&Iuml;à&Oacute;&brvbar;&Atilde;&frac12;&Igrave;&aring;&cedil;&ntilde;&Ecirc;&frac12;&micro;&Auml;±à&Acirc;&euml;&frac12;&acirc;&Acirc;&euml;*/
decodec=pFormatCtx- >streams[videoStream]- >codec;
decodecodec = avcodec_find_decoder(decodec- >codec_id);
if (!decodecodec)
{
   fprintf(stderr, "decode codec not found\n");
   exit(1);
}

picture= avcodec_alloc_frame();

/*&acute;ò&iquest;&ordf;&frac12;&acirc;&Acirc;&euml;&iquest;&acirc;*/
if (avcodec_open(decodec, decodecodec)  < 0)
{
   fprintf(stderr, "could not open decode codec\n");
   exit(1);
}

/*&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ecirc;&auml;&sup3;&ouml;±à&Acirc;&euml;&micro;&Auml;&ETH;&Aring;&Iuml;&cent;*/
pOutputFormat=guess_format(NULL,filename,NULL);
pFormatCtxForOutput = av_alloc_format_context();
pFormatCtxForOutput- >oformat = pOutputFormat;

sprintf(pFormatCtxForOutput- >filename,  "%s", filenameout);

video_st = av_new_stream(pFormatCtxForOutput,0);
encodec= video_st- >codec;
encodec- >codec_id = pOutputFormat- >video_codec;
encodec- >codec_type = CODEC_TYPE_VIDEO;
encodec- >bit_rate = pFormatCtx- >bit_rate;
encodec- >width = decodec- >width ;
encodec- >height = decodec- >height ;
encodec- >time_base= decodec- >time_base;
encodec- >gop_size = decodec- >gop_size;
encodec- >max_b_frames=decodec- >max_b_frames;
encodec- >pix_fmt = decodec- >pix_fmt;
/*&frac14;ì&sup2;éencodec&sup2;&Icirc;&Ecirc;&yacute;&Eacute;è&Ouml;&Atilde;*/
if(av_set_parameters(pFormatCtxForOutput, NULL) <0)
{
      fprintf(stderr, "Invalid output format parameters\n");
      exit(1);
}
/*&Otilde;&Ograve;&micro;&frac12;&Iuml;à&Oacute;&brvbar;&micro;&Auml;±à&Acirc;&euml;&iquest;&acirc;*/
encodecodec = avcodec_find_encoder(encodec- >codec_id);
if (!encodecodec)
{
   fprintf(stderr, "encode codec not found\n");
   exit(1);
}
/*&acute;ò&iquest;&ordf;±à&Acirc;&euml;&iquest;&acirc;*/
if ((err=avcodec_open(encodec, encodecodec))  < 0)
{
   fprintf(stderr, "could not open encode codec\n");
      //print_error(filename, err);
   exit(1);
}
/*&acute;&acute;&frac12;¨&ETH;&Acirc;&Eacute;ú&sup3;&Eacute;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;*/
if (!(pOutputFormat- >flags & AVFMT_NOFILE))
{
   if (url_fopen(&pFormatCtxForOutput- >pb, filenameout, URL_WRONLY)  < 0)
   {
    fprintf(stderr, "Could not open  '%s '\n", filenameout);
    exit(1);
   }
}
/*&ETH;&acute;&Icirc;&Auml;&frac14;&thorn;&Iacute;·*/
if (av_write_header(pFormatCtxForOutput)  < 0)
{
      fprintf(stderr, "Could not write header for output file\n");
    exit(1);
    }
frame = 0;
while(av_read_frame(pFormatCtx, &packet) >=0)
{
if(packet.stream_index==videoStream)
{
     len = avcodec_decode_video(decodec, picture, &got_picture,packet.data, packet.size);
        //audiolen = avcodec_decode_audio(decodec, &tmp, &audiosize, packet.data, packet.size);
  //printf("avcodec_decode_audio size %d.\n", audiolen);
   
  if (got_picture)
     {
      
      printf("saving frame %3d\n", frame);
      fflush(stdout);
      sprintf(buf, "frame%d.pgm", frame);         
      picture- >pts=av_rescale(frame,AV_TIME_BASE*(int64_t)encodec- >time_base.num,encodec- >time_base.den);
      picture- >pict_type=0;
      out_size = avcodec_encode_video(encodec, outbuf_ptr, outbuf_size, picture);      
      printf("encoding frame %3d (size=%5d)\n", frame, out_size);
      if (out_size  > 0)
      {
         AVPacket pkt;
         av_init_packet(&pkt);
         if(encodec- >coded_frame && encodec- >coded_frame- >key_frame)
         pkt.flags  &brvbar;= PKT_FLAG_KEY;
         pkt.flags = packet.flags;
         pkt.stream_index= video_st- >index;
         pkt.data= outbuf_ptr;
         pkt.size= out_size;
         av_write_frame(pFormatCtxForOutput, &pkt);
      }
      
      frame++;
  }
  else
  {
      //av_write_frame(pFormatCtxForOutput, &packet);
  }
}
else if (packet.stream_index==audioStream)
{
  printf("audio format.\n");
}
  av_free_packet(&packet);
    }
/*&ETH;&acute;&Icirc;&Auml;&frac14;&thorn;&Icirc;&sup2;&sup2;&cent;&Ccedil;&Ograve;&sup1;&Oslash;±&Otilde;±à&Acirc;&euml;&frac12;&acirc;&Acirc;&euml;&iquest;&acirc;&pound;&not;&Ecirc;&Iacute;·&Aring;&Auml;&Uacute;&acute;&aelig;*/
    av_write_trailer(pFormatCtxForOutput);
    avcodec_close(decodec);
    av_free(decodec);
avcodec_close(encodec);
    av_free(encodec);
    av_free(picture);
if (audioStream != -1)
{
  printf("audiostream %d.\n", audioStream);
}
if (videoStream != -1)
{
  printf("Videostream %d.\n", videoStream);
}
}
//==================================================================================
3 在MinGW32窗口中输入:
gcc test.c 或者 gcc -o test test.c -L/lib/都提示:av_register_all()未定义。
怎么会这样?
 楼主| 发表于 2007-9-3 15:43:29 | 显示全部楼层
第一次没发好重新发一遍:

    问题:提示undefined reference to `av_register_all ' 等多个函数。   
    1 编译成功ffmpeg之后,在C:\msys\local中生成bin,include,lib这3个文件夹子,其中lib文件夹也已经包含 ffmpeg下的avcodec-51.lib、avformat-51.lib、avutil-49.lib这三个文件。   
    2 在local文件夹中建立test.c,输入代码,见附录(网上找的):
    3 在MinGW32窗口中输入: gcc test.c 或者 gcc -o test test.c -L/lib/都提示:av_register_all()未定义。 怎么会这样?

附录:
#include <stdio.h>
#include "include\ffmpeg\avcodec.h"
#include "include\ffmpeg\avformat.h"
#include "include\ffmpeg\avutil.h"
#include <windows.h>
#define INBUF_SIZE 4096
//extern void print_error(const char *filename, int err);
void main()
{
const char *filename="1.3gp";
const char *filenameout="2.3gp";
AVCodec *decodecodec;
AVCodec *encodecodec;
int bytesDecoded=0;
AVCodecContext *decodec= NULL;
AVCodecContext *encodec= NULL;
AVFormatContext *pFormatCtx;
AVFormatContext *pFormatCtxForOutput;
AVOutputFormat *pOutputFormat;
AVStream *video_st=NULL;
unsigned char fgcolor[3]={255,128,128};
int frame, outbuf_size = 400000, audioStream, position=0,  got_picture, len,i, frametosave=250, out_size, videoStream, err;
AVFrame *picture;
AVPacket        packet;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE],  *outbuf_ptr;
int audiolen = 0, audiosize = 0;
char buf[1024];
short tmp = 0;
outbuf_ptr = (unsigned char *) malloc(outbuf_size);
/*×&cent;&sup2;á&Euml;ù&Oacute;&ETH;&micro;&Auml;&frac12;&acirc;&Acirc;&euml;±à&Acirc;&euml;&iquest;&acirc;*/
av_register_all();
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

/*&acute;ò&iquest;&ordf;&ETH;è&Ograve;&ordf;&frac12;&acirc;&Acirc;&euml;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;*/
err=av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL);
if(err!=0)
{
   fprintf(stderr,"Can't open source file\n");
      //print_error(filename, err);
      exit(1);
}
/*&raquo;&ntilde;&Egrave;&iexcl;&para;à&Atilde;&frac12;&Igrave;&aring;&Icirc;&Auml;&frac14;&thorn;&micro;&Auml;&ETH;&Aring;&Iuml;&cent;*/
if(av_find_stream_info(pFormatCtx)<0)
{
     fprintf(stderr,"Can't find stream info\n");
     exit(1);
}
   
    /*if (av_set_parameters(pFormatCtx, NULL) < 0)
{
        exit(1);
    }
dump_format(pFormatCtx, 0, filename, 1); */

/*&sup2;é&Otilde;&Ograve;&Icirc;&Auml;&frac14;&thorn;&Ouml;&ETH;&Ecirc;&Ccedil;·&ntilde;&Oacute;&ETH;&Ecirc;&Oacute;&AElig;&micro;*/
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams->codec->codec_type==CODEC_TYPE_VIDEO)
{
   videoStream=i;
   break;
}
if(videoStream==-1)
{
     fprintf(stderr,"Not found video stream\n");
    exit(1);
}
printf("Video decoding\n");
audioStream = -1;
for(i=0; i<pFormatCtx->nb_streams; i++)
{
  if(pFormatCtx->streams->codec->codec_type == CODEC_TYPE_AUDIO)
  {
   audioStream = i;
   break;
  }
}

/*&Ntilde;°&Otilde;&Ograve;&Iuml;à&Oacute;&brvbar;&Atilde;&frac12;&Igrave;&aring;&cedil;&ntilde;&Ecirc;&frac12;&micro;&Auml;±à&Acirc;&euml;&frac12;&acirc;&Acirc;&euml;*/
decodec=pFormatCtx->streams[videoStream]->codec;
decodecodec = avcodec_find_decoder(decodec->codec_id);
if (!decodecodec)
{
   fprintf(stderr, "decode codec not found\n");
   exit(1);
}

picture= avcodec_alloc_frame();

/*&acute;ò&iquest;&ordf;&frac12;&acirc;&Acirc;&euml;&iquest;&acirc;*/
if (avcodec_open(decodec, decodecodec) < 0)
{
   fprintf(stderr, "could not open decode codec\n");
   exit(1);
}

/*&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ecirc;&auml;&sup3;&ouml;±à&Acirc;&euml;&micro;&Auml;&ETH;&Aring;&Iuml;&cent;*/
pOutputFormat=guess_format(NULL,filename,NULL);
pFormatCtxForOutput = av_alloc_format_context();
pFormatCtxForOutput->oformat = pOutputFormat;

sprintf(pFormatCtxForOutput->filename,  "%s", filenameout);

video_st = av_new_stream(pFormatCtxForOutput,0);
encodec= video_st->codec;
encodec->codec_id = pOutputFormat->video_codec;
encodec->codec_type = CODEC_TYPE_VIDEO;
encodec->bit_rate = pFormatCtx->bit_rate;
encodec->width = decodec->width ;
encodec->height = decodec->height ;
encodec->time_base= decodec->time_base;
encodec->gop_size = decodec->gop_size;
encodec->max_b_frames=decodec->max_b_frames;
encodec->pix_fmt = decodec->pix_fmt;
/*&frac14;ì&sup2;éencodec&sup2;&Icirc;&Ecirc;&yacute;&Eacute;è&Ouml;&Atilde;*/
if(av_set_parameters(pFormatCtxForOutput, NULL)<0)
{
      fprintf(stderr, "Invalid output format parameters\n");
      exit(1);
}
/*&Otilde;&Ograve;&micro;&frac12;&Iuml;à&Oacute;&brvbar;&micro;&Auml;±à&Acirc;&euml;&iquest;&acirc;*/
encodecodec = avcodec_find_encoder(encodec->codec_id);
if (!encodecodec)
{
   fprintf(stderr, "encode codec not found\n");
   exit(1);
}
/*&acute;ò&iquest;&ordf;±à&Acirc;&euml;&iquest;&acirc;*/
if ((err=avcodec_open(encodec, encodecodec)) < 0)
{
   fprintf(stderr, "could not open encode codec\n");
      //print_error(filename, err);
   exit(1);
}
/*&acute;&acute;&frac12;¨&ETH;&Acirc;&Eacute;ú&sup3;&Eacute;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;*/
if (!(pOutputFormat->flags & AVFMT_NOFILE))
{
   if (url_fopen(&pFormatCtxForOutput->pb, filenameout, URL_WRONLY) < 0)
   {
    fprintf(stderr, "Could not open '%s'\n", filenameout);
    exit(1);
   }
}
/*&ETH;&acute;&Icirc;&Auml;&frac14;&thorn;&Iacute;·*/
if (av_write_header(pFormatCtxForOutput) < 0)
{
      fprintf(stderr, "Could not write header for output file\n");
    exit(1);
    }
frame = 0;
while(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==videoStream)
{
     len = avcodec_decode_video(decodec, picture, &got_picture,packet.data, packet.size);
        //audiolen = avcodec_decode_audio(decodec, &tmp, &audiosize, packet.data, packet.size);
  //printf("avcodec_decode_audio size %d.\n", audiolen);
  
  if (got_picture)
     {
      
      printf("saving frame %3d\n", frame);
      fflush(stdout);
      sprintf(buf, "frame%d.pgm", frame);        
      picture->pts=av_rescale(frame,AV_TIME_BASE*(int64_t)encodec->time_base.num,encodec->time_base.den);
      picture->pict_type=0;
      out_size = avcodec_encode_video(encodec, outbuf_ptr, outbuf_size, picture);     
      printf("encoding frame %3d (size=%5d)\n", frame, out_size);
      if (out_size > 0)
      {
         AVPacket pkt;
         av_init_packet(&pkt);
         if(encodec->coded_frame && encodec->coded_frame->key_frame)
         pkt.flags |= PKT_FLAG_KEY;
         pkt.flags = packet.flags;
         pkt.stream_index= video_st->index;
         pkt.data= outbuf_ptr;
         pkt.size= out_size;
         av_write_frame(pFormatCtxForOutput, &pkt);
      }
      
      frame++;
  }
  else
  {
      //av_write_frame(pFormatCtxForOutput, &packet);
  }
}
else if (packet.stream_index==audioStream)
{
  printf("audio format.\n");
}
  av_free_packet(&packet);
    }
/*&ETH;&acute;&Icirc;&Auml;&frac14;&thorn;&Icirc;&sup2;&sup2;&cent;&Ccedil;&Ograve;&sup1;&Oslash;±&Otilde;±à&Acirc;&euml;&frac12;&acirc;&Acirc;&euml;&iquest;&acirc;&pound;&not;&Ecirc;&Iacute;·&Aring;&Auml;&Uacute;&acute;&aelig;*/
    av_write_trailer(pFormatCtxForOutput);
    avcodec_close(decodec);
    av_free(decodec);
avcodec_close(encodec);
    av_free(encodec);
    av_free(picture);
if (audioStream != -1)
{
  printf("audiostream %d.\n", audioStream);
}
if (videoStream != -1)
{
  printf("Videostream %d.\n", videoStream);
}
}
点评回复

使用道具 举报

 楼主| 发表于 2007-9-3 15:51:46 | 显示全部楼层
最后再试一次:
    问题:提示undefined reference to `av_register_all ' 等多个函数。        
    1 编译成功ffmpeg之后,在C:\msys\local中生成bin,include,lib这3个文件夹子,其中lib文件夹也已经包含 ffmpeg下的avcodec-51.lib、avformat-51.lib、avutil-49.lib这三个文件。        
    2 在local文件夹中建立test.c,输入代码,见附录(网上找的):   
    3 在MinGW32窗口中输入: gcc test.c 或者 gcc -o test test.c -L/lib/都提示:av_register_all()未定义。 怎么会这样?

附录:

#include <stdio.h>
#include "include\ffmpeg\avcodec.h"
#include "include\ffmpeg\avformat.h"
#include "include\ffmpeg\avutil.h"
#include <windows.h>
#define INBUF_SIZE 4096
//extern void print_error(const char *filename, int err);
void main()
{
const char *filename="1.3gp";
const char *filenameout="2.3gp";
AVCodec *decodecodec;
AVCodec *encodecodec;
int bytesDecoded=0;
AVCodecContext *decodec= NULL;
AVCodecContext *encodec= NULL;
AVFormatContext *pFormatCtx;
AVFormatContext *pFormatCtxForOutput;
AVOutputFormat *pOutputFormat;
AVStream *video_st=NULL;
unsigned char fgcolor[3]={255,128,128};
int frame, outbuf_size = 400000, audioStream, position=0,  got_picture, len,i, frametosave=250, out_size, videoStream, err;
AVFrame *picture;
AVPacket        packet;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE],  *outbuf_ptr;
int audiolen = 0, audiosize = 0;
char buf[1024];
short tmp = 0;
outbuf_ptr = (unsigned char *) malloc(outbuf_size);
/**/
av_register_all();
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

/**/
err=av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL);
if(err!=0)
{
   fprintf(stderr,"Can't open source file\n");
      //print_error(filename, err);
      exit(1);
}
/**/
if(av_find_stream_info(pFormatCtx)<0)
{
     fprintf(stderr,"Can't find stream info\n");
     exit(1);
}
   
    /*if (av_set_parameters(pFormatCtx, NULL) < 0)
{
        exit(1);
    }
dump_format(pFormatCtx, 0, filename, 1); */

/**/
videoStream=-1;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams->codec->codec_type==CODEC_TYPE_VIDEO)
{
   videoStream=i;
   break;
}
if(videoStream==-1)
{
     fprintf(stderr,"Not found video stream\n");
    exit(1);
}
printf("Video decoding\n");
audioStream = -1;
for(i=0; i<pFormatCtx->nb_streams; i++)
{
  if(pFormatCtx->streams->codec->codec_type == CODEC_TYPE_AUDIO)
  {
   audioStream = i;
   break;
  }
}

/**/
decodec=pFormatCtx->streams[videoStream]->codec;
decodecodec = avcodec_find_decoder(decodec->codec_id);
if (!decodecodec)
{
   fprintf(stderr, "decode codec not found\n");
   exit(1);
}

picture= avcodec_alloc_frame();

/**/
if (avcodec_open(decodec, decodecodec) < 0)
{
   fprintf(stderr, "could not open decode codec\n");
   exit(1);
}

/**/
pOutputFormat=guess_format(NULL,filename,NULL);
pFormatCtxForOutput = av_alloc_format_context();
pFormatCtxForOutput->oformat = pOutputFormat;

sprintf(pFormatCtxForOutput->filename,  "%s", filenameout);

video_st = av_new_stream(pFormatCtxForOutput,0);
encodec= video_st->codec;
encodec->codec_id = pOutputFormat->video_codec;
encodec->codec_type = CODEC_TYPE_VIDEO;
encodec->bit_rate = pFormatCtx->bit_rate;
encodec->width = decodec->width ;
encodec->height = decodec->height ;
encodec->time_base= decodec->time_base;
encodec->gop_size = decodec->gop_size;
encodec->max_b_frames=decodec->max_b_frames;
encodec->pix_fmt = decodec->pix_fmt;
/**/
if(av_set_parameters(pFormatCtxForOutput, NULL)<0)
{
      fprintf(stderr, "Invalid output format parameters\n");
      exit(1);
}
/**/
encodecodec = avcodec_find_encoder(encodec->codec_id);
if (!encodecodec)
{
   fprintf(stderr, "encode codec not found\n");
   exit(1);
}
/**/
if ((err=avcodec_open(encodec, encodecodec)) < 0)
{
   fprintf(stderr, "could not open encode codec\n");
      //print_error(filename, err);
   exit(1);
}
/**/
if (!(pOutputFormat->flags & AVFMT_NOFILE))
{
   if (url_fopen(&pFormatCtxForOutput->pb, filenameout, URL_WRONLY) < 0)
   {
    fprintf(stderr, "Could not open '%s'\n", filenameout);
    exit(1);
   }
}
/**/
if (av_write_header(pFormatCtxForOutput) < 0)
{
      fprintf(stderr, "Could not write header for output file\n");
    exit(1);
    }
frame = 0;
while(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==videoStream)
{
     len = avcodec_decode_video(decodec, picture, &got_picture,packet.data, packet.size);
        //audiolen = avcodec_decode_audio(decodec, &tmp, &audiosize, packet.data, packet.size);
  //printf("avcodec_decode_audio size %d.\n", audiolen);
  
  if (got_picture)
     {
      
      printf("saving frame %3d\n", frame);
      fflush(stdout);
      sprintf(buf, "frame%d.pgm", frame);        
      picture->pts=av_rescale(frame,AV_TIME_BASE*(int64_t)encodec->time_base.num,encodec->time_base.den);
      picture->pict_type=0;
      out_size = avcodec_encode_video(encodec, outbuf_ptr, outbuf_size, picture);     
      printf("encoding frame %3d (size=%5d)\n", frame, out_size);
      if (out_size > 0)
      {
         AVPacket pkt;
         av_init_packet(&pkt);
         if(encodec->coded_frame && encodec->coded_frame->key_frame)
         pkt.flags |= PKT_FLAG_KEY;
         pkt.flags = packet.flags;
         pkt.stream_index= video_st->index;
         pkt.data= outbuf_ptr;
         pkt.size= out_size;
         av_write_frame(pFormatCtxForOutput, &pkt);
      }
      
      frame++;
  }
  else
  {
      //av_write_frame(pFormatCtxForOutput, &packet);
  }
}
else if (packet.stream_index==audioStream)
{
  printf("audio format.\n");
}
  av_free_packet(&packet);
    }
/**/
    av_write_trailer(pFormatCtxForOutput);
    avcodec_close(decodec);
    av_free(decodec);
avcodec_close(encodec);
    av_free(encodec);
    av_free(picture);
if (audioStream != -1)
{
  printf("audiostream %d.\n", audioStream);
}
if (videoStream != -1)
{
  printf("Videostream %d.\n", videoStream);
}
}
点评回复

使用道具 举报

 楼主| 发表于 2007-9-3 15:52:39 | 显示全部楼层
[em11][em13][em03]
点评回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-11-20 18:44 , Processed in 0.067054 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表