title: ffmpeg transcoding.c problems about 'dimensions not set'
body: <p>I am trying to solve this problem in <code>transcoding.c</code> example about FFMPEG. but I don't know how to approach it...</p>
  <p>I am getting this error:</p>
  <pre class="lang-none prettyprint-override"><code>[libx264 @ 00000000022c7120] dimensions not set
  Cannot open video encoder for stream #0
  Error occurred: Invalid argument
  </code></pre>
  <p>Here is my code to fix the <code>transcoding.c</code> example a little bit</p>
  <pre><code>static int open_output_file(const char *filename)
  {
      AVStream *out_stream;
      AVStream *in_stream;
      AVCodecContext *dec_ctx, *enc_ctx;
      AVCodec *encoder;
      int ret;
      unsigned int i;
  
      ofmt_ctx = NULL;
  
      avformat_alloc_output_context2(&amp;ofmt_ctx, NULL, NULL, filename);
  
      if (!ofmt_ctx) {
          av_log(NULL, AV_LOG_ERROR, &quot;Could not create output context\n&quot;);
          return AVERROR_UNKNOWN;
      }
  
      for (i = 0; i &lt; ifmt_ctx-&gt;nb_streams; i++) {
          in_stream = ifmt_ctx-&gt;streams[i];
          out_stream = avformat_new_stream(ofmt_ctx, NULL);
  
          if (!out_stream) {
              av_log(NULL, AV_LOG_ERROR, &quot;Failed allocating output stream\n&quot;);
              return AVERROR_UNKNOWN;
          }
  
          dec_ctx = avcodec_alloc_context3(in_stream-&gt;codec-&gt;codec);
          enc_ctx = avcodec_alloc_context3(out_stream-&gt;codec-&gt;codec);
  
          if (dec_ctx-&gt;codec_type == AVMEDIA_TYPE_VIDEO || dec_ctx-&gt;codec_type == AVMEDIA_TYPE_AUDIO) {
              /* in this example, we choose transcoding to same codec */
              encoder = avcodec_find_encoder(dec_ctx-&gt;codec_id);
              
              if (!encoder) {
                  av_log(NULL, AV_LOG_FATAL, &quot;Necessary encoder not found\n&quot;);
                  return AVERROR_INVALIDDATA;
              }
  
              /* In this example, we transcode to same properties (picture size,
              * sample rate etc.). These properties can be changed for output
              * streams easily using filters */
              if (dec_ctx-&gt;codec_type == AVMEDIA_TYPE_VIDEO) {
  
                  enc_ctx-&gt;height = dec_ctx-&gt;height;
                  enc_ctx-&gt;width = dec_ctx-&gt;width;
                  enc_ctx-&gt;sample_aspect_ratio = dec_ctx-&gt;sample_aspect_ratio;
  
                  /* take first format from list of supported formats */
                  enc_ctx-&gt;pix_fmt = AV_PIX_FMT_YUV420P;
  
                  /* video time_base can be set to whatever is handy and supported by encoder */
                  enc_ctx-&gt;time_base = dec_ctx-&gt;time_base;
              }
  
              else {
                  enc_ctx-&gt;sample_rate = dec_ctx-&gt;sample_rate;
                  enc_ctx-&gt;channel_layout = dec_ctx-&gt;channel_layout;
                  enc_ctx-&gt;channels = av_get_channel_layout_nb_channels(enc_ctx-&gt;channel_layout);
  
                  /* take first format from list of supported formats */
                  enc_ctx-&gt;sample_fmt = encoder-&gt;sample_fmts[0];
                  enc_ctx-&gt;time_base.num = 1;
                  enc_ctx-&gt;time_base.den = enc_ctx-&gt;sample_rate;
              }
  
              /* Third parameter can be used to pass settings to encoder */
              ret = avcodec_open2(enc_ctx, encoder, NULL);
          
              if (ret &lt; 0) {
                  cout &lt;&lt; &quot;ret&lt;0&quot; &lt;&lt; endl;
                  av_log(NULL, AV_LOG_ERROR, &quot;Cannot open video encoder for stream #%u\n&quot;, i);
                  return ret;
              }
          }
          else if (dec_ctx-&gt;codec_type == AVMEDIA_TYPE_UNKNOWN) {
              av_log(NULL, AV_LOG_FATAL, &quot;Elementary stream #%d is of unknown type, cannot proceed\n&quot;, i);
              return AVERROR_INVALIDDATA;
          }
          else {
              /* if this stream must be remuxed */
              ret = avcodec_copy_context(ofmt_ctx-&gt;streams[i]-&gt;codec, ifmt_ctx-&gt;streams[i]-&gt;codec);
              if (ret &lt; 0) {
                  av_log(NULL, AV_LOG_ERROR, &quot;Copying stream context failed\n&quot;);
                  return ret;
              }
          }
          if (ofmt_ctx-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
              enc_ctx-&gt;flags |= CODEC_FLAG_GLOBAL_HEADER;
      }
  
      av_dump_format(ofmt_ctx, 0, filename, 1);
  
      if (!(ofmt_ctx-&gt;oformat-&gt;flags &amp; AVFMT_NOFILE)) {
          ret = avio_open(&amp;ofmt_ctx-&gt;pb, filename, AVIO_FLAG_WRITE);
          if (ret &lt; 0) {
              av_log(NULL, AV_LOG_ERROR, &quot;Could not open output file '%s'&quot;, filename);
              return ret;
          }
      }
  
      /* init muxer, write output file header */
      ret = avformat_write_header(ofmt_ctx, NULL);
      if (ret &lt; 0) {
          av_log(NULL, AV_LOG_ERROR, &quot;Error occurred when opening output file\n&quot;);
          return ret;
      }
      return 0;
  }
  </code></pre>
  <p>Why doesn't <code>avcodec_open2</code> work in my code?</p>