无法从视频中提取每个帧

时间:2014-07-22 07:04:33

标签: objective-c ios7 mpmovieplayercontroller avaudioplayer avassetimagegenerator

我有30秒的7秒MOV视频,但是当我尝试将代码循环到多个值时,它无法正常工作。即使我能够写出所有帧,它总是给我7-8个独特的帧。 请求的时间总是不同,但我总是得到实际的时间框架。

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"MOV"]]];
moviePlayer.shouldAutoplay = NO;


NSMutableArray *timesm=[[NSMutableArray alloc]init];
for (int i=0; i<frames; i++) {
    CMTime firstThird = CMTimeMakeWithSeconds(videoDurationSeconds *i/(float)frames, 600);

    [timesm addObject:[NSValue valueWithCMTime:firstThird]];
    double abc=firstThird.value/600;
    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:abc timeOption:MPMovieTimeOptionExact];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"zshipra %i.jpg",i]];
    NSData *imageData = UIImageJPEGRepresentation(thumbnail,0.7);
    [imageData writeToFile:savedImagePath atomically:NO];
}

我还尝试了另一种来自Apple开发者网站的方式,但两者都是从7秒视频开始提供7帧。

AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime videoDuration = asset1.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);
int frames=(int)videoDurationSeconds*30;

NSMutableArray *timesm=[[NSMutableArray alloc]init];
for (int i=0; i<frames; i++) {
    CMTime firstThird = CMTimeMakeWithSeconds(videoDurationSeconds *i/(float)frames, 600);

    [timesm addObject:[NSValue valueWithCMTime:firstThird]];

}


[generate1 generateCGImagesAsynchronouslyForTimes:timesm
                                completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {

    UIImage* myImage = [[UIImage alloc] initWithCGImage:image];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"ankur-%@ %i.jpg",image,(int)[[NSDate date] timeIntervalSince1970]]];
    NSData *imageData = UIImageJPEGRepresentation(myImage,0.7);
    [imageData writeToFile:savedImagePath atomically:NO];


    NSString *requestedTimeString = (NSString *)

    CFBridgingRelease(CMTimeCopyDescription(NULL, requestedTime));

    NSString *actualTimeString = (NSString *)

    CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));

    NSLog(@"Requested: %@; actual %@", requestedTimeString, actualTimeString);

    if (result == AVAssetImageGeneratorSucceeded) {

        // Do something interesting with the image.
    }


    if (result == AVAssetImageGeneratorFailed) {

        NSLog(@"Failed with error: %@", [error localizedDescription]);
    }

    if (result == AVAssetImageGeneratorCancelled) {

        NSLog(@"Canceled");

   }

}];

1 个答案:

答案 0 :(得分:0)

我已经使用您的代码进行了测试。并且通过两次更改,似乎所有帧图像都已成功生成。

1)指定公差选项

generate1.requestedTimeToleranceAfter = CMTimeMakeWithSeconds(1/30.0, videoDuration.timescale);
generate1.requestedTimeToleranceBefore = CMTimeMakeWithSeconds(1/30.0, videoDuration.timescale);

2)使用原生CMTime单位,而不是第二单位

NSMutableArray *timesm=[[NSMutableArray alloc]init];
for (int i=0; i<frames; i++) {
    CMTime firstThird = CMTimeMake( i * (videoDuration.timescale / 30.0f), videoDuration.timescale);
    [timesm addObject:[NSValue valueWithCMTime:firstThird]];
}