我試圖創建一個簡單的視頻應用程序(從ios 4設備加載現有視頻文件,使用直接像素訪問進行編輯並以不同的名稱進行保存)。iOS 4:基於AVAsset的應用程序的視頻質量
我設法加載,編輯和保存我的電影文件在真實設備(ipod 4g)。我唯一的問題是與電影質量(原始與編輯之一)有關。我不知道我在做什麼錯,但輸出文件的質量與輸入文件相比非常糟糕。
下面你可以找到我怎麼裝我的電影:
// // *** 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;
然後,我只是讀取下一個樣本緩衝區,修改像素數據,並用不同的名稱保存它。這已經工作,但輸出質量是如此糟糕......
請大家給我一些建議:-)。
非常好的努力。\ –