博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIImagePickerController 视频录制操作,视频大小,时间长度
阅读量:6854 次
发布时间:2019-06-26

本文共 5547 字,大约阅读时间需要 18 分钟。

一:使用 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];}

 

转载于:https://www.cnblogs.com/cocoajin/p/3494290.html

你可能感兴趣的文章
ZFS调试命令zdb之进阶用法
查看>>
集群管理平台安全问题
查看>>
新注册的微信公众号如何获得原始粉丝?
查看>>
Hyper-V Server Replica
查看>>
java实现顺序链表
查看>>
负载均衡产品的NAT转换技术及IP溯源的实现
查看>>
脉冲耦合神经网络(PCNN)的matlab实现
查看>>
NLTK基础教程学习笔记(五)
查看>>
一小段有用脚本,仅记录
查看>>
Cacti合并流量图
查看>>
split分割合并及验证文件的一致性
查看>>
输入gpedit.msc时提示找不到文件这是什么原因
查看>>
Hadoop 常用端口说明
查看>>
技术分享连载(三十三)
查看>>
Oracle Linux 6.5安装Oracle 11.2.0.4 x64
查看>>
获得Microsoft MVP后的感受!!!!
查看>>
Nginx 教程 #3:SSL 设置
查看>>
Exchange Server 2003 规划与部署
查看>>
struts2中类型转换器的使用
查看>>
三星正式超越Intel成为了芯片行业的老大
查看>>