2011-02-19 49 views
2

我試圖創建一個簡單的視頻應用程序(從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; 

然後,我只是讀取下一個樣本緩衝區,修改像素數據,並用不同的名稱保存它。這已經工作,但輸出質量是如此糟糕......

請大家給我一些建議:-)。

+0

非常好的努力。\ –

回答

1

嘗試設置:

imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

選擇電影之前。

1

它看起來像你使用UIImagePickerController。這會壓縮我相信的視頻,導致您獲得的質量很差。

另外,您可能會考慮將expectMediaDataInRealTime設置爲YES,以優化幀的丟棄。

+0

非常感謝!你是對的。我確定視頻質量不好與我使用AVAsset的方式有關,而與UIImagePickerController本身沒有關係......這是一個指向文檔的鏈接:http://developer.apple.com/library/ios/#文檔/ uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html – peter

+0

很高興幫助。如果它適合你,請接受它作爲答案。 – akaru

相關問題