Pregunta

Cada vez que se intenta generar una imagen fija de mi video de activos, se genera en un momento de 0.000..segundos.Puedo ver esto desde mi mensaje de registro.Lo bueno es que puedo meter la imagen en el momento 0.000..para que se muestren en un UIImageView llamado "myImageView." Pensé que el problema era que AVURLAssetPreferPreciseDurationAndtimingkey no se había establecido, pero incluso después de que me di cuenta de cómo hacer eso, todavía no funciona..

Aquí es lo que tengo..

tiempo, actualTime, y generar se declaran en la Cabecera

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));

Y mi Consola lee..

2011-04-28 18:49:59.062 videoTest[26553:207] El total de la Duración del Vídeo es 1.880000

2011-04-28 18:49:59.064 videoTest[26553:207] El tiempo quiero que mi imagen es 1.000000

2011-04-28 18:49:59.064 videoTest[26553:207] El tiempo real de mi imagen era tomar fue 0.000000

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

Gracias chicos tanto de antemano..:)

¿Fue útil?

Solución

Para resolver esto le basta para establecer requestedTimeToleranceBefore y requestedTimeToleranceAfter a kCMTimeZero para AVAssetImageGenerator.

AVAssetImageGenerator De Referencia De La Clase

Otros consejos

Ayer por la noche tuve una Idea y por supuesto que trabajó esta mañana.Esencialmente acabo de crear una nueva Composición de los Activos y, a continuación, crear un intervalo de tiempo que representa un fotograma de Vídeo a 24 fotogramas por segundo.Una vez que tenga estas composiciones creadas, acabo de tomar el primer fotograma de cada comp.Hago esto para cada fotograma y crear una matriz con todas mis cuadros.Aquí es lo que yo hice..

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

A Continuación, La Consola Lee..

2011-04-29 10:42:24.292 videoTest[29019:207] Este vídeo se calcula en 45.120000 Marcos..

2011-04-29 10:42:24.293 videoTest[29019:207] Que hizo un total de 45 Cuadros!!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top