美文网首页
ios录制视频并上传

ios录制视频并上传

作者: Ayaoguai | 来源:发表于2017-06-19 16:03 被阅读249次

    在网上摘录的片段整理

    //选择本地视频 
    - (void)chooseLocalVideo 
    { 
     UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; 
     ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//sourcetype有三种分别是camera,photoLibrary和photoAlbum 
     NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie" 
     ipc.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie 
     [self presentViewController:ipc animated:YES completion:nil]; 
     ipc.delegate = self;//设置委托 
    } 
    //录制视频 
    - (void)startTranscribeVideo 
    { 
     UIImagePickerController *ipc = [[UIImagePickerController alloc] init]; 
     ipc.sourceType = UIImagePickerControllerSourceTypeCamera;//sourcetype有三种分别是camera,photoLibrary和photoAlbum 
     NSArray *availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];//Camera所支持的Media格式都有哪些,共有两个分别是@"public.image",@"public.movie" 
     ipc.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];//设置媒体类型为public.movie 
     [self presentViewController:ipc animated:YES completion:nil]; 
     ipc.videoMaximumDuration = 30.0f;//30秒 
     ipc.delegate = self;//设置委托 
    }
    

    获取视频的大小和时长

    - (CGFloat) getFileSize:(NSString *)path 
    { 
     NSLog(@"%@",path); 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     float filesize = -1.0; 
     if ([fileManager fileExistsAtPath:path]) { 
      NSDictionary *fileDic = [fileManager attributesOfItemAtPath:path error:nil];//获取文件的属性 
      unsigned long long size = [[fileDic objectForKey:NSFileSize] longLongValue]; 
      filesize = 1.0*size/1024; 
     }else{ 
      NSLog(@"找不到文件"); 
     } 
     return filesize; 
    }//此方法可以获取文件的大小,返回的是单位是KB。 
    - (CGFloat) getVideoLength:(NSURL *)URL 
    { 
     AVURLAsset *avUrl = [AVURLAsset assetWithURL:URL]; 
     CMTime time = [avUrl duration]; 
     int second = ceil(time.value/time.timescale); 
     return second; 
    }//此方法可以获取视频文件的时长。
    

    视频的转码和压缩

    //完成视频录制,并压缩后显示大小、时长 
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    { 
     NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL]; 
     NSLog(@"%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:sourceURL]]); 
     NSLog(@"%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[sourceURL path]]]); 
     NSURL *newVideoUrl ; //一般.mp4 
     NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可 
     [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; 
     newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;//这个是保存在app自己的沙盒路径里,后面可以选择是否在上传后删除掉。我建议删除掉,免得占空间。 
     [picker dismissViewControllerAnimated:YES completion:nil]; 
     [self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil]; 
    } 
    - (void) convertVideoQuailtyWithInputURL:(NSURL*)inputURL 
            outputURL:(NSURL*)outputURL 
           completeHandler:(void (^)(AVAssetExportSession*))handler 
    { 
    //presetName 几种格式
    //AVAssetExportPresetLowQuality,
    //AVAssetExportPreset960x540,
    //AVAssetExportPreset640x480,
    //AVAssetExportPresetMediumQuality,
    //AVAssetExportPreset1920x1080,
    //AVAssetExportPreset1280x720,
    //AVAssetExportPresetHighestQuality,
    //AVAssetExportPresetAppleM4A
     AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; 
      AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality]; 
      // NSLog(resultPath); 
      exportSession.outputURL = outputURL; 
      exportSession.outputFileType = AVFileTypeMPEG4; //转换的格式
      exportSession.shouldOptimizeForNetworkUse= YES; 
      [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
       { 
        switch (exportSession.status) { 
         case AVAssetExportSessionStatusCompleted: 
          NSLog(@"AVAssetExportSessionStatusCompleted"); 
          NSLog(@"%@",[NSString stringWithFormat:@"%f s", [self getVideoLength:outputURL]]); 
          NSLog(@"%@", [NSString stringWithFormat:@"%.2f kb", [self getFileSize:[outputURL path]]]); 
          //UISaveVideoAtPathToSavedPhotosAlbum([outputURL path], self, nil, NULL);//这个是保存到手机相册 
          NSLog(@"这地方写你的上传视频或者再次判断视频的大小等等"); 
          break; 
         case AVAssetExportSessionStatusFailed: 
          NSLog(@"AVAssetExportSessionStatusFailed"); 
          break; 
        } 
       }]; 
    }
    

    相关文章

      网友评论

          本文标题:ios录制视频并上传

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