UIImagePickerController 视频录制操作,视频大小,时间长度
一:使用 iOS 系统 UIImagePickerController
- 获取视频大小
- 获取视频长度
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"AVVideo Demo"; CGRect theFrame = CGRectMake(10, 15, 130, 20); [self addOpBtnWith:@"pick Video" frame:theFrame action:@selector(pickVideoTaped)]; theFrame = CGRectMake(150, 15, 130, 20); [self addOpBtnWith:@"转成mp4" frame:theFrame action:@selector(revoverToMp4)]; } #pragma mark action //转换格式为 mp4 - (void)revoverToMp4 { AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:_videoURL options:nil]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetHighestQuality]; NSDateFormatter* formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; NSString* _mp4Path = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]; exportSession.outputURL = [NSURL fileURLWithPath: _mp4Path]; exportSession.shouldOptimizeForNetworkUse = YES; exportSession.outputFileType = AVFileTypeMPEG4; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch ([exportSession status]) { case AVAssetExportSessionStatusFailed: { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[[exportSession error] localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; break; } case AVAssetExportSessionStatusCancelled: NSLog(@"Export canceled"); break; case AVAssetExportSessionStatusCompleted: NSLog(@"Successful!"); NSLog(@"%@",_mp4Path); NSLog(@"file Size %ldk",(long)[self getFileSize:_mp4Path]); break; default: break; } }]; } else { UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"AVAsset doesn't support mp4 quality" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } } //弹出视频拍摄 - (void)pickVideoTaped { pickVideo = [[UIImagePickerController alloc]init]; pickVideo.sourceType = UIImagePickerControllerSourceTypeCamera; NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; pickVideo.mediaTypes = [NSArray arrayWithObject:availableMedia[1]]; pickVideo.delegate = self; [self presentViewController:pickVideo animated:YES completion:nil]; } #pragma mark - UIImagePickerControllerDelegate //拍照完成代理 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"%@",info); _videoURL = info[UIImagePickerControllerMediaURL]; NSString * size = [NSString stringWithFormat:@"%ld kb", (long)[self getFileSize:[[_videoURL absoluteString] substringFromIndex:16]]]; NSString *length = [NSString stringWithFormat:@"%.0f s", [self getVideoDuration:_videoURL]]; NSLog(@"-------------%@%@",size,length); [picker dismissViewControllerAnimated:YES completion:nil]; } //取消代理 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } //获取视频时间 - (CGFloat) getVideoDuration:(NSURL*) URL { NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:URL options:opts]; float second = 0; second = urlAsset.duration.value/urlAsset.duration.timescale; return second; } //获取视频 大小 - (NSInteger) getFileSize:(NSString*) path { NSFileManager * filemanager = [[NSFileManager alloc]init]; if([filemanager fileExistsAtPath:path]){ NSDictionary * attributes = [filemanager attributesOfItemAtPath:path error:nil]; NSNumber *theFileSize; if ( (theFileSize = [attributes objectForKey:NSFileSize]) ) return [theFileSize intValue]/1024; else return -1; } else { return -1; } } - (void)addOpBtnWith:(NSString *)name frame:(CGRect)frame action:(SEL)action { UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; aBtn.frame = frame; [aBtn setTitle:name forState:UIControlStateNormal]; [aBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:aBtn]; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具