peter
peter

Reputation: 108

iOS 4: video quality with AVAsset based application

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).

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.

Below you can find how am I loading my movie:

// // *** 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:&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:&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,<- 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;

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...

Please guys give me some advice:-).

Upvotes: 2

Views: 5339

Answers (2)

Fred Lindberg
Fred Lindberg

Reputation: 36

Try setting:

imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

before picking the movie.

Upvotes: 2

akaru
akaru

Reputation: 6317

It looks like you are using the UIImagePickerController. That will compress your video I believe, resulting in the bad quality you're getting.

Also, you may consider setting expectsMediaDataInRealTime to YES, to optimize the dropping of frames.

Upvotes: 1

Related Questions