美文网首页
iOS获取视频时长和首帧图

iOS获取视频时长和首帧图

作者: HF_K | 来源:发表于2023-02-28 17:40 被阅读0次

获取视频时长

//计算视频长度  (秒)
/// 网络地址
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;

相关文章

网友评论

      本文标题:iOS获取视频时长和首帧图

      本文链接:https://www.haomeiwen.com/subject/jivlldtx.html