服务器之家

服务器之家 > 正文

iOS实现视频压缩上传实例代码

时间:2021-03-14 17:08     来源/作者:最爱阴雨天

之前写过图片上传PHP服务器,今天把接口稍微改了一下,把视频上传的代码贴出来,目前上传功能已经调通,视频的压缩代码上似乎并不完善,后续会完善压缩部分的代码;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
- (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(@"沙盒:%@",pathName);
  //转码配置
  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:@"视频压缩失败"];
        [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:@"正在上传"];
          NSLog(@"正在上传%f%%",(1.0*uploadProgress.completedUnitCount/uploadProgress.totalUnitCount)*100);
 
        } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
          [SVProgressHUD showSuccessWithStatus:@"上传成功"];
          [SVProgressHUD dismissWithDelay:1.0];
        } fail:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          [SVProgressHUD showErrorWithStatus:@"上传失败"];
          [SVProgressHUD dismissWithDelay:1.0];
        }];
 
      }
    }
  }];
}

?
1
2
3
4
5
6
7
8
9
[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);
  }];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/ffe0452aed73

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部