iPhone使用AVFoundation从视频中读取UIimage(帧)

时间:2010-11-16 22:52:13

标签: ios ios4 avfoundation

抱歉我的英文) 通过iPhone查找有关视频读取帧的信息我找到了这个项目http://www.codza.com/extracting-frames-from-movies-on-iphone/comment-page-1#comment-1116,但我也读到了一些可以使用AVFoundation从视频中捕获帧以获得更好性能的地方。

但我无法找到有关我如何做到这一点的信息......

有些想法?

感谢您阅读

5 个答案:

答案 0 :(得分:37)

您正在谈论使用这些电话在特定时间从视频中生成Apple所谓的缩略图

对于MPMoviePlayerController(iOS用于保存文件或其他来源的视频),有两个命令可以执行此操作。第一个在特定时间点从电影生成单个缩略图(图像),第二个生成一组时间范围的缩略图。

此示例在10秒内将图像放入影片剪辑 myMovie.mp4

MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
        initWithContentURL:[NSURL URLWithString:@"myMovie.mp4"]];
UIImage *singleFrameImage = [movie thumbnailImageAtTime:10 
        timeOption:MPMovieTimeOptionExact];

请注意,这会同步执行 - 即,在您获取屏幕截图时,用户将被迫等待。

另一种选择是从电影中获取一系列图像:

MPMoviePlayerController *movie = [[MPMoviePlayerController alloc]
        initWithContentURL [NSURL URLWithString:@"myMovie.mp4"]];
NSNumber time1 = 10;
NSNumber time2 = 11;
NSNumber time3 = 12;
NSArray *times = [NSArray arrayWithObjects:time1,time2,time3,nil];
[movie requestThumbnailImagesAtTimes:times timeOption:MPMovieTimeOptionExact];

每次生成新图像时,第二种方式都会触发MPMoviePlayerThumbnailImageRequestDidFinishNotification类型的通知。你可以设置一个观察者来监控它并处理图像 - 我会让你自己去做这个工作!

答案 1 :(得分:18)

您还可以尝试AVAssetImageGenerator,特别是generateCGImagesAsynchronouslyForTimes:completionHandler

This SO answer有很好的示例代码。

答案 2 :(得分:5)

使用AVAssetImageGenerator获取帧的Swift 2代码:

func previewImageForLocalVideo(url:NSURL) -> UIImage?
{
    let asset = AVAsset(URL: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    imageGenerator.appliesPreferredTrackTransform = true

    var time = asset.duration
    //If possible - take not the first frame (it could be completely black or white on camara's videos)
    time.value = min(time.value, 2)

    do {
        let imageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: nil)
        return UIImage(CGImage: imageRef)
    }
    catch let error as NSError
    {
        print("Image generation failed with error \(error)")
        return nil
    }
}

答案 3 :(得分:1)

在Swift 4中,这对我有一些修改,主要是将imageGenerator.copyCGImage的“at”参数更改为CMTime类型:

func showFrame(from file:String) {
    let file = file.components(separatedBy: ".")
    guard let path = Bundle.main.path(forResource: file[0], ofType:file[1]) else {
        debugPrint( "\(file.joined(separator: ".")) not found")
        return
    }
    let url = URL(fileURLWithPath: path)
    let image = previewImageForLocalVideo(url: url)
    let imgView = UIImageView(image: image)
    view.addSubview(imgView)
}    

func previewImageForLocalVideo(url:URL) -> UIImage? {
    let asset = AVAsset(url: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    imageGenerator.appliesPreferredTrackTransform = true
    let tVal = NSValue(time: CMTimeMake(12, 1)) as! CMTime
    do {
        let imageRef = try imageGenerator.copyCGImage(at: tVal, actualTime: nil)
        return UIImage(cgImage: imageRef)
    }
    catch let error as NSError
    {
        print("Image generation failed with error \(error)")
        return nil
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    showFrame(from:"video.mp4")
}

Source

答案 4 :(得分:1)

以下是从视频获取FPS图像的代码

  

1)导入

#import <Photos/Photos.h>
  

2)在viewDidLoad

    videoUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"VfE_html5" ofType:@"mp4"]];
    [self createImage:5]; // 5 is frame per second (FPS) you can change FPS as per your requirement.
  

3)功能

-(void)createImage:(int)withFPS {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.requestedTimeToleranceAfter =  kCMTimeZero;
    generator.requestedTimeToleranceBefore =  kCMTimeZero;

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  withFPS ; i++){
        @autoreleasepool {
            CMTime time = CMTimeMake(i, withFPS);
            NSError *err;
            CMTime actualTime;
            CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
            UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
            [self savePhotoToAlbum: generatedImage]; // Saves the image on document directory and not memory
            CGImageRelease(image);
        }
    }
}

-(void)savePhotoToAlbum:(UIImage*)imageToSave {

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"sucess.");
        }
        else {
            NSLog(@"fail.");
        }
    }];
}