一、视频切片上传
1、获取视频数据的总片数
NSURL *fileURL = [NSURL fileURLWithPath:_mp4FilePath];
NSInteger fileSize = [NSData dataWithContentsOfURL:fileURL].length;
// 总片数的获取方法:
NSInteger chunks = (_fileSize%1024==0)?((int)(_fileSize/1024*1024)):((int)(_fileSize/(1024*1024) + 1));
2、获取每一片视频数据
-(NSData *)readDataWithChunk:(NSInteger)chunk{
// 将文件分片,读取每一片的数据:
NSData* data;
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:_mp4FilePath];
int offset =1024*1024;//(每一片的大小是1M)
[readHandle seekToFileOffset:offset * chunk];
data = [readHandle readDataOfLength:offset];
return data;
}
3、上传每一片的数据
-(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(@"上传成功!");
});
}
二、视频转码
-(void)movFileTransformToMP4WithSourceUrl:(NSURL *)sourceUrl completion:(void(^)(NSString *Mp4FilePath))comepleteBlock
{
/**
* mov格式转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(@"视频格式转换出错Unknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"视频格式转换出错Waiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"视频格式转换出错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(@"视频格式转换出错Unknown");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"视频格式转换出错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];
}