(FFMPEG) avformat_write_header crashes (MSVC2013) (C++) (Qt) -


i downloaded ffmpeg , i'm trying use in qt msvc2013 compiler.

to understand how works, started reading documentation , api. according figure, trying make little test libavformat.

i did said in demuxing module, muxing module. but, program crashes when call avformat_write_header() function.

i know did wrong , if me understand that.

in main:

av_register_all();  if(!decode())     return; 

the decode() methode:

bool mainwindow::decode() { avformatcontext *formatcontext = null; avpacket packet;  /**************** muxing varaiables ******************/  avformatcontext *muxingcontext = avformat_alloc_context(); avoutputformat *outputformat = null; aviocontext *contextio = null; avcodec *codecencode = avcodec_find_encoder(av_codec_id_wmav2); avstream *avstream =  null; avcodeccontext *codeccontext = null;   /******************* demuxing **************************/  //open media file if(avformat_open_input(&formatcontext,"h.mp3",null,null)!=0) {     qdebug() << "paka ouve fichier";     return false; }  //function tries read , decode few frames find missing           information. if(avformat_find_stream_info(formatcontext,null)<0) {     qdebug()<<"paka find stream";     return false; }   /**************** muxing *************************/  //the oformat field must set select muxer used. muxingcontext->oformat = outputformat;  //unless format of avfmt_nofile type, pb field must set //an opened io context, either returned avio_open2() or custom one. if(avio_open2(&contextio,"out.wma",avio_flag_write,null,null)<0) {     qdebug() <<"paka kreye fichier soti";     return false; } muxingcontext->pb = contextio;  //unless format of avfmt_nostreams type, @ least //one stream must created avformat_new_stream() function. avstream = avformat_new_stream(muxingcontext,codecencode);  //the caller should fill stream codec context information, //such codec type, id , other parameters //(e.g. width / height, pixel or sample format, etc.) known  codeccontext = avstream->codec; codeccontext->codec_type = avmedia_type_audio; codeccontext->codec_id = av_codec_id_wmav2; codeccontext->sample_fmt = codecencode->sample_fmts[0]; codeccontext->bit_rate = 128000; codeccontext->sample_rate = 44000; codeccontext->channels = 2;  //the stream timebase should set timebase caller desires //to use stream (note timebase used muxer //can different, described later).  avstream->time_base = formatcontext->streams[0]->time_base; qdebug()<<formatcontext->streams[0]->time_base.num <<"/"  <<formatcontext-    >streams[0]->time_base.den;   //when muxing context set up, caller must call      //avformat_write_header() //to initialize muxer internals , write file header  qdebug() << "does not crash yet"; if(avformat_write_header(muxingcontext,null) <0) {     qdebug()<<"cannot write header";     return false; } qdebug() << "oops can't see me (john cena)";  ///////////////////// reading opened file ////////////////////////// while(av_read_frame(formatcontext,&packet)==0) {     //the data sent muxer repeatedly calling     //av_write_frame() or av_interleaved_write_frame()     if(av_write_frame(muxingcontext,&packet)<0)         qdebug()<<"paka write frame";     else         qdebug()<<"writing"; }  //once data has been written, caller must call //av_write_trailer() flush buffered packets , finalize //the output file, close io context (if any) , //free muxing context avformat_free_context().  if(av_write_trailer(muxingcontext)!=0) {     qdebug()<<"paka ekri trailer";     return false; }   return true; } 

the program shows message does not crash yet. not oops can't see me (john cena)

and there no error. used mp3 file input , ouput in wma.

instead of avformat_alloc_context(), use avformat_alloc_output_context2(). set muxingcontext->oformat.