title: How to create video from its frames iPhone
body: <p>I had done R&amp;D and got success in how to get frames in terms of images from video file played in <code>MPMoviePlayerController</code>.</p>
  
  <p>Got all frames from this code, and save all images in one Array.</p>
  
  <pre><code>for(int i= 1; i &lt;= moviePlayerController.duration; i++)
  {
      UIImage *img = [moviePlayerController thumbnailImageAtTime:i timeOption:MPMovieTimeOptionNearestKeyFrame];
      [arrImages addObject:img];
  }
  </code></pre>
  
  <p>Now the question is that, After change some image file, like adding emotions to the images and also adding filters, such as; movie real, black and white, How can we create video again and store the same video in Document directory with the same frame rate and without losing quality of video.</p>
  
  <p>After changing some images I had done following code to save that video again.</p>
  
  <pre><code>- (void) writeImagesAsMovie:(NSString*)path 
  {
      NSError *error  = nil;
      UIImage *first = [arrImages objectAtIndex:0];
      CGSize frameSize = first.size;
      AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                    [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                                error:&amp;error];
      NSParameterAssert(videoWriter);
  
      NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                     AVVideoCodecH264, AVVideoCodecKey,
                                     [NSNumber numberWithInt:640], AVVideoWidthKey,
                                     [NSNumber numberWithInt:480], AVVideoHeightKey,
                                     nil];
      AVAssetWriterInput* writerInput = [[AVAssetWriterInput
                                          assetWriterInputWithMediaType:AVMediaTypeVideo
                                          outputSettings:videoSettings] retain];
  
      AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                       assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                       sourcePixelBufferAttributes:nil];
  
      NSParameterAssert(writerInput);
      NSParameterAssert([videoWriter canAddInput:writerInput]);
      [videoWriter addInput:writerInput];
  
      [videoWriter startWriting];
      [videoWriter startSessionAtSourceTime:kCMTimeZero];
  
      int frameCount = 0;
      CVPixelBufferRef buffer = NULL;
      for(UIImage *img in arrImages)
      {
          buffer = [self newPixelBufferFromCGImage:[img CGImage] andFrameSize:frameSize]; 
  
              if (adaptor.assetWriterInput.readyForMoreMediaData) 
              {
                  CMTime frameTime = CMTimeMake(frameCount,(int32_t) kRecordingFPS);
                  [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
  
                  if(buffer)
                      CVBufferRelease(buffer);
              }
          frameCount++;
      } 
  
       [writerInput markAsFinished];
       [videoWriter finishWriting];
  }
  
  
  - (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image andFrameSize:(CGSize)frameSize
  {
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                               [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                               nil];
      CVPixelBufferRef pxbuffer = NULL;
      CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, frameSize.width,
                                            frameSize.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, 
                                            &amp;pxbuffer);
      NSParameterAssert(status == kCVReturnSuccess &amp;&amp; pxbuffer != NULL);
  
      CVPixelBufferLockBaseAddress(pxbuffer, 0);
      void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
      NSParameterAssert(pxdata != NULL);
  
      CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(pxdata, frameSize.width,
                                                   frameSize.height, 8, 4*frameSize.width, rgbColorSpace, 
                                                   kCGImageAlphaNoneSkipFirst);
      NSParameterAssert(context);
      CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
      CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                             CGImageGetHeight(image)), image);
      CGColorSpaceRelease(rgbColorSpace);
      CGContextRelease(context);
  
      CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
  
      return pxbuffer;
  }
  </code></pre>
  
  <p>I am new in this topic so please help me solve this question.</p>