How do you convert a video to animated gif using A

2019-05-14 20:52发布

问题:

I am pushing a new UIViewController after recording a movie with AVFoundation and AVCamRecorder.

Currently I'm playing the movie back from the URL the movie was saved at and that's working fine. I am having a huge bit of trouble understanding how to convert the video from the URL to an animated gif.

I'm pretty new to Objective C and had success creating a pretty custom video recorder, but now am discouraged on this next piece.

I have

SecondViewController.h

@interface SecondViewController : UIViewController
@property NSURL *videoURL;
@end

SecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"
#import "CaptureManager.h"
#import "AVCamRecorder.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface SecondViewController ()

@property (nonatomic, strong) CaptureManager *grabFile;
@property (nonatomic, strong) MPMoviePlayerController *movie;

@end

@implementation SecondViewController

- (void)viewDidLoad
{
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL];
    _movie.view.frame = CGRectMake(0.0, 64.0, 320.0, 320.0);
    _movie.movieSourceType = MPMovieSourceTypeFile;
    _movie.repeatMode = MPMovieRepeatModeOne;
    [self.view addSubview:_movie.view];
    [_movie play];
}

I've tried a few things with

AVURLAsset

and

AVAssetImageGenerator

but I can't get far enough to output anything. I'd like to know the steps and if possible some example bits on how to extract the frames into an animated gif. Thanks for reading my question! :)

回答1:

The answer to this was to use CVPixelBuffer on the generated video to get image assets out and into memory, then add the images to NSDictionary and create a gif from the stored images.

How to use CVPixelBuffer
How to turn a CVPixelBuffer into a UIImage?

How to take stored images from a dictionary and create a gif:
Create and and export an animated gif via iOS?