一筛峭、視頻切片上傳
1宛徊、獲取視頻數(shù)據(jù)的總片數(shù)
NSURL *fileURL = [NSURL fileURLWithPath:_mp4FilePath];
NSInteger fileSize = [NSData dataWithContentsOfURL:fileURL].length;
// 總片數(shù)的獲取方法:
NSInteger chunks = (_fileSize%1024==0)?((int)(_fileSize/1024*1024)):((int)(_fileSize/(1024*1024) + 1));
2崔梗、獲取每一片視頻數(shù)據(jù)
-(NSData *)readDataWithChunk:(NSInteger)chunk{
// 將文件分片夜只,讀取每一片的數(shù)據(jù):
NSData* data;
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:_mp4FilePath];
int offset =1024*1024;//(每一片的大小是1M)
[readHandle seekToFileOffset:offset * chunk];
data = [readHandle readDataOfLength:offset];
return data;
}
3、上傳每一片的數(shù)據(jù)
-(void)uploadData{
dispatch_group_t group = dispatch_group_create();
NSInteger chunk = 0;
for (NSData *data in self.fileArr) {
if ([self.fileArr[chunk] isKindOfClass:[NSData class]]) {
dispatch_group_enter(group);
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:_randomStr forKey:@"r"];
[params setObject:@(_chunks) forKey:@"chunks"];
[params setObject:@(chunk) forKey:@"chunk"];
[XYNetworking uploadWithURL:@"oUpload.php" params:params fileData:data name:@"file" fileName:_fileName mimeType:@"video/*" success:^(id responseObject) {
dispatch_group_leave(group);
[self.fileArr replaceObjectAtIndex:chunk withObject:@"finish"];
} failure:^(NSError *error) {
dispatch_group_leave(group);
}];
}
chunk++;
}
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"上傳成功!");
});
}
二蒜魄、視頻轉(zhuǎn)碼
-(void)movFileTransformToMP4WithSourceUrl:(NSURL *)sourceUrl completion:(void(^)(NSString *Mp4FilePath))comepleteBlock
{
/**
* mov格式轉(zhuǎn)mp4格式
*/
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceUrl options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
NSLog(@"%@",compatiblePresets);
if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *uniqueName = [NSString stringWithFormat:@"%@.mp4",[formatter stringFromDate:date]];
NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString * resultPath = [document stringByAppendingPathComponent:uniqueName];//PATH_OF_DOCUMENT為documents路徑
NSLog(@"output File Path : %@",resultPath);
exportSession.outputURL = [NSURL fileURLWithPath:resultPath];
exportSession.outputFileType = AVFileTypeMPEG4;//可以配置多種輸出文件格式
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
switch (exportSession.status) {
case AVAssetExportSessionStatusUnknown:
NSLog(@"視頻格式轉(zhuǎn)換出錯Unknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"視頻格式轉(zhuǎn)換出錯Waiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"視頻格式轉(zhuǎn)換出錯Exporting");
break;
case AVAssetExportSessionStatusCompleted:
{
NSLog(@"AVAssetExportSessionStatusCompleted");
NSLog(@"mp4 file size:%lf MB",[NSData dataWithContentsOfURL:exportSession.outputURL].length/1024.f/1024.f);
comepleteBlock(resultPath);
}
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"視頻格式轉(zhuǎn)換出錯Unknown");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"視頻格式轉(zhuǎn)換出錯Cancelled");
break;
}
}];
}
}
三扔亥、獲取視頻某一幀的圖片
/**
獲取視頻縮略圖
*/
+ (UIImage *)get_videoThumbImage:(NSURL *)videoURL
{
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:videoURL options:opts];
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
generator.appliesPreferredTrackTransform = YES;
CMTime actualTime;
NSError *error = nil;
CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(0, 600) actualTime:&actualTime error:&error];
if (error) {
return nil;
}
return [UIImage imageWithCGImage:img];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者