如何使用图像创建视频时在图像更改上添加动画 [英] How to add animation on image change while create video using images

查看:120
本文介绍了如何使用图像创建视频时在图像更改上添加动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像数组,我想通过在序列中一个接一个地播放这些图像来创建视频。我想在图像变化时添加不同类型的动画。建议我使用Cocoa框架在Objective-C中实现此功能的一些方法或任何解决方案。

I have an array of images from which I want to create a video by playing these images one after the other in a sequence.I want to add a different types of animation when image get change. Suggest me some method or any solution to achieve this functionality in Objective-C with the Cocoa framework.

这是制作图像视频的工作代码,但请建议如何我们在制作视频时动画图像:

Here is working code for making video of images, But please suggest that how we animate images while making video:

-(void)createVideoFromImages:(NSString *) path withSize:(CGSize) size
{
    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];

    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];

    //Video encoding
    CVPixelBufferRef buffer = NULL;

    //convert uiimage to CGImage.
    int frameCount = 0;

    for(int i = 0; i<[arrPicture count]; i++)
    {
        buffer = [self bufferImageFromCGImage:[[arrPicture objectAtIndex:i] CGImage] size:size];

        __block  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)1);
                append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

                // CVPixelBufferPoolRef bufferPool = adaptor.pixelBufferPool;
                // NSParameterAssert(bufferPool != NULL);
                [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++;
        CVBufferRelease(buffer);
    }

    [videoWriterInput markAsFinished];
    [videoWriter finishWritingWithCompletionHandler:^{
        NSLog(@"Finished writing...checking completion status...");
        if (videoWriter.status != AVAssetWriterStatusFailed && videoWriter.status == AVAssetWriterStatusCompleted)
        {
            NSLog(@"Video writing succeeded.");

            // Move video to camera roll
            // NOTE: You cannot write directly to the camera roll.
            // You must first write to an iOS directory then move it!
            NSURL *videoTempURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", path]];
        } else
        {
            NSLog(@"Video writing failed: %@", videoWriter.error);
        }
    }];
}


-(CVPixelBufferRef) bufferImageFromCGImage:(CGImageRef)image size:(CGSize)size
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES],     kCVPixelBufferCGImageCompatibilityKey,
                             [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                             nil];
    CVPixelBufferRef pxbuffer = NULL;
    CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width,
                                          size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
                                          &pxbuffer);
    status=status;//Added to make the stupid compiler not show a stupid warning.
    NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

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

    CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
                                                 size.height, 8, 4*size.width, rgbColorSpace,
                                                 kCGImageAlphaNoneSkipFirst);
    NSParameterAssert(context);

    //CGContextTranslateCTM(context, 0, CGImageGetHeight(image));
    //CGContextScaleCTM(context, 1.0, -1.0);//Flip vertically to account for different origin
    long width,height;
    if (CGImageGetWidth(image) > self.view.frame.size.width) {
        width = self.view.frame.size.width;
    }else{
        width = self.view.frame.size.width; //  CGImageGetWidth(image);
    }

    if (CGImageGetHeight(image) > self.view.frame.size.height) {
        height = self.view.frame.size.height;
    }else{
        height = self.view.frame.size.height -64 ;//  CGImageGetHeight(image);
    }
    CGContextDrawImage(context, CGRectMake(0, 0, width,
                                           height), image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);

    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

    return pxbuffer;
}


推荐答案

通过使用你的代码,它不是可以在处理视频时为图像添加动画

By using your code it's not possible to animate image while processing video

但是有一个想法可以制作动画图像视频

But there is one idea to make video of animated images

你可以播放动画时记录屏幕
使用此代码记录屏幕

https:// github.com/alskipp/ASScreenRecorder

You can record the screen while playing animation use this code to record screen
https://github.com/alskipp/ASScreenRecorder

这篇关于如何使用图像创建视频时在图像更改上添加动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆