Non riesco a trovare un CMTime preciso per Generazione di fermo immagine da 1,8 secondi Video
-
26-10-2019 - |
Domanda
Ogni volta che provo a generare un fermo immagine dal mio risorsa video, si è generato in un momento di 0,000 secondi ... Posso vedere questo dal mio messaggio di log. La cosa buona è che posso ottenere l'immagine al momento 0.000 .. di presentarsi in un UIImageView chiamato "myImageView." Ho pensato che il problema era che AVURLAssetPreferPreciseDurationAndTimingKey non è stato impostato, ma anche dopo che ho capito come fare, lo fa ancora non funziona ..
Ecco quello che ho ..
tempo, ActualTime, e generare sono dichiarati nell'intestazione
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));
E la mia console legge ..
2011-04-28 18: 49: 59,062 VideoTest [26553: 207] La ??Total Video durata è 1,880 mila
2011-04-28 18: 49: 59,064 VideoTest [26553: 207] Il tempo che voglio la mia immagine è 1.000000
2011-04-28 18: 49: 59,064 VideoTest [26553: 207] Il tempo effettivo mia immagine è stata presa era 0.000000
..........................
Grazie ragazzi così tanto in anticipo ..:)
Soluzione
Per risolvere questo è sufficiente set requestedTimeToleranceBefore e requestedTimeToleranceAfter per kCMTimeZero per AVAssetImageGenerator.
Altri suggerimenti
La scorsa notte ho avuto un'idea e abbastanza sicuro ha funzionato questa mattina. In sostanza mi basta creare una nuova composizione dell'attivo e quindi creare un intervallo di tempo che rappresenta un fotogramma per il video a 24 fotogrammi al secondo. Una volta ho queste composizioni create, ho appena afferrare il primo fotogramma di ogni comp. Lo faccio per ogni fotogramma e creare una matrice contenente tutti i miei fotogrammi. Ecco quello che ho fatto ..
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]);
Poi la console legge ..
2011-04-29 10: 42: 24,292 VideoTest [29019: 207] Questo video è calcolato al 45.120000 Frames ..
2011-04-29 10: 42: 24,293 VideoTest [29019: 207] Hai fatto un totale di 45 fotogrammi !!