Question

Chaque fois que j'essaie de générer un cadre toujours de mon actif vidéo, il est généré à un moment de 0.000 .. secondes. Je peux voir cela de mon message de journal. La chose est bon que je peux obtenir l'image au moment de 0.000 .. à apparaître dans un UIImageView appelé « myImageView. » Je pensais que le problème était que AVURLAssetPreferPreciseDurationAndTimingKey n'a pas été réglée, mais même après avoir compris comment le faire, il ne fonctionne toujours pas ..

Voici ce que j'ai ..

temps, actualTime, et générer sont déclarées dans l'en-tête

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/videoTest4.m4v"]];
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
NSURL *url = [NSURL fileURLWithPath:path];

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];

Float64 durationSeconds = CMTimeGetSeconds([asset duration]);

generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = nil;

time = CMTimeMake(600,600.0);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
myImageView.image = currentImg;

NSLog(@"The total Video Duration is %f",durationSeconds);
NSLog(@"The time I want my image is %f",CMTimeGetSeconds(time));
NSLog(@"The actual time my image was take was %f",CMTimeGetSeconds(actualTime));

Et mon lit Console ..

2011-04-28 18: 49: 59,062 VideoTest [26553: 207] La ??durée totale de la vidéo est 1,880000

2011-04-28 18: 49: 59,064 VideoTest [26553: 207] Le temps que je veux que mon image est 1,000000

2011-04-28 18: 49: 59,064 VideoTest [26553: 207] Le temps réel mon image a été prise a été 0,000000

..........................

Merci les gars tellement à l'avance ..:)

Était-ce utile?

La solution

Pour résoudre cela, vous avez juste besoin de jeu requestedTimeToleranceBefore et requestedTimeToleranceAfter à kCMTimeZero pour AVAssetImageGenerator.

AVAssetImageGenerator Référence de la classe

Autres conseils

Tard hier soir, j'ai eu une idée et bien sûr ça a marché ce matin. Essentiellement, je viens de créer une nouvelle composition d'actifs, puis créer une plage de temps qui représente une image pour la vidéo à 24 images par seconde. Une fois que j'ai ces compositions créées, je prends juste la première image de chaque échantillon. Je le fais pour chaque trame et créer un tableau contenant tous mes cadres. Voici ce que je l'ai fait ..

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/videoTest4.m4v"]];
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
NSURL *url = [NSURL fileURLWithPath:path];

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];

Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 24.0;

AVMutableComposition *myComp = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

NSError *error = nil;
BOOL ok = NO;

NSMutableArray* frameArray = [[NSMutableArray alloc] init];

generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
NSError *err = nil;

for (int i = 0; i < floor(durationFrames); i++) {

    CMTime startTime = CMTimeMake(i, 24);
    CMTime endTime = CMTimeMake(i+1, 24);

    CMTimeRange myRange = CMTimeRangeMake(startTime, endTime);

    AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];
    if (!ok) {
        // Deal with the error.
    }

    time = CMTimeMake(0,1);
    CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];
    UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
    [frameArray addObject:currentImg];

    [currentImg release];

}

NSLog(@"This video is calculated at %f Frames..",durationFrames);
NSLog(@"You made a total of %i Frames!!",[frameArray count]);

Ensuite, la console .. Reads

2011-04-29 10: 42: 24,292 VideoTest [29019: 207] Cette vidéo est calculée à 45.120000 .. Pour

2011-04-29 10: 42: 24,293 VideoTest [29019: 207] Vous avez fait un total de 45 images !!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top