1.8秒のビデオから静止画像を生成するための正確なcmtimeを取得できません
-
26-10-2019 - |
質問
ビデオ資産からまだフレームを生成しようとするたびに、0.000の時点で生成されます。秒。ログメッセージからこれを見ることができます。良いことは、時刻0.000で画像を取得できることです。問題は、avurlassetpreferprecisedurationandtimingkeyが設定されていないことだと思いましたが、それを行う方法を見つけた後でも、それはまだ機能しません。
これが私が持っているものです。
時間、実際の時間、および生成はヘッダーで宣言されます
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));
そして私のコンソールは読みます。
2011-04-28 18:49:59.062 VideoTest [26553:207]合計ビデオ期間は1.880000です
2011-04-28 18:49:59.064 VideoTest [26553:207]私の画像が欲しい時間は1.000000です
2011-04-28 18:49:59.064 VideoTest [26553:207]私の画像が取った実際の時間は0.000000でした
..........................
よろしくお願いします。:)
解決
これを解決するには、設定するだけです requestedtimetolerancebefore と Requestedtimetolerancefter avassetimagegeneratorのkcmtimezeroへ。
他のヒント
昨夜遅く、私はアイデアを持っていました、そして、それが今朝うまくいくと確信しています。基本的に、新しい構成資産を作成するだけで、1秒あたり24フレームでビデオの1つのフレームを表すタイム範囲を作成します。これらの構成を作成したら、各コンプの最初のフレームをつかみます。フレームごとにこれを行い、すべてのフレームを保持する配列を作成します。これが私がしたことです。
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]);
その後、コンソールが読み取ります。
2011-04-29 10:42:24.292 VideoTest [29019:207]このビデオは45.120000フレームで計算されています。
2011-04-29 10:42:24.293 VideoTest [29019:207]合計45フレームを作成しました!!