美文网首页
视频压缩与 上传

视频压缩与 上传

作者: 爱喝农药de清凉 | 来源:发表于2017-10-18 15:54 被阅读11次

压缩:

    // 获取视频文件的url
   NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
    AVAsset * asset = [AVAsset assetWithURL:mediaURL];
    //设置压缩质量
    AVAssetExportSession * session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
    //优化网络
    session.shouldOptimizeForNetworkUse = YES;
    NSString* path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]  stringByAppendingPathComponent:@"hello.mp4"];
    [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
     //设置输出路径
    session.outputURL = [NSURL fileURLWithPath:path];
      //设置输出类型 这里可以更改输出的类型 具体可以看文档描述
    session.outputFileType = AVFileTypeMPEG4;
    [session exportAsynchronouslyWithCompletionHandler:^{
        
        if (session.status == AVAssetExportSessionStatusCompleted) {
            //压缩完成
            dispatch_async(dispatch_get_main_queue(), ^{
                
           NSURL * CompressURL = session.outputURL;
                
                _videoImageView.image = [JKVideoAudioTool getThumImageForVideoUrl:mediaURL atTime:0];
                //               NSString * str = [NSString stringWithContentsOfURL:mediaURL encoding:kCFStringEncodingUTF8 error:nil];
                NSLog(@"*********** url :%@",CompressURL);
                [self.dataSource removeAllObjects];
                [self.dataSource addObject:CompressURL];
                
            });
        }
        
        
    }];
    
}

上传:传data 设置格式

                NSData *data = [NSData dataWithContentsOfURL:img];
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                formatter.dateFormat = @"yyyyMMddHHmmss";
                NSString *str = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
                
                [formData appendPartWithFileData:data name:photoKey fileName:fileName mimeType:@"multipart/form-data"];

相关文章

网友评论

      本文标题:视频压缩与 上传

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