在最近的項目中缨伊,涉及到視頻上傳进宝,在網(wǎng)上找了下資料党晋,這里整理下來希望對大家有幫助,這里簡短談下整個流程未玻;
1. 創(chuàng)建保存該視頻的文件夾
- (void)createVideoFolderIfNotExist{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *folderPath = [path stringByAppendingPathComponent:videoFolders];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL isDirExist = [fileManager fileExistsAtPath:folderPath isDirectory:&isDir];
if(!(isDirExist && isDir)) {
BOOL create = [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
if(!create){
NSLog(@"創(chuàng)建保存視頻文件夾失敗");
}
}
}
2. 視頻錄制完以后保存的視頻
- (NSString *)getVideoSaveFilePathString{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:videoFolders];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *nowTimeStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
NSString *fileName = [[path stringByAppendingPathComponent:nowTimeStr] stringByAppendingString:@".mov"];
return fileName;
}
3. 根據(jù)以上兩個方法,最后合成為 mp4
- (NSString *)getVideoMergeFilePathString{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:videoFolders];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *nowTimeStr = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
NSString *fileName = [[path stringByAppendingPathComponent:nowTimeStr] stringByAppendingString:@"merge.mp4"];
return fileName;
}
主要的代碼
//根據(jù)設備輸出獲得連接
AVCaptureConnection *captureConnection=[self.captureMovieFileOutput connectionWithMediaType:AVMediaTypeVideo];
//根據(jù)連接取得設備輸出的數(shù)據(jù)
if (![self.captureMovieFileOutput isRecording]) {
// shootBt.backgroundColor = UIColorFromRGB(0xfa5f66);
//預覽圖層和視頻方向保持一致
captureConnection.videoOrientation=[self.captureVideoPreviewLayer connection].videoOrientation;
[self.captureMovieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:[self getVideoSaveFilePathString]] recordingDelegate:self];
#視頻壓縮
NSString *path = [self getVideoMergeFilePathString];
NSURL *mergeFileURL = [NSURL fileURLWithPath:path];
AVMutableVideoCompositionInstruction *mainInstruciton = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruciton.timeRange = CMTimeRangeMake(kCMTimeZero, totalDuration);
mainInstruciton.layerInstructions = layerInstructionArray;
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = @[mainInstruciton];
mainCompositionInst.frameDuration = CMTimeMake(1, 100);
mainCompositionInst.renderSize = CGSizeMake(renderW, renderW*preLayerHWRate);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exporter.videoComposition = mainCompositionInst;
exporter.outputURL = mergeFileURL;
exporter.outputFileType = AVFileTypeMPEG4; //mp4
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
if ([exporter status] == AVAssetExportSessionStatusCompleted) {
dispatch_async(dispatch_get_main_queue(), ^{
################# 這里的二進制就是上傳的東西了##################
NSData *data = [NSData dataWithContentsOfURL:exporter.outputURL];
}];
上傳的方法
NSString *imageStr = @"公司接口";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[manager POST:imageStr parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
/*
此方法參數(shù)
1. 要上傳的[二進制數(shù)據(jù)]
2. 對應網(wǎng)站上[upload.php中]處理文件的[字段"file"]
3. 要保存在服務器上的[文件名]
4. 上傳文件的[mimeType]
*/
[formData appendPartWithFileData:datas name:@"video" fileName:@"videosNames" mimeType:@"video/mp4"];
// [formData appendPartWithFormData:datas name:@"video"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *cashDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",cashDic[@"remark"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"faile ----->%@",error);
}];