首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAssetWriter内存错误

AVAssetWriter内存错误
EN

Stack Overflow用户
提问于 2014-07-15 12:53:22
回答 1查看 1.3K关注 0票数 0

我没有几种方法可以用mov文件来编写视频来临时执行dir,但是在15秒之后。我在犯错误:

  • 收到了内存警告。
  • 收到了内存警告。
  • 收到了内存警告。
  • 收到了内存警告。

然后app就要崩溃了。我被困住了不知道出了什么事..。

代码语言:javascript
复制
- (void) saveVideoToFileFromBuffer:(CMSampleBufferRef) buffer {


    if (!movieWriter) {

        NSString *moviePath = [NSString stringWithFormat:@"%@tmpMovie", NSTemporaryDirectory()];

        if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath])
            [self removeMovieAtPath:moviePath];

        NSError *error = nil;

        movieWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:moviePath] fileType: AVFileTypeQuickTimeMovie error:&error];
        if (error) {

            m_log(@"Error allocating AssetWriter: %@", [error localizedDescription]);

        } else {

            CMFormatDescriptionRef description = CMSampleBufferGetFormatDescription(buffer);

            if(![self setUpMovieWriterObjectWithDescriptor:description])
                    m_log(@"ET go home, no video recording!!");
        }

    }

    if (movieWriter.status != AVAssetWriterStatusWriting) {

        [movieWriter startWriting];

        [movieWriter startSessionAtSourceTime:kCMTimeZero];

        apiStatusChangeIndicator = NO;

    }

    if (movieWriter.status == AVAssetWriterStatusWriting) {

        if (![movieInput appendSampleBuffer:buffer]) m_log(@"Failed to append sample buffer!");

    }


}

其余代码:

代码语言:javascript
复制
- (BOOL) setUpMovieWriterObjectWithDescriptor:(CMFormatDescriptionRef) descriptor {

    CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(descriptor);

    NSDictionary *compressionSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoProfileLevelH264Baseline31,AVVideoProfileLevelKey,
                                        [NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey, nil];
    //AVVideoProfileLevelKey set because of errors


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


    if ([movieWriter canApplyOutputSettings:videoSettings forMediaType:AVMediaTypeVideo]) {

        movieInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
        movieInput.expectsMediaDataInRealTime = YES;

        if ([movieWriter canAddInput:movieInput]) {

            [movieWriter addInput:movieInput];
        } else {

            m_log(@"Couldn't apply video input to Asset Writer!");

            return NO;
        }

    } else {

        m_log(@"Couldn't apply video settings to AVAssetWriter!");

        return NO;
    }



    return YES;
}

如果有人能指出我的错误就太好了!如果需要,可以共享更多代码。SampleBuffer来自带有过滤器的CIImage。

现在新事物,我可以录制几秒钟的电影,并保存它,但它都是黑色屏幕.

更新

保存视频是可行的,但是从CMSampleBufferRef创建CIImage失败。这是我得到绿色或黑色屏幕的原因,下面是代码:

代码语言:javascript
复制
- (CMSampleBufferRef) processCIImageToPixelBuffer:(CIImage*) image andSampleBuffer:(CMSampleTimingInfo) info{

    CVPixelBufferRef renderTargetPixelBuffer;

    CFDictionaryRef empty;
    CFMutableDictionaryRef attrs;

    empty = CFDictionaryCreate(kCFAllocatorDefault,
                               NULL,
                               NULL,
                               0,
                               &kCFTypeDictionaryKeyCallBacks,
                               &kCFTypeDictionaryValueCallBacks);

    attrs = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                      1,
                                      &kCFTypeDictionaryKeyCallBacks,
                                      &kCFTypeDictionaryValueCallBacks);

    CFDictionarySetValue(attrs,
                         kCVPixelBufferIOSurfacePropertiesKey,
                         empty);

    CVReturn cvError = CVPixelBufferCreate(kCFAllocatorSystemDefault,
                                           [image extent].size.width,
                                           [image extent].size.height,
                                           kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,
                                           attrs,
                                           &renderTargetPixelBuffer);
    if (cvError != 0) {

        m_log(@"Error when init Pixel buffer: %i", cvError);

    }

    CFRelease(empty);
    CFRelease(attrs);

    CVPixelBufferLockBaseAddress(renderTargetPixelBuffer, 0 );

    [_coreImageContext render:image toCVPixelBuffer:renderTargetPixelBuffer];

    CVPixelBufferUnlockBaseAddress(renderTargetPixelBuffer, 0 );

    CMVideoFormatDescriptionRef videoInfo = NULL;
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, renderTargetPixelBuffer, &videoInfo);

    CMSampleBufferRef recordingBuffer;


    OSStatus cmError = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, renderTargetPixelBuffer, true, NULL, NULL, videoInfo, &info, &recordingBuffer);

    if (cmError != 0 ) {

        m_log(@"Error creating sample buffer: %i", (int)cmError);
    }

    CVPixelBufferRelease(renderTargetPixelBuffer);
    renderTargetPixelBuffer = NULL;
    CFRelease(videoInfo);
    videoInfo = NULL;

    return recordingBuffer;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-15 13:31:28

您应该使用配置文件工具检查您的代码。尤其是内存泄漏。可能您没有释放示例缓冲区:

代码语言:javascript
复制
CMSampleBufferInvalidate(buffer);
CFRelease(buffer);
buffer = NULL;
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24758715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档