首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

崩溃了好几天的编码有关问题!高手指教

2012-04-09 
崩溃了好几天的编码问题!高手指教!C/C++ codevoid video_encode_example(const char * inputfilename , co

崩溃了好几天的编码问题!高手指教!

C/C++ code
void video_encode_example(const char * inputfilename , const char *outputfilename)  //编码{    AVCodec *codec;    AVCodecContext *c= NULL;    int i, out_size, size, outbuf_size;    FILE *f_out,*f_in;    AVFrame *picture;    uint8_t *outbuf, *picture_buf;    int frame = 0;    int insize = 0;    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;    printf("Video encoding\n");    /* find the mpeg1 video encoder */    codec = avcodec_find_encoder(CODEC_ID_H264);  //函数用于查找一个与codec ID相匹配的已注册的编码器。    if (!codec)    {        fprintf(stderr, "codec not found\n");        exit(1);    }    c = avcodec_alloc_context(); //函数用于分配一个AVCodecContext并设置默认值,如果失败返回NULL,并可用av_free()进行释放。    picture= avcodec_alloc_frame(); //函数用于分配一个AVFrame并设置默认值,如果失败返回NULL,并可用av_free()进行释放。    /* put sample parameters */    c->bit_rate = 400000;  //设置采样参数,即比特率    /* resolution must be a multiple of two */// 设置分辨率,必须是2的倍数    c->width = 352;    c->height = 288;    /* frames per second *///设置帧率//该帧率为25,其实timebase = 1/framerate,分别为分子和分母。    c->time_base.den = 25;     c->time_base.num = 1;    c->gop_size = 10; /* emit one intra frame every ten frames *///设置GOP大小,该值表示每10帧会插入一个I帧(intra frame)。    c->max_b_frames=1;//设置B帧最大数, c->max_b_frames = 1; 该值表示在两个非B帧之间,所允许插入的B帧的最大帧数。    c->pix_fmt = PIX_FMT_YUV420P;  //设置像素格式,该值将像素格式设置为YUV420P。    /* open it */    if (avcodec_open(c, codec) < 0)  //函数用给定的AVCodec来初始化AVCodecContext    {        fprintf(stderr, "could not open codec\n");        exit(1);    }    /* the codec gives us the frame size, in samples */    f_out = fopen(outputfilename, "wb");  //接着是打开文件    if (!f_out)     {        fprintf(stderr, "could not open %s\n", outputfilename);        exit(1);    }    f_in = fopen(inputfilename,"rb");    if (!f_in)    {        fprintf(stderr, "could not open %s\n", inputfilename);        exit(1);        }    /* alloc image and output buffer */    outbuf_size = 100000;   //分配图像和输出缓存    outbuf = malloc(outbuf_size);    size = c->width * c->height;    picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */    picture->data[0] = picture_buf;    picture->data[1] = picture->data[0] + size;    picture->data[2] = picture->data[1] + size / 4;    picture->linesize[0] = c->width;    picture->linesize[1] = c->width / 2;    picture->linesize[2] = c->width / 2;    for (;;)    {        insize = fread(inbuf,1,INBUF_SIZE,f_in);        if (insize == 0)        {            break;        }        inbuf_ptr = inbuf;        while (size > 0)        {            avpicture_fill(picture,inbuf_ptr, PIX_FMT_YUV420P,                c->width, c->height);            out_size = avcodec_encode_video(c,outbuf,outbuf_size,picture);            if (out_size < 0 )            {                fprintf(stderr, "Error while encoder frame %d\n", frame);                exit(1);            }            if (outbuf)            {                printf("saving frame %3d\n", frame);                printf("encoding frame %3d (size=%5d)\n", frame, out_size);                fwrite(outbuf, 1, out_size, f_out);  //将编码完的数据写入到文件里            }        }        }    free(picture_buf);    free(outbuf);    avcodec_close(c);    av_free(c);    av_free(picture);    printf("\n");}


[解决办法]
这个去专题开发技术版的 多媒体流媒体区问吧。

[解决办法]
ffmpeg问题
[解决办法]
太长了,这个是干嘛用的?好奇……
------解决方案--------------------


探讨
太长了,这个是干嘛用的?好奇……

[解决办法]
帮你Google了一下,像下面这样改一下试试看:
C/C++ code
  // 旧  outbuf_size = 100000;   //分配图像和输出缓存  outbuf = malloc(outbuf_size);  // 新 outbuf_size = 200000; outbuf = av_malloc( outbuf_size );
[解决办法]
用ffmpeg进行h264编码的前提是安装了x264库,并在编译ffmpeg库的时候打开--enable-gpl --enable-libx264选项,否则是不能进行h264压缩编码的。
[解决办法]
能出画面说明x264库已经有了,只是你的参数设置有些问题,加上下面的设置试试吧:
C/C++ code
c->partitions= X264_PART_I8X8;    c->b_quant_factor= 1.30f;    c->i_quant_factor= 1.0 / 1.40f;    c->qcompress=0.9;
[解决办法]
探讨
引用:

用ffmpeg进行h264编码的前提是安装了x264库,并在编译ffmpeg库的时候打开--enable-gpl --enable-libx264选项,否则是不能进行h264压缩编码的。

我编码成功了。能正常播放了,哈哈!
但有些问题:当其他格式的视频编码成264的格式,我知道是先转成yuv420p的,但我想知道是先解码转成yuv的在编码,还是有其他的函数操……

[解决办法]
不太熟悉。
[解决办法]
探讨
这个去专题开发技术版的 多媒体流媒体区问吧。

热点排行