AVAssetWriterInput Settings Quality vs. Filesize

2.7k Views Asked by At

Here's a rather specific question that's left me stumped. I'm writing a video recording software that captures video data from a webcam to an MP4. My software is going to replace a script that's already in place that triggers QuickTime Player to do the same but output to a MOV.

I'm using AVFoundation and have the capturing and saving in place but, after repeated tweaks and tests, I've found that the script that's already in place consistently creates MOV files with a higher video quality and lower file sizes than my software.

Here is a link to two samples, a MOV created by the on-site script and an MP4 created by my software: https://www.dropbox.com/sh/1qnn5afmrquwfcr/AADQvDMWkbYJwVNlio9_vbeNa?dl=0

The MOV was created by a colleague and is the quality and file size I'm trying to match with my software. The MP4 was recording in my office, obviously a different lighting situation, but with the same camera as is used on-site.

Comparing the two videos, I can see that they have the same dimensions, duration, and video codec but differ in both file size and quality.

Here is the code where I set up my AVAssetWriter and AVAssetWriter input:

NSDictionary *settings = nil;

settings = [NSDictionary dictionaryWithObjectsAndKeys:
            // Specify H264 (MPEG4) as the codec
            AVVideoCodecH264, AVVideoCodecKey,

            // Specify the video width and height
            [NSNumber numberWithInt:self.recordSize.width], AVVideoWidthKey,
            [NSNumber numberWithInt:self.recordSize.height], AVVideoHeightKey,

            // Specify the video compression
            [NSDictionary dictionaryWithObjectsAndKeys:
             [NSNumber numberWithInteger:2500000], AVVideoAverageBitRateKey,
             [NSNumber numberWithInt:1], AVVideoMaxKeyFrameIntervalKey,
             //AVVideoProfileLevelH264Baseline30, AVVideoProfileLevelKey, Not available on 10.7
              nil], AVVideoCompressionPropertiesKey,

            // Specify the HD output color
            [NSDictionary dictionaryWithObjectsAndKeys:
             AVVideoColorPrimaries_ITU_R_709_2, AVVideoColorPrimariesKey,
             AVVideoTransferFunction_ITU_R_709_2, AVVideoTransferFunctionKey,
             AVVideoYCbCrMatrix_ITU_R_709_2, AVVideoYCbCrMatrixKey, nil], AVVideoColorPropertiesKey,
            nil];

self.assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];// sourceFormatHint:self.formatHint];

/*self.pixelBufferAdaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc]
 initWithAssetWriterInput:self.assetWriterInput
 sourcePixelBufferAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
 [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange],//kCVPixelFormatType_32BGRA],
 kCVPixelBufferPixelFormatTypeKey,
 nil]];*/
NSError *error;

self.videoFile = [NSURL fileURLWithPath:file];

//[self.movieFileOutput startRecordingToOutputFileURL:self.videoFile recordingDelegate:self];

self.assetWriter = [[AVAssetWriter alloc]
                    initWithURL:self.videoFile
                    fileType:AVFileTypeMPEG4//AVFileTypeQuickTimeMovie//
                    error:&error];

if (self.assetWriter){
  [self.assetWriter addInput:self.assetWriterInput];

  self.assetWriterInput.expectsMediaDataInRealTime = YES;

And the code where I set up my AVCaptureVideoDataOutput and add it to my capture session:

AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

output.alwaysDiscardsLateVideoFrames = NO;

  output.videoSettings = nil;

  self.videoQueue = dispatch_queue_create("ca.blackboxsoftware.avcapturequeue", NULL);

  [output setSampleBufferDelegate:self queue:self.videoQueue];

  [self.session addOutput:output];

This quality issue is the big stumbling block of my software and I desperately need your help with it. I'll be happy to post any other code you might need to see and test out changes you feel would make the difference.

Thank you for your time.

0

There are 0 best solutions below