<div dir="ltr"><div>I am encountering an error when trying to open the codec with avcodec_open2(). I have tried the same code without any problems if I specify "avi" instead of "h264" in the av_guess_format() function. </div><div><br></div><div>I don't know what to make of it. Has anyone else encountered a similar problem? </div><div><br></div><div>The library that I'm using is ffmpeg-20160219-git-98a0053-win32-dev. I would really really appreciate if you could help me out of this confusion.</div><div><br></div><div>This is my console output:</div><div>    </div><div>    Video encoding</div><div>    [libx264 @ 01383460] broken ffmpeg default settings detected</div><div>    [libx264 @ 01383460] use an encoding preset (e.g. -vpre medium)</div><div>    [libx264 @ 01383460] preset usage: -vpre <speed> -vpre <profile></div><div>    [libx264 @ 01383460] speed presets are listed in x264 --help</div><div>    [libx264 @ 01383460] profile is optional; x264 defaults to high</div><div>    Cannot open video codec, -542398533</div><div>    Err: Generic error in an external library</div><div><br></div><div>This is the code that I'm working with:</div><div><br></div><div><div>AVCodec *codec = NULL;<br></div><div>AVCodecContext *codecCtx= NULL;</div><div>AVFormatContext *pFormatCtx = NULL;</div><div>AVOutputFormat *pOutFormat = NULL;</div><div>AVStream * pVideoStream = NULL;;</div><div>AVFrame *picture = NULL;;</div><div><br></div><div>int i, x, y, ret;</div><div><br></div><div>printf("Video encoding\n");</div><div><br></div><div>// Register all formats and codecs</div><div>av_register_all();</div><div><br></div><div>// guess format from file extension</div><div>pOutFormat = av_guess_format("h264", NULL, NULL);</div><div>if (NULL==pOutFormat){</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>cerr << "Could not guess output format" << endl;</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>return -1;</div><div>}<span class="Apple-tab-span" style="white-space:pre">  </span></div><div><br></div><div>// allocate context</div><div>pFormatCtx = avformat_alloc_context();</div><div>pFormatCtx->oformat = pOutFormat;</div><div>memcpy(pFormatCtx->filename,filename,</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>min(strlen(filename), sizeof(pFormatCtx->filename)));</div><div><span class="Apple-tab-span" style="white-space:pre">     </span></div><div>// Add stream to pFormatCtx</div><div>pVideoStream = avformat_new_stream(pFormatCtx, 0);</div><div>if (!pVideoStream) </div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>printf("Cannot add new video stream\n");</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>return -1;</div><div>}</div><div><br></div><div>// Set stream's codec context</div><div>codecCtx = pVideoStream->codec;</div><div>codecCtx->codec_id = (AVCodecID)pOutFormat->video_codec;</div><div>codecCtx->codec_type = AVMEDIA_TYPE_VIDEO;</div><div>codecCtx->frame_number = 0;</div><div>// Put sample parameters.</div><div>codecCtx->bit_rate = 2000000;</div><div>// Resolution must be a multiple of two.</div><div>codecCtx->width  = 320;</div><div>codecCtx->height = 240;</div><div>codecCtx->time_base.den = 10;</div><div>codecCtx->time_base.num = 1;</div><div>pVideoStream->time_base.den = 10;</div><div>pVideoStream->time_base.num = 1;</div><div>codecCtx->gop_size = 12; // emit one intra frame every twelve frames at most</div><div>codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;</div><div><br></div><div>if (codecCtx->codec_id == AV_CODEC_ID_H264)</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>// Just for testing, we also add B frames </div><div><span class="Apple-tab-span" style="white-space:pre">  </span>codecCtx->mb_decision = 2;</div><div>}</div><div>// Some formats want stream headers to be separate.</div><div>if(pFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)</div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>codecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;</div><div>}</div><div><br></div><div>if(codecCtx->codec_id == AV_CODEC_ID_H264)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>av_opt_set(codecCtx->priv_data, "preset", "slow", 0);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div>// Open the codec.</div><div>codec = avcodec_find_encoder(codecCtx->codec_id);</div><div>if (codec == NULL) {</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>fprintf(stderr, "Codec not found\n");</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>return -1;</div><div>}</div><div>ret = avcodec_open2(codecCtx, codec, NULL); // returns -542398533 here</div><div>if (ret < 0) </div><div>{</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>printf("Cannot open video codec, %d\n",ret);</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>char tst[100];</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>av_strerror(ret, tst, 100);</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>av_log(0, 0, "Err: %s\n", tst);</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>return -1;</div><div>}</div></div><div><br></div><div><br></div></div>