title: Why decoding frames from avi container and encode them to h264/mp4 doesn't work?
body: <p>I started using ffmpeg and I want to convert avi file to mp4/h264 file. I've read many posts including <a href="https://stackoverflow.com/questions/5964142/raw-h264-frames-in-mpegts-container-using-libavcodec">this</a>, but I couldn't find any good example how to save frames to mp4 file. The code below is simplified one that decodes frames from avi file and encode it to H264/mp4 file, but when I save the frames the mp4 file cannot be played. I think I do somethinkg wrong in encoding</p>
  
  <p>I will appreciate if you could tell me what is wrong and how to fix it.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>const char* aviFileName = "aviFrom.avi";
  const char* mp4FileName = "mp4To.mp4";
  
  // Filling pFormatCtx by open video file and Retrieve stream information
  // ...
  // Retrieving codecCtxDecode and opening codecDecode 
  //...
  
  
  // Get encoder
  codecCtxEncode = avcodec_alloc_context();   
  codecCtxEncode-&gt;qmax = 69; 
  codecCtxEncode-&gt;max_qdiff = 4;
  codecCtxEncode-&gt;bit_rate = 400000;
  codecCtxEncode-&gt;width = codecCtxDecode-&gt;width;
  codecCtxEncode-&gt;height = codecCtxDecode-&gt;height;
  codecCtxEncode-&gt;pix_fmt = AV_PIX_FMT_YUV420P;   
  codecEncode = avcodec_find_encoder(CODEC_ID_H264);
  if(codecEncode == NULL)
      return -1;  
  if(avcodec_open2(codecCtxEncode, codecEncode, NULL))
      return -1;
  
  SwsContext *sws_ctx = sws_getContext(codecCtxDecode-&gt;width, codecCtxDecode-&gt;height, codecCtxDecode-&gt;pix_fmt,
                              codecCtxDecode-&gt;width, codecCtxDecode-&gt;height, AV_PIX_FMT_YUV420P,
                              SWS_BILINEAR, NULL, NULL, NULL);
  
  // Allocate an AVFrame structure    
  frameDecoded = avcodec_alloc_frame();
  frameEncoded = avcodec_alloc_frame();    
  
  avpicture_alloc((AVPicture *)frameEncoded, AV_PIX_FMT_YUV420P, codecCtxDecode-&gt;width, codecCtxDecode-&gt;height);
  
  while(av_read_frame(pFormatCtx, &amp;packet)&gt;=0)
  {       
      // Is this a packet from the video stream?
      if(packet.stream_index==videoStreamIndex) {
          avcodec_decode_video2(codecCtxDecode, frameDecoded, &amp;frameFinished, &amp;packet);
          // Did we get a video frame?
          if(frameFinished)
          {           
              fwrite(packet.data, packet.size,
              sws_scale(sws_ctx, frameDecoded-&gt;data, frameDecoded-&gt;linesize, 0, codecCtxDecode-&gt;height, 
                          frameEncoded-&gt;data, frameEncoded-&gt;linesize);
  
  
  
              int64_t pts = packet.pts;
              av_free_packet(&amp;packet);
              av_init_packet(&amp;packet);
              packet.data = NULL;
              packet.size = 0;    
              frameEncoded-&gt;pts = pts;                
  
              int failed = avcodec_encode_video2(codecCtxEncode, &amp;packet, frameEncoded, &amp;got_output);
              if(failed)
              {
                  exit(1);
              }
              fwrite(packet.data,1,packet.size, mp4File);
          }
      }
  
      av_free_packet(&amp;packet);
  }
  </code></pre>