Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用ffmpeg编码到.MP4时出现问题_C++_Ffmpeg_Libavcodec_Mpeg 4_Libavformat - Fatal编程技术网

C++ 使用ffmpeg编码到.MP4时出现问题

C++ 使用ffmpeg编码到.MP4时出现问题,c++,ffmpeg,libavcodec,mpeg-4,libavformat,C++,Ffmpeg,Libavcodec,Mpeg 4,Libavformat,所以我对这个有点陌生,我正在编写一个WIN32应用程序,将视频录制到.AVI,然后我计划使用ffmpeg编码到.MP4。根据我找到的一个样本,我走到了这一步。它编译并说它编码并写入了文件,但输出文件无法打开和播放。我试着像原始样本一样使用MPEG1编码,但它只在几秒钟内给我奇怪的颜色 我是不是遗漏了该处理文件的内容 任何有编码/ffmpeg经验的人,一些指点、建议或帮助都会让我非常感激。提前谢谢 int _tmain(int argc, _TCHAR* argv[]) { AVForm

所以我对这个有点陌生,我正在编写一个WIN32应用程序,将视频录制到.AVI,然后我计划使用ffmpeg编码到.MP4。根据我找到的一个样本,我走到了这一步。它编译并说它编码并写入了文件,但输出文件无法打开和播放。我试着像原始样本一样使用MPEG1编码,但它只在几秒钟内给我奇怪的颜色

我是不是遗漏了该处理文件的内容

任何有编码/ffmpeg经验的人,一些指点、建议或帮助都会让我非常感激。提前谢谢

int _tmain(int argc, _TCHAR* argv[])
{

    AVFormatContext *pFormatCtx;
    int codec_id = CODEC_ID_MPEG4;
    char filename [] ="C:\\wav\\test2.flv";
// Open video file
     AVCodec *codec;
     AVCodecContext *c= NULL;
     int i, out_size, x, y, outbuf_size;
    FILE *f;
     AVFrame *picture;
    uint8_t *outbuf;
     int had_output=0;

    av_register_all();
   printf("Encode video file %s\n", filename);

    codec = avcodec_find_encoder(CODEC_ID_H264);
    if (!codec) {
         fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    picture= avcodec_alloc_frame();

   /* put sample parameters */
   c->bit_rate = 40000;
   //c->bit_rate_tolerance=30;
     /* resolution must be a multiple of two */
    c->width = 352;
    c->height = 288;
    /* frames per second */

    c->time_base.den=  25;
    c->time_base.num= 1;
     c->gop_size = 10; /* emit one intra frame every ten frames */
   c->max_b_frames=1;
     c->pix_fmt = PIX_FMT_YUV420P;

    if(codec_id == CODEC_ID_H264)
        av_opt_set(c->priv_data, "preset", "slow", 0);
/* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "could not open codec\n");
       exit(1);
    }


     f = fopen(filename, "wb");
     if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
         exit(1);
     }

    /* alloc image and output buffer */
    outbuf_size = 100000 + 12*c->width*c->height;
     outbuf = (uint8_t*)malloc(outbuf_size); //CHANGED

     /* the image can be allocated by any means and av_image_alloc() is
      * just the most convenient way if av_malloc() is to be used */
    av_image_alloc(picture->data, picture->linesize,
                   c->width, c->height, c->pix_fmt, 1);

     /* encode 1 second of video */
     for(i=0;i<25;i++) {
         fflush(stdout);
         /* prepare a dummy image */
        /* Y */
         for(y=0;y<c->height;y++) {
            for(x=0;x<c->width;x++) {
                picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
            }
         }

        /* Cb and Cr */
        for(y=0;y<c->height/2;y++) {
            for(x=0;x<c->width/2;x++) {
                picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
                picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
            }
        }

         /* encode the image */
        out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
         had_output |= out_size;
        printf("encoding frame %3d (size=%5d)\n", i, out_size);
        fwrite(outbuf, 1, out_size, f);
     }

     /* get the delayed frames */
     for(; out_size || !had_output; i++) {
         fflush(stdout);

        out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
        had_output |= out_size;
         printf("write frame %3d (size=%5d)\n", i, out_size);
       fwrite(outbuf, 1, out_size, f);
     }

     /* add sequence end code to have a real mpeg file */
     outbuf[0] = 0x00;
     outbuf[1] = 0x00;
     outbuf[2] = 0x01;
   outbuf[3] = 0xb7;
     fwrite(outbuf, 1, 4, f);
     fclose(f);
        fclose(p);
    free(outbuf);

     avcodec_close(c);
     av_free(c);
     av_free(picture->data[0]);
    av_free(picture);
     printf("\n");

    return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
AVFormatContext*pFormatCtx;
int codec_id=codec_id_MPEG4;
char filename[]=“C:\\wav\\test2.flv”;
//打开视频文件
AVCodec*编解码器;
AVCodecContext*c=NULL;
int i,out_大小,x,y,exputf_大小;
文件*f;
AVFrame*图片;
uint8_t*f;
int有_输出=0;
av_寄存器_all();
printf(“编码视频文件%s\n”,文件名);
codec=avcodec\u find\u编码器(codec\u ID\u H264);
如果(!编解码器){
fprintf(stderr,“找不到编解码器”);
出口(1);
}
c=avcodec\u alloc\u context3(编解码器);
picture=avcodec_alloc_frame();
/*输入样本参数*/
c->比特率=40000;
//c->比特率公差=30;
/*分辨率必须是2的倍数*/
c->宽度=352;
c->高度=288;
/*每秒帧数*/
c->time_base.den=25;
c->time_base.num=1;
c->gop_size=10;/*每十帧发射一帧内*/
c->max_b_frames=1;
c->pix_fmt=pix_fmt_YUV420P;
if(编解码器id==编解码器id\U H264)
av_opt_set(c->priv_数据,“预设”,“慢速”,0);
/*打开它*/
if(avcodec_open2(c,codec,NULL)<0){
fprintf(stderr,“无法打开编解码器”\n);
出口(1);
}
f=fopen(文件名,“wb”);
如果(!f){
fprintf(stderr,“无法打开%s\n”,文件名);
出口(1);
}
/*alloc映像和输出缓冲区*/
突出尺寸=100000+12*c->宽度*c->高度;
突发=(uint8_t*)malloc(突发大小);//已更改
/*可以通过任何方式分配映像,并且av_image_alloc()是
*如果要使用av_malloc(),这是最方便的方法*/
av_图像_alloc(图片->数据,图片->线条尺寸,
c->宽度,c->高度,c->pix_fmt,1);
/*编码1秒的视频*/
对于(i=0;idata[0][y*图片->线宽[0]+x]=x+y+i*3;
}
}
/*Cb和Cr*/
对于(y=0;yheight/2;y++){
对于(x=0;xwidth/2;x++){
图片->数据[1][y*图片->线宽[1]+x]=128+y+i*2;
图片->数据[2][y*图片->线宽[2]+x]=64+x+i*5;
}
}
/*对图像进行编码*/
out_size=avcodec_encode_视频(c、EXBUF、EXBUF_size、图片);
had|u输出|=输出尺寸;
printf(“编码帧%3d(大小=%5d)\n”,i,out\u大小);
fwrite(f,1,out_size,f);
}
/*获取延迟帧*/
for(;out_size | |!had_输出;i++){
fflush(stdout);
out_size=avcodec_encode_视频(c、EXBUF、EXBUF_size、NULL);
had|u输出|=输出尺寸;
printf(“写入帧%3d(大小=%5d)\n”,i,out\u大小);
fwrite(f,1,out_size,f);
}
/*添加序列结束代码以获得真正的mpeg文件*/
f[0]=0x00;
exputf[1]=0x00;
exputf[2]=0x01;
exputf[3]=0xb7;
fwrite(f,1,4,f);
fclose(f);
fclose(p);
免费(免费);
avcodec_close(c);
无AVU(c);
无AVU(图片->数据[0]);
av_免费(图片);
printf(“\n”);
返回0;
}

您没有编写
.MP4
.flv
AVI
文件。您正在编写未经处理的H.264视频流,但没有任何容器。大多数播放机将无法播放该视频流,但它在ffplay和mplayer上运行良好

它似乎按预期工作。它编码25帧奇怪的颜色渐变。不过,由于低比特率,它可能看起来像垃圾


您只需使用
libavformat
将此流放入某个容器中,然后提供一些真实的视频帧进行编码。

好的,谢谢,所以我需要创建一个容器。但是真实的视频帧?我打开的文件是真实的视频,还是我缺少了什么?您是否知道是否有任何好的示例,特别是cre你没有解码任何视频。视频是动态生成的(请参见
/*准备一个虚拟图像*/
),并写入
“C:\\wav\\test2.flv”
。哦,我明白了,这是一个巨大的失误。谢谢:)只是一个问题,比如我打开了一个.AVI文件。我想把它变成一个.MP4文件。我为它创建了容器,在将其编码为MP4之前是否需要解码.AVI。或者我只是打开.AVI,对它进行编码,然后将帧写入我的容器?你需要解码AVI并将其编码为MP4。您可能需要重新编码视频和音频流,但不是必需的。请参考FFMPEG-C++,将此链接用于MP4容器编码视频。