title: Encoding images to video with ffmpeg
body: <p>I am trying to encode series of images to one video file. I am using code from api-example.c, its works, but it gives me weird green colors in video. I know, I need to convert my RGB images to YUV, I found some solution, but its doesn't works, the colors is not green but very strange, so thats the code:</p>
  
  <pre><code>    // Register all formats and codecs
      av_register_all();
  
      AVCodec *codec;
      AVCodecContext *c= NULL;
      int i, out_size, size, outbuf_size;
      FILE *f;
      AVFrame *picture;
      uint8_t *outbuf;
  
      printf("Video encoding\n");
  
      /* find the mpeg video encoder */
      codec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
      if (!codec) {
          fprintf(stderr, "codec not found\n");
          exit(1);
      }
  
      c= avcodec_alloc_context();
      picture= avcodec_alloc_frame();
  
      /* put sample parameters */
      c-&gt;bit_rate = 400000;
      /* resolution must be a multiple of two */
      c-&gt;width = 352;
      c-&gt;height = 288;
      /* frames per second */
      c-&gt;time_base= (AVRational){1,25};
      c-&gt;gop_size = 10; /* emit one intra frame every ten frames */
      c-&gt;max_b_frames=1;
      c-&gt;pix_fmt = PIX_FMT_YUV420P;
  
      /* open it */
      if (avcodec_open(c, codec) &lt; 0) {
          fprintf(stderr, "could not open codec\n");
          exit(1);
      }
  
      f = fopen(filename, "wb");
      if (!f) {
          fprintf(stderr, "could not open %s\n", filename);
          exit(1);
      }
  
      /* alloc image and output buffer */
      outbuf_size = 100000;
      outbuf = malloc(outbuf_size);
      size = c-&gt;width * c-&gt;height;
  
  #pragma mark -
      AVFrame* outpic = avcodec_alloc_frame();
      int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c-&gt;width, c-&gt;height);
  
      //create buffer for the output image
      uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);
  
  #pragma mark -  
      for(i=1;i&lt;77;i++) {
          fflush(stdout);
  
          int numBytes = avpicture_get_size(PIX_FMT_YUV420P, c-&gt;width, c-&gt;height);
          uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
  
          UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"10%d", i]];
          CGImageRef newCgImage = [image CGImage];
  
          CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
          CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
          buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);   
  
          avpicture_fill((AVPicture*)picture, buffer, PIX_FMT_RGB8, c-&gt;width, c-&gt;height);
          avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c-&gt;width, c-&gt;height);
  
          struct SwsContext* fooContext = sws_getContext(c-&gt;width, c-&gt;height, 
                                                  PIX_FMT_RGB8, 
                                                  c-&gt;width, c-&gt;height, 
                                                  PIX_FMT_YUV420P, 
                                                  SWS_FAST_BILINEAR, NULL, NULL, NULL);
  
          //perform the conversion
          sws_scale(fooContext, picture-&gt;data, picture-&gt;linesize, 0, c-&gt;height, outpic-&gt;data, outpic-&gt;linesize);
          // Here is where I try to convert to YUV
  
          /* encode the image */
          out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
          printf("encoding frame %3d (size=%5d)\n", i, out_size);
          fwrite(outbuf, 1, out_size, f);
  
          free(buffer);
          buffer = NULL;      
  
      }
  
      /* get the delayed frames */
      for(; out_size; i++) {
          fflush(stdout);
  
          out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
          printf("write frame %3d (size=%5d)\n", i, out_size);
          fwrite(outbuf, 1, outbuf_size, f);      
      }
  
      /* add sequence end code to have a real mpeg file */
      outbuf[0] = 0x00;
      outbuf[1] = 0x00;
      outbuf[2] = 0x01;
      outbuf[3] = 0xb7;
      fwrite(outbuf, 1, 4, f);
      fclose(f);
      free(outbuf);
  
      avcodec_close(c);
      av_free(c);
      av_free(picture);
      printf("\n");
  </code></pre>
  
  <p>Please give me advice how to fix that problem.</p>