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

推荐阅读更多精彩内容