iOS 视频处理(获取第一帧图片/视频时长...)

1.获取第一帧图片

NSLog(@"beng......");

// 本地视频
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"开屏视频.mp4" ofType:nil];
//    NSURL *url = [NSURL fileURLWithPath:path];

// 网络视频
NSString *path = @"http://aliyun.app.video.3dov.cn/Act-ss-mp4-hd/67975D3EC0663C70042D9A725167C244.mp4";
NSURL *url = [NSURL URLWithString:path];

// 获取第一帧图片
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
CMTime time = CMTimeMake(1, 2);
CGImageRef oneRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
UIImage *oneImg = [[UIImage alloc] initWithCGImage:oneRef];


// 显示图片
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = oneImg;
imageView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:imageView];
NSLog(@"end......");
第一帧图片.png

2.视频时长

NSLog(@"beng......");

// 本地视频
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"开屏视频.mp4" ofType:nil];
//    NSURL *url = [NSURL fileURLWithPath:path];

// 网络视频
NSString *path = @"http://aliyun.app.video.3dov.cn/Act-ss-mp4-hd/67975D3EC0663C70042D9A725167C244.mp4";
NSURL *url = [NSURL URLWithString:path];

AVURLAsset *asset = [AVURLAsset assetWithURL:url];
CMTime time = [asset duration];
int seconds = ceil(time.value / time.timescale);

NSLog(@"视频时长: %d", seconds);

NSLog(@"end......");

// 结果: 视频时长: 88

你可能感兴趣的:(iOS 视频处理(获取第一帧图片/视频时长...))