AVAssetWriterInput

时间:2010-11-12 08:50:56

标签: iphone cocoa-touch avfoundation

我正在尝试拍摄相机并使用avassetwriter将数据写入磁盘。从代表处看,avassetwriterinputpixelbufferadator似乎无法附加数据。我不知道为什么

- (NSURL*) assetURL{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
 NSString *filePath = [basePath stringByAppendingPathComponent:@"videoOutput"];

 return [NSURL fileURLWithPath:filePath];
}



- (id) init {
 if(![super init]) return nil;

 self.captureSession = [[c alloc] init];
 self.captureSession.sessionPreset = AVCaptureSessionPresetHigh; 

 // HIGH: 640 x 480
 // MEDIUM: 360 x 480
 // LOW: 192 x 144

 [self loadVideoInput];
 [self loadVideoOutput];
 [self loadPreviewLayer];
 [self loadWriter];



 return self;
}
- (void) loadVideoInput{


 AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if (device.position == AVCaptureDevicePositionFront) {
            videoDevice = device;
   break;
        }
    }



 if ( videoDevice ) {
  NSError *error;
  AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
  if ( !error ) {
   if ([self.captureSession canAddInput:videoIn]) 
    [self.captureSession addInput:videoIn];
   else NSLog(@"Couldn't add video input");  
  } else NSLog(@"Couldn't create video input");
 } else NSLog(@"Couldn't create video capture device");
}
- (void) loadVideoOutput{

 AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
 output.alwaysDiscardsLateVideoFrames = YES;

 [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

 //dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
 //[output setSampleBufferDelegate:self queue:queue];
 //dispatch_release(queue);


 //output.minFrameDuration = CMTimeMake(15, 1); // If you wish to cap the frame rate to a known value, such as 15 fps, set 
 [output setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; // BGRA is necessary for manual preview



 if ([self.captureSession canAddOutput:videoOut]) 
  [self.captureSession addOutput:videoOut]; 
 else 
  NSLog(@"Couldn't add video output");
 //[self.captureSession addOutput:output];
 [output release];

}
- (void) loadPreviewLayer{
 previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
 previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
}

- (void) loadWriter{



 NSError *error = nil;
 videoWriter = [[AVAssetWriter alloc] initWithURL:[self assetURL] fileType:AVFileTypeQuickTimeMovie error:&error];
 NSParameterAssert(videoWriter);

 NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
           AVVideoCodecH264, AVVideoCodecKey,
           [NSNumber numberWithInt:640], AVVideoWidthKey,
           [NSNumber numberWithInt:480], AVVideoHeightKey,
           nil];
 writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];
 writerInput.expectsMediaDataInRealTime = YES;
 NSParameterAssert(writerInput);
 NSParameterAssert([videoWriter canAddInput:writerInput]);
 [videoWriter addInput:writerInput];

 currentTime = kCMTimeZero;

 adaptor = [[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:nil] retain];


 NSLog(@"Error? %@",error);

}


- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {

 if(recording){


  CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
  currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);


  // STUFF ISN'T WORKING HERE


  BOOL success = [adaptor appendPixelBuffer:imageBuffer withPresentationTime:currentTime];
  NSLog(@"%@",success ? @"YES" : @"NO");
 }



}


- (void) startRecording{
 [videoWriter startWriting];
 [videoWriter startSessionAtSourceTime:currentTime];
 recording = YES;

}

- (void) stopRecording{
 recording = NO;
 [writerInput markAsFinished];
 [videoWriter endSessionAtSourceTime:currentTime];
 [videoWriter finishWriting];
}

2 个答案:

答案 0 :(得分:0)

您需要每次都删除现有文件(在您的情况下为“filePath”),尝试录制视频。

答案 1 :(得分:-1)

[adaptor appendPixelBuffer:imageBuffer withPresentationTime:currentTime];

也许您自己需要经理视频时间,从样本缓冲区获取当前时间,这不是视频时间。尝试累积前帧捕获时间