从视频中获取每一帧图片(包括第一帧)



  1. 1.  

    iphone ios取出视频的每一帧图片

     
  2. [mImageGenerator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)]] completionHandler:  
  3.     ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)  
  4.     {  
  5.    
  6.         NSLog(@"actual got image at time:%f", CMTimeGetSeconds(actualTime));  
  7.         if (image)  
  8.         {  
  9.             [CATransaction begin];  
  10.             [CATransaction setDisableActions:YES];  
  11.             [layer setContents:(id)image];  
  12.    
  13.             //UIImage *img = [UIImage imageWithCGImage:image];  
  14.             //UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);  
  15.    
  16.             [CATransaction commit];  
  17.         }  
  18.     }];  
  19.    


2.(1)如何取得第一帧

AVFoundationframework 

 CoreMediaframework
    + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {  
        AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];  
        NSParameterAssert(asset);  
        AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];  
        assetImageGenerator.appliesPreferredTrackTransform = YES;  
        assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;  
      
        CGImageRef thumbnailImageRef = NULL;  
        CFTimeInterval thumbnailImageTime = time;  
        NSError *thumbnailImageGenerationError = nil;  
        thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];  
      
        if (!thumbnailImageRef)  
            NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);  
      
        UIImage *thumbnailImage = thumbnailImageRef ? [[[UIImage alloc] initWithCGImage:thumbnailImageRef] autorelease] : nil;  


return thumbnailImage;
}


//注:CMTime firstframe=CMTimeMake(1,10);   

“CMTime可是專門用來表示影片時間用的類別,
他的用法為: CMTimeMake(time, timeScale)

time指的就是時間(不是秒),
而時間要換算成秒就要看第二個參數timeScale了.
timeScale指的是1秒需要由幾個frame構成(可以視為fps),
因此真正要表達的時間就會是 time / timeScale 才會是秒.”

上面的代码可以这么理解,视频的fps(帧率)是10,firstframe是第一帧的视频时间为0.1秒,lastframe是第10帧视频时间为1秒。

或者换种写法  CMTime curFrame = CMTimeMake(第几帧, 帧率); CMTimeMake(a, b);  //a当前第几帧, b每秒钟多少帧.当前播放时间a/b  

获得总帧数和帧率的方法

 NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *urlAsString = [mainBundle pathForResource:@"asdf"
                                                 ofType:@"mp4"];
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                     forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    NSURL    *url = [NSURL fileURLWithPath:urlAsString];
    
    AVURLAsset *myAsset = [[AVURLAsset alloc] initWithURL:url options:opts];

  CMTimeValue  value = myAsset.duration.value;//总帧数

 CMTimeScale  timeScale =   myAsset.duration.timescale; //timescale为  fps

    (2).

      MPMoviePlayerController*moviePlayer =      [[MPMoviePlayerControlleralloc] initWithContentURL:videoURL];

      moviePlayer.shouldAutoplay =NO;

     [moviePlayer Play];

     UIImage*thumbnail =  [moviePlayer thumbnailImageAtTime:time    timeOption:MPMovieTimeOptionNearestKeyFrame];

     [MoviePlayer Pause];



你可能感兴趣的:(视频,学习笔记)