1

看起来很简单。我只想要视频 5 秒内的缩略图:

AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:url] options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
Float64 seconds = 5;
int32_t preferredTimeScale = 30;
CMTime actualTime;
CMTime inTime = CMTimeMakeWithSeconds(seconds, preferredTimeScale);
CMTimeShow(inTime);
CGImageRef oneRef = [generate1 copyCGImageAtTime:inTime actualTime:&actualTime error:&err];

输出是这样的:

Printing description of actualTime:
(CMTime) actualTime = 83 1000ths of a second {
  value = 83
  timescale = 1000
  flags = kCMTimeFlags_Valid
  epoch = 0
}
Printing description of inTime:
(CMTime) inTime = 150 30ths of a second {
  value = 150
  timescale = 30
  flags = kCMTimeFlags_Valid
  epoch = 0
}

为什么不actualTime一样inTime???如何在视频中获得 5 秒的图像?

4

1 回答 1

0

尝试使用requestedTimeToleranceBeforerequestedTimeToleranceAfter

generate1.requestedTimeToleranceBefore = kCMTimeZero
generate1.requestedTimeToleranceAfter = kCMTimeZero

var requestedTimeToleranceBefore: CMTime

可以生成图像的请求时间之前的最大时间长度。

var requestedTimeToleranceAfter: CMTime

在请求的时间之后可以生成图像的最大时间长度。

于 2016-09-13T22:34:01.943 回答