Frage

Jedes Mal, wenn ich versuche, einen Standbild aus meinem Video -Asset zu generieren, wird er zu einer Zeit von 0,000 erzeugt. Sekunden. Ich kann das aus meiner Protokollnachricht sehen. Das Gute ist, dass ich das Bild zum Zeitpunkt 0,000 erhalten kann. Ich dachte, das Problem war, dass AvurlassetPreferpreciseduration und TimingKey nicht festgelegt wurde, aber selbst nachdem ich herausgefunden habe, wie es geht, funktioniert es immer noch nicht.

Hier ist was ich habe ..

Zeit, tatsächliche Zeit und Erzeugung werden im Header deklariert

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

Und meine Konsole liest ..

2011-04-28 18: 49: 59.062 Videoband [26553: 207] Die gesamte Videodauer beträgt 1,880000

2011-04-28 18: 49: 59.064 VideoStest [26553: 207] Die Zeit, die ich möchte, dass mein Bild 1.000000 ist

2011-04-28 18: 49: 59.064 Videoband [26553: 207] Die tatsächliche Zeit war mein Bild auf 0,000000

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

Vielen Dank im Voraus .. :)

War es hilfreich?

Lösung

Um dies zu beheben, müssen Sie nur festlegen beantragte Timetolerance vorne und beantragte Timetoleranz nach kcmtimeZero für avassetimageGenerator.

AvassetimageGenerator -Klasse Referenz

Andere Tipps

Spät in der letzten Nacht hatte ich eine Idee und sicher genug hat es heute Morgen funktioniert. Im Wesentlichen erstelle ich nur einen neuen Kompositions -Asset und erstelle dann einen Zeitbereich, der einen Rahmen für Video mit 24 Bildern pro Sekunde darstellt. Sobald ich diese Kompositionen erstellt habe, greife ich einfach den ersten Rahmen jedes Comps. Ich mache das für jeden Frame und erstelle ein Array, das alle meine Rahmen hält. Hier ist was ich getan habe ..

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

Dann liest die Konsole ..

2011-04-29 10: 42: 24.292 VideoStest [29019: 207] Dieses Video wird bei 45.120000-Bildern berechnet.

2011-04-29 10: 42: 24.293 VideoStest [29019: 207] Sie haben insgesamt 45 Bilder gemacht !!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top