获取视频时长
//计算视频长度 (秒)
/// 网络地址
NSURL *sourceURL = [NSURL URLWithString:urlString];
/// 本地文件
NSURL *sourceURL = [NSURL fileURLWithPath:filePath];
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:path options:opts];
CMTime time = [asset duration];
NSInteger second = ceil(time.value/time.timescale);
通过地址获取头帧图
/// 网络地址
NSURL *sourceURL = [NSURL URLWithString:urlString];
/// 本地文件
NSURL *sourceURL = [NSURL fileURLWithPath:filePath];
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:path options:opts];
NSParameterAssert(asset);//断言
AVAssetImageGenerator *assetImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
NSError *error = nil;
CGImageRef thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:NULL error:&error];
if( error ) {
NSLog(@"%@", error );
}
if(thumbnailImageRef) {
return [[UIImage alloc]initWithCGImage:thumbnailImageRef];
}
return nil;