簡體   English   中英

AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer

[英]AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer

我使用下面的代碼將圖像寫入視頻

-(void) writeImagesToMovieAtPath:(NSString *) path withSize:(CGSize) size
{
    NSLog(@"Write Started");

    NSError *error = nil;

    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                                  [NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4
                                                              error:&error];
    NSParameterAssert(videoWriter);

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                   AVVideoCodecH264, AVVideoCodecKey,
                                   [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                                   [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                                   nil];

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


    AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                     assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                     sourcePixelBufferAttributes:nil];

    NSParameterAssert(videoWriterInput);
    NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
    videoWriterInput.expectsMediaDataInRealTime = YES;
    [videoWriter addInput:videoWriterInput];

    //Start a session:
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:kCMTimeZero];

    CVPixelBufferRef buffer = NULL;

    //convert uiimage to CGImage.

    int frameCount = 16;
    int kRecordingFPS = 30;
    UIImage * im = [UIImage imageNamed:@"IMG_0103.JPG"];

    NSArray * imageArray = [[NSArray alloc]initWithObjects:im,im,im,im,im,im,im,im,im,im,im,im,im,im,im,im, nil];

    for(UIImage * img in imageArray)
    {
        buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:size];

        BOOL append_ok = NO;
        int j = 0;
        while (!append_ok && j < 30)
        {
            if (adaptor.assetWriterInput.readyForMoreMediaData)
            {
                printf("appending %d attemp %d\n", frameCount, j);

                CMTime frameTime = CMTimeMake(frameCount,(int32_t) kRecordingFPS);
                append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

                if(buffer)
                    CVBufferRelease(buffer);
                [NSThread sleepForTimeInterval:0.05];
            }
            else
            {
                printf("adaptor not ready %d, %d\n", frameCount, j);
                [NSThread sleepForTimeInterval:0.1];
            }
            j++;
        }
        if (!append_ok) {
            printf("error appending image %d times %d\n", frameCount, j);
        }
        frameCount++;
    }


    //Finish the session:
    [videoWriterInput markAsFinished];
    [videoWriter finishWriting];
    NSLog(@"Write Ended");
    [videoWriterInput release];
    [videoWriter release];
    [imageArray release];
}

它第一次工作,但如果我運行2,3次或更多的代碼,它報告:

!appending 0 0-0 attemp 0
!appending 0 0-0 attemp 1
My App(11385,0xb0103000) malloc: *** error for object 0x9ab4270: double free
*** set a breakpoint in malloc_error_break to debug
!appending 0 0-0 attemp 2
My App(11385,0xb0103000) malloc: *** error for object 0x9ab4270: double free
*** set a breakpoint in malloc_error_break to debug
!appending 0 0-0 attemp 3

Soemtime它導致崩潰

我試圖找到原因,但沒有結果,

您的評論歡迎

而不是使用[NSThread sleepForTimeInterval:0.1]; 使用while(!adaptor.assetWriterInput.readyForMoreMediaData){}

因為很多次readyForMoreMediaData在0.1時間間隔內從未設置為YES。

還試着釋放appendPixelBuffer占用的內存:withPresentationTime:方法實際上這個方法阻止你成功運行

您需要使用CFRelease()釋放緩沖區,如“創建規則”中所述

if ([assetWriterInput isReadyForMoreMediaData]) {
    CVPixelBufferRef pixelBuffer = [self newPixelBufferFromCGImage:image.CGImage];
    CMTime frameTime = CMTimeMake(frameCount,(int32_t) kRecordingFPS);
    if (![adaptor appendPixelBuffer:pixelBuffer withPresentationTime:frameTime]) {
        NSError *error = [videoWriter error];
        NSLog(@"failed to append sbuf: %@", error);
    }
    CFRelease(pixelBuffer);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM