iOS Implementation Video Compression Upload Example Code

  • 2021-12-13 17:22:22
  • OfStack

I wrote the picture upload PHP server before. Today, I changed the interface slightly and posted the video upload code. At present, the upload function has been switched on, and the video compression code seems to be imperfect. The code of the compression part will be improved later;


- (void)convertVideoWithURL:(NSURL *)url
{
  NSDate *date = [NSDate date];
  NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
  [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
  NSString *dateName = [dateformatter stringFromDate:date];
  NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
  NSString *pathName = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",dateName]];
  NSLog(@" Sandbox: %@",pathName);
  // Transcoding configuration 
  AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
  exportSession.shouldOptimizeForNetworkUse = YES;
  exportSession.outputURL = [NSURL fileURLWithPath:pathName];
  exportSession.outputFileType = AVFileTypeMPEG4;
  [exportSession exportAsynchronouslyWithCompletionHandler:^{
    int exportStatus = exportSession.status;
    switch (exportStatus)     {
      case AVAssetExportSessionStatusFailed:
      {
        // log error to text view
        NSError *exportError = exportSession.error;
        NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
        [SVProgressHUD showErrorWithStatus:@" Video compression failed "];
        [SVProgressHUD dismissWithDelay:1.0];
        break;
      }
      case AVAssetExportSessionStatusCompleted:
      {
        self.videoData = [NSData dataWithContentsOfFile:pathName];

        [[NetTool shareDL]upLoadVideoWithURL:@"http://192.168.1.102/php/image.php" paremeter:nil data:self.videoData videoName:[NSString stringWithFormat:@"%@.mp4",dateName] progress:^(NSProgress * _Nonnull uploadProgress) {

          [SVProgressHUD showProgress:1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount status:@" Uploading "];
          NSLog(@" Uploading %f%%",(1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount)*100);

        } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
          [SVProgressHUD showSuccessWithStatus:@" Upload succeeded "];
          [SVProgressHUD dismissWithDelay:1.0];
        } fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          [SVProgressHUD showErrorWithStatus:@" Upload failed "];
          [SVProgressHUD dismissWithDelay:1.0];
        }];

      }
    }
  }];
}


[manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
    [formData appendPartWithFileData:videoData name:@"upimage" fileName:videoName mimeType:@"video/mp4"];
  } progress:^(NSProgress * _Nonnull uploadProgress) {
    progress(uploadProgress);
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    success(task,responseObject);
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    fail(task,error);
  }];

Related articles: