媒体转码切片_视频切片上传与转码

本文详细介绍了如何进行视频切片上传和转码。首先,通过获取视频总片数并读取每一片数据,然后逐片上传。接着,使用AVFoundation框架将MOV格式视频转换为MP4。最后,提供了获取视频某一帧图片的方法。
摘要由CSDN通过智能技术生成

一、视频切片上传

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];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值