iOS 之从影片中获取每一帧图片

  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

[html] view plaincopy
  1. + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {  
  2.     AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];  
  3.     NSParameterAssert(asset);  
  4.     AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];  
  5.     assetImageGenerator.appliesPreferredTrackTransform = YES;  
  6.     assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;  
  7.   
  8.     CGImageRef thumbnailImageRef = NULL;  
  9.     CFTimeInterval thumbnailImageTime = time;  
  10.     NSError *thumbnailImageGenerationError = nil;  
  11.     thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];  
  12.   
  13.     if (!thumbnailImageRef)  
  14.         NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);  
  15.   
  16.     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 <span style="font-size: 14px; color: rgb(255, 0, 0); font-family: Times;">= myAsset.duration.value;//</span><span style="color: rgb(255, 0, 0); font-size: 14px; font-family: Times;">总帧数</span>
<pre class="declaration" name="code" style="margin: 0px 0.333em 1em 0.5em; white-space: pre-wrap; word-wrap: break-word; font-family: Courier, Consolas, monospace;"> CMTimeScale  timeScale = <span style="color: rgb(255, 0, 0); font-family: Times; font-size: 14px;">  myAsset.duration.timescale; //</span><span style="color: rgb(255, 0, 0); font-size: 14px; font-family: Times;">timescale为  fps</span>
 
  

   

  1. (2).

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

      moviePlayer.shouldAutoplay =NO;

     [moviePlayer Play];

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

     [MoviePlayer Pause];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值