2010-11-12 75 views
2

我想拍攝相機輸入並使用avassetwriter將數據寫入磁盤。從委託人看來,avassetwriterinputpixelbufferadator未能追加數據。我不確定爲什麼AVAssetWriterInput

- (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]; 
} 
+0

你解決了這個問題嗎?如果是這樣,那麼解決方案是什麼? – zakdances 2012-08-02 14:21:26

回答

0

您需要每次刪除現有文件(在您的案例中是「filePath」),試圖錄制視頻。

-1
[adaptor appendPixelBuffer:imageBuffer withPresentationTime:currentTime]; 

也許你需要自己管理視頻時間,你從sampleBuffer獲得currentTime,它不完全是視頻時間。嘗試累積捕獲時間前幀

相關問題