您的位置:首页 > 移动开发 > IOS开发

avfoundation 合成一组图片到视频

2016-05-27 19:21 645 查看
直接上代码,注释都有,不在赘述

//按钮点击

- (IBAction)mergeVideo:(id)sender {

    //虚拟一个将要存放的视频

    NSString *path = [GG pathForDocument:@"b.mp4"];

    [self writeImageAsMovie:@[[UIImage imageNamed:@"4.PNG"]] toPath:path size:self.view.frame.size duration:3];

}

//向虚拟的视频地址里面写入图片 这里只写一张,您可以多写几张

-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration

{

    

    NSError *error = nil;

    

    AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:

                                  [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie

                                                              error:&error];

    

    NSParameterAssert(videoWriter);

    //视频配置

    NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:

                                   AVVideoCodecH264, AVVideoCodecKey,

                                   [NSNumber numberWithInt:size.width], AVVideoWidthKey,

                                   [NSNumber numberWithInt:size.height], AVVideoHeightKey,

                                   nil];

    //写入

    AVAssetWriterInput* writerInput = [AVAssetWriterInput

                                        assetWriterInputWithMediaType:AVMediaTypeVideo

                                        outputSettings:videoSettings];

    

    AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor

                                                     assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput

                                                     sourcePixelBufferAttributes:nil];

    

    

    NSParameterAssert(writerInput);

    NSParameterAssert([videoWriter canAddInput:writerInput]);

    [videoWriter addInput:writerInput];

    

    //开启 session:

    [videoWriter startWriting];

    [videoWriter startSessionAtSourceTime:kCMTimeZero];

    

    CVPixelBufferRef buffer = NULL;

    

    //转换为 CGImage.

    buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];

    [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

    

    //下面可以自己发挥多写入几张图片:

    

    __weak typeof (self) weakSelf = self;

    

    //完成session:

    [writerInput markAsFinished];

    [videoWriter finishWritingWithCompletionHandler:^{

        NSLog(@"write finished");

        //保存至相册

        [weakSelf saveToiPhoneLib:path];

    }];

    

}

//转换image到cgimage

- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image

{

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

                             [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,

                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,

                             nil];

    CVPixelBufferRef pxbuffer = NULL;

    

    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, self.view.frame.size.width,

                                          self.view.frame.size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,

                                          &pxbuffer);

    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

    

    CVPixelBufferLockBaseAddress(pxbuffer, 0);

    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);

    NSParameterAssert(pxdata != NULL);

    

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(pxdata, self.view.frame.size.width,

                                                 self.view.frame.size.height, 8, 4*self.view.frame.size.width, rgbColorSpace,

                                                 kCGImageAlphaNoneSkipFirst);

    NSParameterAssert(context);

    CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));

    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),

                                           CGImageGetHeight(image)), image);

    CGColorSpaceRelease(rgbColorSpace);

    CGContextRelease(context);

    

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

    

    return pxbuffer;

}

//保存至相册

-(void) saveToiPhoneLib:(NSString *)path{

    UISaveVideoAtPathToSavedPhotosAlbum (path,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);

}

//保存完成回调

- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {

    NSString *result = error ? [NSString stringWithFormat:@"失败:%@", error.localizedFailureReason] : @"成功:请查看您的相册";

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"合成结果" message:result

                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

}




$(".MathJax").remove();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios avfoundation 视频合