2011-12-13 38 views
1

我已经完成图像视频转换在iPhone(当然,我从堆栈溢出问题得到的代码)。但问题是录制视频的速度非常快,即使我拥有2250帧左右的帧,它也会在2秒内跑掉。我知道问题在于帧率。 但我不知道如何使其正确。 我的代码如下iphone图像视频问题视频速度

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:@"test.mov"]; 

if ([self openVideoFile:myFilePath withSize:CGSizeMake (480.0, 320.0)]) { 
    for (int i=1; i<2226; i++) { 
     NSString *imagename=[NSString stringWithFormat:@"1 (%i).jpg",i]; 
     UIImage *image=[ UIImage imageNamed:imagename]; 
     [self writeImageToMovie:[image CGImage]]; 
    } 
    [videoWriter finishWriting]; 
} 
else { 
    NSLog(@"friled to open video file"); 
} 

这个代码是在调用函数的功能defenitions下面

- (BOOL) openVideoFile: (NSString *) path withSize:(CGSize)imageSize { 
    CGSize size = CGSizeMake (480.0, 320.0);//imageSize; 

    NSError *error = nil; 
    videoWriter = [[AVAssetWriter alloc] initWithURL: 
        [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie 
               error:&error]; 
    if (error != nil){ 
     NSLog(@"error>>>> %@",error); 
     return NO; 
    } 

    NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
               [NSNumber numberWithDouble:size.width], AVVideoCleanApertureWidthKey, 
               [NSNumber numberWithDouble:size.height], AVVideoCleanApertureHeightKey, 
               [NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey, 
               [NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey, 
               nil]; 


    NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
               [NSNumber numberWithInt:1], AVVideoPixelAspectRatioHorizontalSpacingKey, 
               [NSNumber numberWithInt:1],AVVideoPixelAspectRatioVerticalSpacingKey, 
               nil]; 



    NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
            videoCleanApertureSettings, AVVideoCleanApertureKey, 
            videoAspectRatioSettings, AVVideoPixelAspectRatioKey, 
            nil]; 

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys: 
            AVVideoCodecH264, AVVideoCodecKey, 
            codecSettings,AVVideoCompressionPropertiesKey, 
            [NSNumber numberWithDouble:size.width], AVVideoWidthKey, 
            [NSNumber numberWithDouble:size.height], AVVideoHeightKey, 
            nil]; 
    writerInput = [[AVAssetWriterInput 
        assetWriterInputWithMediaType:AVMediaTypeVideo 
        outputSettings:videoSettings] retain]; 
    NSMutableDictionary * bufferAttributes = [[NSMutableDictionary alloc] init]; 
    [bufferAttributes setObject: [NSNumber numberWithInt: kCVPixelFormatType_32ARGB] 
         forKey: (NSString *) kCVPixelBufferPixelFormatTypeKey]; 
    [bufferAttributes setObject: [NSNumber numberWithInt: 480] 
         forKey: (NSString *) kCVPixelBufferWidthKey]; 
    [bufferAttributes setObject: [NSNumber numberWithInt: 320] 
         forKey: (NSString *) kCVPixelBufferHeightKey]; 


    adaptor = [[AVAssetWriterInputPixelBufferAdaptor 
       assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput 
       sourcePixelBufferAttributes:nil] retain]; 

    NSMutableDictionary*  attributes; 
    attributes = [NSMutableDictionary dictionary]; 

    int width = 480; 
    int height = 320; 

    [attributes setObject:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey]; 
    [attributes setObject:[NSNumber numberWithInt:width] forKey: (NSString*)kCVPixelBufferWidthKey]; 
    [attributes setObject:[NSNumber numberWithInt:height] forKey: (NSString*)kCVPixelBufferHeightKey]; 
    CVReturn theError = CVPixelBufferPoolCreate(kCFAllocatorDefault, NULL, (CFDictionaryRef) attributes, &pixelBufferPool);           


    NSParameterAssert(writerInput); 
    NSParameterAssert([videoWriter canAddInput:writerInput]); 
    [videoWriter addInput:writerInput]; 

    writerInput.expectsMediaDataInRealTime = YES; 
    [videoWriter startWriting]; 
    [videoWriter startSessionAtSourceTime:kCMTimeZero]; 

    buffer = NULL; 
    lastTime = kCMTimeZero; 
    presentTime = kCMTimeZero; 

    return YES; 
} 






    - (void) writeImageToMovie:(CGImageRef)image 
{ 
    if([writerInput isReadyForMoreMediaData]) 
    { 
     buffer = [self pixelBufferFromCGImage:image]; 
     BOOL success = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime]; 
     if (!success) NSLog(@"Failed to appendPixelBuffer"); 
     CVPixelBufferRelease(buffer); 

     presentTime = CMTimeAdd(lastTime, CMTimeMake(1, 1000));//I think problem is here but what will be given for correct output 
     lastTime = presentTime; 
    } 
    else 
    { 
     NSLog(@"error - writerInput not ready"); 
    } 
} 


    - (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image 
{ 
    CGSize size = CGSizeMake (480.0, 320.0); 
    CVPixelBufferRef pxbuffer; 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, 
          [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, 
          nil]; 
    if (pixelBufferPool == NULL) NSLog(@"pixelBufferPool is null!"); 
    CVReturn status = CVPixelBufferPoolCreatePixelBuffer (NULL, pixelBufferPool, &pxbuffer); 
    CVPixelBufferLockBaseAddress(pxbuffer, 0); 
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); 

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pxdata, size.width, 
               size.height, 8, 4*size.width, rgbColorSpace, 
               kCGImageAlphaNoneSkipFirst); 
    CGContextConcatCTM(context, CGAffineTransformMakeRotation(0)); 
    CGContextDrawImage(context, CGRectMake(90, 10, CGImageGetWidth(image), 
              CGImageGetHeight(image)), image); 
    CGColorSpaceRelease(rgbColorSpace); 
    CGContextRelease(context); 

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0); 

    return pxbuffer; 
} 

给做什么用的CMTime变量,我该如何正确 一个更多的帮助,使它我怎样才能用这个视频添加音频。

+0

创建视频我还一个问题应用程序崩溃,没有在日志中打印它只发生在视频记录使用彩色图像时发生,而不是在使用普通白色图像时发生崩溃 – Johnykutty 2011-12-22 12:29:44

回答

1

您的PTS非常接近。为什么不是CMTimeMake(1, 1000),而不是30FPS:CMTimeMake(1, 30))

0

我修改了你的代码,并提出了解决方案,在我的情况下,我在0.1秒内记录了我的视图中的每个图像,所以我的fps是0.1fps。

presentTime = CMTimeAdd(lastTime, CMTimeMake(1, 10)); 

您的视频过快的原因是因为您的比例是1000秒图像中的1秒。您可以每秒制作1张图片。

presentTime = CMTimeAdd(lastTime, CMTimeMake(1, 1)); 

试试这个,希望它能帮到你。

0

添加这个库 AVFoundation CoreMedia corevideo的

我能够生成视频,但没能获得音频是人有使用此代码,并与音频