Записвайте видео с AVCaptureVideoDataOutput

Не съм сигурен какво трябва да сложа в метода

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

За да запишете кадрите към видеоклип. Може ли някой да сподели с мен тялото на техния код за този метод, където резултатите записват кадрите към филм?

Мислех, че съм настроил правилно assetWriter и videoInput, но всичко, което получавам, е филм с 1 кадър, използван многократно.


person user997533    schedule 09.07.2012    source източник
comment
Вижте кода на AVCamDemo от WWDC 2010. Има пример, който ще ви покаже какво да правите.   -  person Steve McFarlin    schedule 10.07.2012
comment
Ако не е необходимо действително да инспектирате/модифицирате/показвате кадрите, можете да замените AVCaptureVideoDataOutput и AVAssetWriter с по-простия AVCaptureMovieFileOutput.   -  person Rhythmic Fistman    schedule 10.07.2012


Отговори (2)


Проверете примерния код на Apple Розов писател. Това е много добър пример за това, което търсите.

person Sj.    schedule 24.09.2012

Можете успешно да записвате видео и да хващате кадри едновременно с помощта на този метод:

AVCaptureSession *captureSession = [AVCaptureSession new];
AVCaptureDevice *captureDevice = [AVCaptureDevice new];
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput new];
AVCaptureVideoDataOutput *output = [AVCaptureVideoDataOutput new];


NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, AVVideoCodecH264, AVVideoCodecKey, nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput  assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];


/ AVCaptureVideDataOutput /
AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor =
           [[AVAssetWriterInputPixelBufferAdaptor alloc] 
                initWithAssetWriterInput:assetWriterInput 
                sourcePixelBufferAttributes:
                     [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], 
                           kCVPixelBufferPixelFormatTypeKey,
                     nil]];


/* Asset writer with MPEG4 format*/
AVAssetWriter *assetWriterMyData = [[AVAssetWriter alloc]
                                initWithURL:URLFromSomwhere
                                fileType:AVFileTypeMPEG4
                                error:you need to check error conditions,
                                      this example is too lazy];
[assetWriterMyData addInput:assetWriterInput];
assetWriterInput.expectsMediaDataInRealTime = YES;

/ Start writing data /

[assetWriterMyData startWriting];
[assetWriterMyData startSessionAtSourceTime:kCMTimeZero];
[captureSession startRunning];



- (void)        captureOutput:(AVCaptureOutput *)captureOutput 
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
           fromConnection:(AVCaptureConnection *)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    // a very dense way to keep track of the time at which this frame
    // occurs relative to the output stream, but it's just an example!
    static int64_t frameNumber = 0;
    if(assetWriterInput.readyForMoreMediaData)
        [pixelBufferAdaptor appendPixelBuffer:imageBuffer
                         withPresentationTime:CMTimeMake(frameNumber, 25)];
    frameNumber++;
}

/* To stop recording, stop capture session and finish writing data*/

[captureSession stopRunning];
[assetWriterMyData finishWriting];
person TestGrid.io Team    schedule 07.07.2016