【问题标题】:Not able to Extract each and every frame from Video无法从视频中提取每一帧
【发布时间】:2014-09-12 21:22:12
【问题描述】:

我有一个 30fps 的 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");

   }

}];

【问题讨论】:

    标签: objective-c ios7 mpmovieplayercontroller avaudioplayer avassetimagegenerator


    【解决方案1】:

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

    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]];
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多