【问题标题】:How do you convert a video to animated gif using AVFoundation?如何使用 AVFoundation 将视频转换为动画 gif?
【发布时间】:2014-04-19 16:42:01
【问题描述】:

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

目前我正在从保存电影的 URL 播放电影,并且工作正常。我很难理解如何将视频从 URL 转换为动画 gif。

对 Objective C 很陌生,并且成功地创建了一个非常自定义的视频录像机,但现在我对下一个作品感到沮丧。

我有

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

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

【问题讨论】:

  • 我认为 AVFoundation 对 GIF 格式一无所知,您可能只想从视频中提取图像(使用 AVAssetReader 或 AVAssetImageGenerator),然后使用某个库创建动画 gif。

标签: objective-c avfoundation animated-gif


【解决方案1】:

解决办法是在生成的视频上使用CVPixelBuffer 将图像资源取出并存入内存,然后将图像添加到NSDictionary 并从存储的图像创建一个gif。

如何使用CVPixelBuffer
How to turn a CVPixelBuffer into a UIImage?

如何从字典中获取存储的图像并创建 gif:
Create and and export an animated gif via iOS?

【讨论】:

    猜你喜欢
    • 2015-12-03
    • 2020-03-17
    • 2018-07-09
    • 2017-04-20
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多