2012-02-15 19 views
1

我使用AVAssetWriter類從我的應用程序的屏幕重新編碼視頻。 視頻記錄。但重新錄製的視頻是藍色的。 我的代碼來設置AVAssetWriter低於AVAssetWriter提供藍色視頻文件

-(BOOL) setUpWriter { 
    NSError* error = nil; 
    videoWriter = [[AVAssetWriter alloc] initWithURL:[self tempFileURL] fileType:AVFileTypeAppleM4V error:&error]; 
    NSParameterAssert(videoWriter); 

    //Configure video 
    NSDictionary* videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys: 
              [NSNumber numberWithDouble:1024.0*1024.0], AVVideoAverageBitRateKey, 
              nil ]; 

    CGSize size=[CCDirector sharedDirector].winSize; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
            AVVideoCodecH264, AVVideoCodecKey, 
            [NSNumber numberWithInt:size.width], AVVideoWidthKey, 
            [NSNumber numberWithInt:size.height], AVVideoHeightKey, 
            videoCompressionProps, AVVideoCompressionPropertiesKey, 
            nil]; 

    videoWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain]; 

    NSParameterAssert(videoWriterInput); 
    videoWriterInput.expectsMediaDataInRealTime = YES; 
    NSDictionary* bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
             [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil]; 

    avAdaptor = [[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput sourcePixelBufferAttributes:bufferAttributes] retain]; 

    //add input 
    [videoWriter addInput:videoWriterInput]; 
    [videoWriter startWriting]; 
    [videoWriter startSessionAtSourceTime:CMTimeMake(0, 1000)]; 

    return YES; 
} 

有沒有在我的代碼的bug。 我該如何解決這個問題

+0

你好,我想你這個代碼,但我具有我無法理解的問題。我的videoWriter成功完成,但無法在outputURL上找到該文件。請幫幫我。以及avAdaptor做什麼? – 2012-11-17 07:51:25

回答

3

你AVAssetWriterInput期待ARGB,但你可能提供RGBA。

這將看到可能的100%阿爾法解釋爲藍色和紅色組件丟棄,這將解釋您的藍色色調。

很難說沒有看到您的屏幕錄製代碼。

+0

雅我認爲是這樣,我從openglview(cocos2d場景) 這是RGBA。我在哪裏可以將ARGB更改爲RGBA – Johnykutty 2012-02-16 03:41:03

+0

我已更改 'NSDictionary * bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];' 至 'NSDictionary * bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32RGBA],kCVPixelBufferPixelFormatTypeKey,nil];' 它崩潰在 'CFDataGetBytes(image,CFRangeMake(0,CFDataGetLength(image)),destPixels);' – Johnykutty 2012-02-16 04:02:36

+0

並不那麼簡單 - 對於RGB iOS AVFoundation更喜歡kCVPixelFormatType_32BGRA,所以在寫入之前您必須交換像素組件。如果您使用的是OpenGLES2,則可以使用GPU來有效地「調整」組件。更多信息在這裏:http://stackoverflow.com/questions/8771471/converting-rgba-to-argb-glreadpixels-avassetwriter – 2012-02-16 08:19:32

3

可能的問題是不正確的像素格式,嘗試使用kCVPixelFormatType_32BGRA