如何使用AVFoundation将视频转换为动画gif? [英] How do you convert a video to animated gif using AVFoundation?

查看:210
本文介绍了如何使用AVFoundation将视频转换为动画gif?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AVFoundation和AVCamRecorder录制电影后,我正在推送新的UIViewController.

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

目前,我正在从保存电影的URL播放电影,并且工作正常.在了解如何将视频从URL转换为动画gif时,我遇到了很多麻烦.

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.

我是Objective C的新手,并且成功创建了漂亮的自定义录像机,但是现在不鼓励使用下一个.

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

我有

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

我已经尝试过

AVURLAsset

AVURLAsset

AVAssetImageGenerator

AVAssetImageGenerator

但是我无法输出任何东西.我想知道步骤,如果可能的话,我想知道一些有关如何将帧提取到动画gif中的示例位.感谢您阅读我的问题! :)

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! :)

推荐答案

此问题的答案是对生成的视频使用CVPixelBuffer将图像资产取出并存储到内存中,然后将图像添加到NSDictionary并创建存储的图像中保存为gif.

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.

如何使用CVPixelBuffer
如何将CVPixelBuffer转换为UIImage?

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

如何从字典中获取存储的图像并创建gif:
通过iOS创建和导出动画gif吗?

这篇关于如何使用AVFoundation将视频转换为动画gif?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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