Video FFMpeg将h264多路复用到mp4结果文件未运行

Video FFMpeg将h264多路复用到mp4结果文件未运行,video,ffmpeg,h.264,mux,muxer,Video,Ffmpeg,H.264,Mux,Muxer,我正在使用最新的FFMPEG 3.0将mux h264文件的代码写入mp4。Muxing正在工作,但生成的mp4文件没有显示视频,只有在播放生成的mp4文件时时间在运行。请尝试帮助解决这个问题。请告诉我我错过了什么。这是我的密码: int main() { avcodec_register_all(); av_register_all(); // define AVOutputFormat AVOutputFormat *avoutputFormat =

我正在使用最新的FFMPEG 3.0将mux h264文件的代码写入mp4。Muxing正在工作,但生成的mp4文件没有显示视频,只有在播放生成的mp4文件时时间在运行。请尝试帮助解决这个问题。请告诉我我错过了什么。这是我的密码:

int main()
{   
    avcodec_register_all();

    av_register_all();

    // define AVOutputFormat
    AVOutputFormat *avoutputFormat = NULL;
    avoutputFormat = av_guess_format("mp4", NULL, NULL);
    if (!avoutputFormat) {
    fprintf(stderr, "Could not find suitable output format\n");
    return 1;
    }

    AVFormatContext *avoutFmtCtx = NULL;
    /*Below initialize AVFormatContext.*/
    avformat_alloc_output_context2(&avoutFmtCtx, avoutputFormat, NULL, NULL);

    avoutFmtCtx->oformat = avoutputFormat;
    sprintf_s(avoutFmtCtx->filename, "%s", "Out.mp4");  /*Filling output file name..*/

    if (avoutputFormat->video_codec == AV_CODEC_ID_NONE) 
    printf("\n Unsupported format...");

    AVStream * avoutStrm = avformat_new_stream(avoutFmtCtx, 0);

    if (!avoutStrm) {
    _tcprintf(_T("FFMPEG: Could not alloc video stream\n"));        
    }

    AVCodecContext *c = avoutStrm->codec;
    c->codec_id     = avoutputFormat->video_codec;
    c->codec_type   = AVMEDIA_TYPE_VIDEO;
    c->bit_rate     = 2000*1000;
    c->width        = 1920; 
    c->height       = 1080; 
    AVRational avr;
    avr.den = 30;
    avr.num = 1;

    avoutStrm->time_base = c->time_base = av_add_q(avoutStrm->codec->time_base, avr);

    // Some formats want stream headers to be separate
    if(avoutFmtCtx->oformat->flags & AVFMT_GLOBALHEADER)
    c->flags |= CODEC_FLAG_GLOBAL_HEADER;

    // Open the output container file
    if (avio_open(&avoutFmtCtx->pb, avoutFmtCtx->filename, AVIO_FLAG_WRITE) < 0)
    {
    _tcprintf(_T("FFMPEG: Could not open '%s'\n"), avoutFmtCtx->filename);      
    }

    m_pExtDataBuffer = (uint8_t*)av_malloc(1000 + 1000);
    if(!m_pExtDataBuffer) {
    _tcprintf(_T("FFMPEG: could not allocate required buffer\n"));

    }

    uint8_t SPSbuf[1000];
    uint8_t PPSbuf[1000];
    memcpy(m_pExtDataBuffer, SPSbuf, 1000);             
    memcpy(m_pExtDataBuffer + 1000, PPSbuf, 1000);  

    /* Codec "extradata" conveys the H.264 stream SPS and PPS info (MPEG2: sequence header is housed in SPS buffer, PPS buffer is empty)*/
    c->extradata        = m_pExtDataBuffer;
    c->extradata_size   = 1000 + 1000;

    if(avformat_write_header(avoutFmtCtx,NULL)) {
    _tcprintf(_T("FFMPEG: avformat_write_header error!\n"));

    }

    /* Here do writing data in loop...*/
    int m_nProcessedFramesNum = 0;

    while(1)
    {
    ++m_nProcessedFramesNum;
    AVPacket pkt;
    av_init_packet(&pkt);

    AVCodecContext *c = avoutStrm->codec;
    avoutStrm->pts.val = m_nProcessedFramesNum;
    pkt.stream_index    = avoutStrm->index;

    pkt.data            = /*Filling h.264 data from here... This is valid h264 data*/
    pkt.size            = /*Filling valid h264 data size here...*/
    av_new_packet(&pkt,pkt.size);
    pkt.pts = m_nProcessedFramesNum*512;
    pkt.dts = m_nProcessedFramesNum*512;
    pkt.duration = 512;

    // Write the compressed frame in the media file
    if (av_interleaved_write_frame(avoutFmtCtx, &pkt)) {
    _tcprintf(_T("FFMPEG: Error while writing video frame\n"));         
    }/*End of loop.*/

    av_free_packet(&pkt);       

    }   
    av_write_trailer(avoutFmtCtx);
    avio_close(avoutFmtCtx->pb);
    avformat_free_context(avoutFmtCtx);
}
intmain()
{   
avcodec_寄存器_all();
av_寄存器_all();
//定义AVOutputFormat
AVOutputFormat*AVOutputFormat=NULL;
avoutputFormat=av_guess_格式(“mp4”,NULL,NULL);
如果(!avoutputFormat){
fprintf(stderr,“找不到合适的输出格式\n”);
返回1;
}
AVFormatContext*avoutFmtCtx=NULL;
/*下面是初始化AVFormatContext*/
avformat\u alloc\u output\u context2(&avoutFmtCtx,avoutputFormat,NULL,NULL);
avoutFmtCtx->oformat=avoutputFormat;
sprintf_s(avoutFmtCtx->filename,“%s”,“Out.mp4”);/*填充输出文件名*/
if(avoutputFormat->video\u codec==AV\u codec\u ID\u NONE)
printf(“\n不支持的格式…”);
AVStream*avoutStrm=avformat\U new\U流(AVOUTFMTCTTX,0);
如果(!avootstrm){
_tcprintf(_T(“FFMPEG:cannotalloc video stream\n”);
}
AVCodecContext*c=avoutStrm->codec;
c->codec\u id=avoutputFormat->video\u codec;
c->codec\u type=AVMEDIA\u type\u VIDEO;
c->比特率=2000*1000;
c->宽度=1920;
c->高度=1080;
avr;
avr.den=30;
avr.num=1;
avoutStrm->time\U base=c->time\U base=av\U add\U q(avoutStrm->codec->time\U base,avr);
//某些格式希望流标题是独立的
if(avoutFmtCtx->oformat->flags和AVFMT_GLOBALHEADER)
c->flags |=编解码器|标志|全局|头;
//打开输出容器文件
如果(avio_打开(&avoutFmtCtx->pb,avoutFmtCtx->文件名,avio_标志_写入)<0)
{
_tcprintf(_T(“FFMPEG:无法打开“%s”\n”),avootfmtctx->filename);
}
m_pExtDataBuffer=(uint8_t*)av_malloc(1000+1000);
如果(!m_pExtDataBuffer){
_tcprintf(_T(“FFMPEG:无法分配所需的缓冲区”);
}
uint8_t SPSbuf[1000];
uint8_t PPSbuf[1000];
memcpy(m_pExtDataBuffer,SPSbuf,1000);
memcpy(m_pExtDataBuffer+1000,PPSbuf,1000);
/*编解码器“extradata”传送H.264流SPS和PPS信息(MPEG2:序列头位于SPS缓冲区中,PPS缓冲区为空)*/
c->extradata=m_pExtDataBuffer;
c->extradata_size=1000+1000;
if(avformat\ U write\ U头(avoutFmtCtx,NULL)){
_tcprintf(_T(“FFMPEG:avformat_write_header error!\n”);
}
/*这里做的是在循环中写入数据*/
int m_n过程框架m=0;
而(1)
{
++m_n过程框架;
AVPacket-pkt;
av_初始_数据包(&pkt);
AVCodecContext*c=avoutStrm->codec;
avootstrm->pts.val=m_n过程框架;
pkt.stream_index=avootstrm->index;
pkt.data=/*从这里填充h.264数据…这是有效的h264数据*/
pkt.size=/*在此处填充有效的h264数据大小*/
av_新_数据包(&pkt,pkt.size);
pkt.pts=m_n过程框架*512;
pkt.dts=m_n过程框架m*512;
pkt.duration=512;
//将压缩帧写入媒体文件
中频(av交织写入帧(avoutFmtCtx和pkt)){
_tcprintf(_T(“FFMPEG:写入视频帧时出错”);
}/*循环结束*/
av_免费_数据包(&pkt);
}   
av_写入_预告片(avoutFmtCtx);
avio_关闭(avoutFmtCtx->pb);
avformat_free_上下文(avoutFmtCtx);
}