1

I am recoding a video from my app's screen using the AVAssetWriter class. Video records. but the recoded video is bluish one. my code to set up the AVAssetWriter is below

-(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;
}

Is there any bugs in my code. How can I solve this problem

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • Hi, I tried this code of yours but I am having a problem that I can't understand. My videoWriter finishes successfully but can't find the file on the outputURL. Please help me. and what does avAdaptor do? – iOS Monster Nov 17 '12 at 07:51

2 Answers2

3

probably the problem is in incorrect pixel format, try to use kCVPixelFormatType_32BGRA

BorisV
  • 683
  • 3
  • 20
3

Your AVAssetWriterInput is expecting ARGB but you're probably supplying it with RGBA.

That would see the probable 100% alpha interpreted as blue and the red component discarded, which would explain your bluish tint.

Hard to tell without seeing your screen recording code.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Ya I think so, I'm suplying image from an openglview(of cocos2d scene) It's RGBA. Where I can change ARGB to RGBA – Johnykutty Feb 16 '12 at 03:41
  • I've changed 'NSDictionary* bufferAttributes=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey,nil];' to 'NSDictionary* bufferAttributes=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32RGBA], kCVPixelBufferPixelFormatTypeKey,nil];' It crashes at 'CFDataGetBytes(image,CFRangeMake(0,CFDataGetLength(image)), destPixels);' – Johnykutty Feb 16 '12 at 04:02
  • It's not that simple - for RGB iOS AVFoundation prefers kCVPixelFormatType_32BGRA, so you're going to have to swap the pixel components before writing. If you're using OpenGLES2 you can use the GPU to 'swizzle' the components efficiently. More info here: http://stackoverflow.com/questions/8771471/converting-rgba-to-argb-glreadpixels-avassetwriter – Rhythmic Fistman Feb 16 '12 at 08:19
  • 1
    got solution, just converted ARGB image to RGBA image. I got the idea from purple girl http://purplelilgirl.tumblr.com/post/10974345146/mini-tutorial-how-to-capture-video-of-iphone-app-in – Johnykutty Feb 17 '12 at 04:07