title: iOS 4: video quality with AVAsset based application
body: <p>I'm trying to create a simple video application(loading existing video file from ios 4 device, edit it using direct pixel access and save under a different name). </p>
  
  <p>I managed to load, edit and save my movie file on real device(ipod 4g). The only problem that I have is related to movie quality(original vs edited one). I don't know what am I doing wrong, but quality of my output file is very bad comparing to the input one.</p>
  
  <p>Below you can find how am I loading my movie:</p>
  
  <pre><code>// // *** tmp file ***
  NSURL *movieUrl = [info objectForKey:@"UIImagePickerControllerMediaURL"];
  NSLog(@"picker controller movie url: %@", [movieUrl absoluteString]);
  ## picker controller movie url: /private/var/mobile/Applications/6253D1-8C0-41-B780-638250817/tmp//trim.2q03uz.MOV
  AVURLAsset *movieAsset = [[AVURLAsset alloc] initWithURL:movieUrl options:nil]; 
  CGSize size = [movieAsset naturalSize];  
  NSLog(@"movie asset natual size: size.width = %f size.height = %f", size.width, size.height);
  ## movie asset natual size: size.width = 224.000000 size.height = 128.000000
  
  // *** ASSET READER ***
  AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:movieAsset error:&amp;error]; 
  NSArray* videoTracks = [movieAsset tracksWithMediaType:AVMediaTypeVideo]; 
  // asset track
  AVAssetTrack* videoTrack = [videoTracks objectAtIndex:0];   
  NSDictionary* dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] 
                              forKey:(id)kCVPixelBufferPixelFormatTypeKey];
  NSLog(@"video track %f %f %d", [videoTrack naturalSize].width, [videoTrack naturalSize].height, [videoTrack nominalFrameRate]);
  ## video track 224.000000 128.000000 0
  /*
  also tried
   kCVPixelFormatType_32BGRA
   kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
  */
  // asset reader track output
  AVAssetReaderTrackOutput* assetReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack 
                                                  outputSettings:dictionary];
  if(![assetReader canAddOutput:assetReaderOutput])
      NSLog(@"unable to add reader output");
  else 
      [assetReader addOutput:assetReaderOutput];
  
  // *** ASSET WRITER ***
  AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:exportURL
                                  fileType:AVFileTypeQuickTimeMovie error:&amp;error];
  NSParameterAssert(videoWriter);
  NSLog(@"asset writer %d %d", [videoWriter status], [error code]);
  
  NSDictionary *videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithDouble:128.0*1024.0], AVVideoAverageBitRateKey,
                                        nil];
  
  NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                 AVVideoCodecH264, AVVideoCodecKey,
                                 [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                 [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                                 //videoCompressionProps, AVVideoCompressionPropertiesKey,&lt;- no difference at all
                                 nil];
  
  AVAssetWriterInput* writerInput = [[AVAssetWriterInput
                                      assetWriterInputWithMediaType:AVMediaTypeVideo
                                      outputSettings:videoSettings] retain];
  
  // set preffered transform for output video
  writerInput.transform = [videoTrack preferredTransform];
  
  NSParameterAssert(writerInput);
  NSParameterAssert([videoWriter canAddInput:writerInput]);
  [videoWriter addInput:writerInput];
  
  writerInput.expectsMediaDataInRealTime = NO;
  </code></pre>
  
  <p>Then I simply read next sample buffer, modify pixel data and save it with a different name. That already works, but output quality is so bad...</p>
  
  <p>Please guys give me some advice:-).</p>