如何使用 AVFoundation 将视频转换为动画 gif? [英] How do you convert a video to animated gif using AVFoundation?
问题描述
在使用 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
和
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?
如何从字典中获取存储的图像并创建 gif:
通过 iOS 创建和导出动画 gif?
这篇关于如何使用 AVFoundation 将视频转换为动画 gif?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!