微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在3GS设备上,无法使用AVAssetWriter从图像创建影片

如何解决在3GS设备上,无法使用AVAssetWriter从图像创建影片

在3GS设备(IOS 4.1)上,对appendPixelBuffer的调用返回NO,但在iPhone 4设备上运行良好。 问题的根源是对appendPixelBuffer的以下调用
CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[UIImage imageNamed:@\"frame1.png\"] CGImage]];
BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

if (result == NO) //failes on 3GS,but works on iphone 4
    NSLog(@\"Failed to append buffer\");
} 完整代码
-(void)writeImagesAsMovie:(NSArray *)array toPath:(Nsstring*)path {


NSLog(path);

NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];

if(error) {
    NSLog(@\"error creating AssetWriter: %@\",[error description]);
}
错误(仅适用于3GS,iphone 4可以)   错误域= AVFoundationErrorDomain   Code = -11800 \“该操作无法进行   完成。 (AVFoundationErrorDomain   错误-11800。)\“ UserInfo = 0x4970530   {NSUnderlyingError = 0xx4d2c0 \“   操作无法完成。   (Osstatus错误-12908。)\“}
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264,AVVideoCodecKey,[NSNumber numberWithInt:frameSize.width],AVVideoWidthKey,[NSNumber numberWithInt:frameSize.height],AVVideoHeightKey,nil];



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

NSMutableDictionary *attributes = [[NSMutableDictionary alloc]init];
[attributes setobject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(Nsstring*)kCVPixelBufferPixelFormatTypeKey];
[attributes setobject:[NSNumber numberWithUnsignedInt:frameSize.width] forKey:(Nsstring*)kCVPixelBufferWidthKey];
[attributes setobject:[NSNumber numberWithUnsignedInt:frameSize.height] forKey:(Nsstring*)kCVPixelBufferHeightKey];

AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                 sourcePixelBufferAttributes:attributes];

[videoWriter addInput:writerInput];

// fixes all errors
writerInput.expectsMediaDataInRealTime = YES;

//Start a session:
BOOL start = [videoWriter startWriting];
NSLog(@\"Session started? %d\",start);

[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[UIImage imageNamed:@\"frame1.png\"] CGImage]];
BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

if (result == NO) //failes on 3GS,but works on iphone 4
    NSLog(@\"Failed to append buffer\");

if(buffer)
    CVBufferRelease(buffer);

[NSThread sleepForTimeInterval:0.05];
//for (int i = 1;i<[array count]; i++)
for (int i = 1;i<20; i++)
{
    if (adaptor.assetWriterInput.readyForMoreMediaData) 
    {
        NSLog(@\"inside for loop %d\",i);
        CMTime frameTime = CMTimeMake(1,15);
        CMTime lastTime=CMTimeMake(i,15); 
        CMTime presentTime=CMTimeAdd(lastTime,frameTime);
        Nsstring *imgName = [Nsstring stringWithFormat:@\"frame%d.png\",i];
        UIImage *imgFrame = [UIImage imageNamed:imgName] ;
        buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]];
        BOOL result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

        if (result == NO) //failes on 3GS,but works on iphone 4
        {
            NSLog(@\"Failed to append buffer\");
            NSLog(@\"The error is %@\",[videoWriter error]);
        }
        if(buffer)
            CVBufferRelease(buffer);
        [NSThread sleepForTimeInterval:0.05];
    }
    else
    {
        NSLog(@\"error\");
        i--;
    }
}

//Finish the session:
[writerInput markAsFinished];
[videoWriter finishWriting];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
[videoWriter release];
[writerInput release];

NSLog(@\"Movie created successfully\");

}

- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES],kCVPixelBufferCGImageCompatibilityKey,[NSNumber numberWithBool:YES],kCVPixelBufferCGBitmapContextCompatibilityKey,nil];
    CVPixelBufferRef pxbuffer = NULL;

    CVPixelBufferCreate(kcfAllocatorDefault,self.view.frame.size.width,self.view.frame.size.height,kCVPixelFormatType_32ARGB,(CFDictionaryRef) options,&pxbuffer);

    CVPixelBufferLockBaseAddress(pxbuffer,0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata,8,4*self.view.frame.size.width,rgbColorSpace,kCGImageAlphaNoneskipFirst);

    CGContextConcatCTM(context,CGAffineTransformMakeRotation(0));
    CGContextDrawImage(context,CGRectMake(0,CGImageGetWidth(image),CGImageGetHeight(image)),image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);

    CVPixelBufferUnlockBaseAddress(pxbuffer,0);

    return pxbuffer;
}
    

解决方法

检查管理器的控制台日志中的设备。 我在Ipod 2nd Gen和控制台日志上报告了相同的错误 com.apple.mediaserverd [18]:VTSelectAndCreateVideoEncoderInstance:未找到\'avc1 \'的视频编码器 确实这意味着仍在努力,但这为我指明了方向。     ,使用它运行在iPhone 3gs设备上的https://github.com/caferrara/img-to-video。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。