iOS获取视频时长和首帧图

获取视频时长

//计算视频长度  (秒)
/// 网络地址
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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容