1

我需要播放視頻。當視頻播放時,我希望能夠點擊視頻並提取它的縮略圖。使用水龍頭從視頻獲取縮略圖

我一直在閱讀文檔/ SO,好像我可以通過AVFoundation/AVPlayer或MPMoviePlayerController捕獲圖像。兩者都需要時間/時間範圍來獲取圖像。但是,我怎樣才能提取圖像,而不是輕敲玩家呢?

我想我可以使用UIGestureRecognizer,但是如何創建水龍頭和電影時間之間的關係?我應該使用AVFoundation還是MPMoviePlayerController?

欣賞任何提示,我對此有限的經驗。

回答

2

我已經創建了一個代碼來在特定時間獲取幀,如下所示。請檢查它可能會幫助你。

-(void)getArrayOfFrameFromVideoURLs:(NSURL*)outputFileURL 
{ 

    ImagesMainArray = [NSMutableArray array]; 
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:outputFileURL 
               options:[NSDictionary  dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],  AVURLAssetPreferPreciseDurationAndTimingKey, nil]] ; 
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]  initWithAsset:asset]; 
    generator.appliesPreferredTrackTransform = YES; // if I omit this, the frames are rotated 90° (didn't try in landscape) 
    AVVideoComposition * composition = [AVVideoComposition videoCompositionWithPropertiesOfAsset:asset]; 

    // Retrieving the video properties 
    NSTimeInterval duration = CMTimeGetSeconds(asset.duration); 
    CGFloat frameDuration = CMTimeGetSeconds(composition.frameDuration); 
    CGSize renderSize = composition.renderSize; 
    CGFloat totalFrames = round(duration/frameDuration); 

    // Selecting each frame we want to extract : all of them. 

    // Get Count of Frames Per Second 
    AVAssetTrack * videoATrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject]; 
    float fps = 0.0; 
    if(videoATrack) { 
     fps = videoATrack.nominalFrameRate; 
    } 
    NSLog(@"FramesPerSecond=%f",fps); 
    NSMutableArray * times = [NSMutableArray  arrayWithCapacity:round(duration/frameDuration)]; 

    for (int i=0; i<totalFrames; i++) { 
     NSValue *time = [NSValue valueWithCMTime:CMTimeMakeWithSeconds(i*frameDuration, composition.frameDuration.timescale)]; 
     [times addObject:time]; 
    } 

    __block int i = 0; 
    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){ 
     if (result == AVAssetImageGeneratorSucceeded) { 
      [ImagesMainArray addObject:[UIImage imageWithCGImage:im]]; 
     } 
     else 
      NSLog(@"Ouch: %@", error.description); 
     i++; 
     if(i == totalFrames) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [self showArrayOfImgsMain]; 
      }); 
     } 
    }; 

    // Launching the process... 
    generator.requestedTimeToleranceBefore = kCMTimeZero; 
    generator.requestedTimeToleranceAfter = kCMTimeZero; 
    generator.maximumSize = renderSize; 
    [generator generateCGImagesAsynchronouslyForTimes:times completionHandler:handler]; 

} 

更多,您可以檢查:

https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.htm

http://iosguy.com/tag/avplayer/

https://stackoverflow.com/a/16398642

+0

感謝您的建議。我決定使用MPMoviePlayerController,因爲它看起來更簡單一些。 – 2015-02-08 05:58:27