我想說這是一篇很全的視頻處理整理寶貝們赤炒,燥熱起來吧氯析。
技術(shù)探索一步一步來,怎么發(fā)現(xiàn)的又是怎么處理的莺褒,繞了多大一個(gè)彎道掩缓,彎道中學(xué)了多少知識(shí)。后續(xù)講一些分片上傳的東西
公司最近要做視頻拍攝及上傳遵岩。
技術(shù)摸索你辣,本以為應(yīng)用拍攝后的數(shù)據(jù)都是保存在相冊(cè)巡通,那么我的第一個(gè)思路就是獲取相冊(cè)資源并解讀其中的數(shù)據(jù)。
【相冊(cè)資源】
值得注意的一個(gè)框架AssetsLibrary.framework這個(gè)framework專門處理相冊(cè)數(shù)據(jù)的
- ALAssetsLibrary:相冊(cè)庫
- ALAssetsGroup:分類組
- ALAsset:每個(gè)元素
上圖是3者互存關(guān)系舍哄,下面上代碼宴凉。
首先是獲取所有ALAssetsGroup
@property (nonatomic,strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic,strong) NSMutableArray *groups;
- (ALAssetsLibrary *)assetsLibrary{
if (_assetsLibrary == nil) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
}
return _assetsLibrary;
}
- (NSMutableArray *)groups{
if (_groups == nil) {
_groups = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(group){
[_groups addObject:group];
[self.tableView reloadData];
}
} failureBlock:^(NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"訪問相冊(cè)失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alertView show];
}];
});
}
return _groups;
}
然后對(duì)ALAssetsGroup進(jìn)行分類展示,自定義GCMAssetModel表悬,這個(gè)類用于保存assets信息弥锄。
- (void)setGroup:(ALAssetsGroup *)group{
_group = group;
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if (asset == nil) return ;
GCMAssetModel *model = [[GCMAssetModel alloc] init];
if (![[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {//不是圖片
model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
model.imageURL = asset.defaultRepresentation.url;
model.isImage = NO;
[self.assetModels addObject:model];
}else{
model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
model.imageURL = asset.defaultRepresentation.url;
NSLog(@"%@",asset.defaultRepresentation.url);
model.isImage = YES;
[self.assetModels addObject:model];
}
}];
}
最后通過ALAssetsLibrary 方法使用assets的url獲取資源信息
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
GCMAssetModel *model = self.assetModels[indexPath.item];
if (model.isImage == NO) {
MPMoviePlayerViewController* playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:model.imageURL];
[self presentViewController:playerView animated:YES completion:nil];
}else{
cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
effectview.frame = cover.frame;
[cover addSubview:effectview];
[self.view addSubview:cover];
bigImg = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
[bigImg addTarget:self action:@selector(removeBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bigImg];
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL:model.imageURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef imgRef = [assetRep fullResolutionImage];
UIImage *img = [UIImage imageWithCGImage:imgRef
scale:assetRep.scale
orientation:(UIImageOrientation)assetRep.orientation];
[bigImg setImage:img forState:UIControlStateNormal];
} failureBlock:^(NSError *error) {
NSLog(@"相冊(cè)圖片訪問失敗");
}];
}
}
【沙盒資源】
之后老板的需求是要通過app拍攝然后保存到本地(也就是沙盒),當(dāng)時(shí)很蒙圈蟆沫,開始查找哪個(gè)函數(shù)能指定存儲(chǔ)位置籽暇,然而我沒有查到,我就自己摸索其他方案饥追,我就每次用我做的app拍攝視頻發(fā)現(xiàn)每次拍攝后我都發(fā)現(xiàn)在tmp中都有臨時(shí)存儲(chǔ)的mov文件图仓。將包下載下來后播放發(fā)現(xiàn)正是我們想要的罐盔。
知道了這個(gè)機(jī)制我就想通過url能否獲取視頻內(nèi)部信息呢但绕,然后我就找到了這個(gè)方法:
-(void)getMovInfoWithAVURLAsset
{
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"1481872850_wm" withExtension:@"MOV"];
AVURLAsset *movAsset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
for (NSString *format in [movAsset availableMetadataFormats]) {
NSLog(@"formatString: %@",format);
for (AVMetadataItem *metadataitem in [movAsset metadataForFormat:format]) {
NSLog(@"commonKey = %@ value = %@",metadataitem.commonKey,metadataitem.value);
if ([metadataitem.commonKey isEqualToString:@"make"]) {
NSString *make = (NSString *)metadataitem.value;
NSLog(@"make: %@",make);
}
else if([metadataitem.commonKey isEqualToString:@"software"])
{
NSString *software = (NSString *)metadataitem.value;
NSLog(@"software: %@",software);
}
else if([metadataitem.commonKey isEqualToString:@"model"])
{
NSString *model = (NSString *)metadataitem.value;
NSLog(@"model: %@",model);
}
else if([metadataitem.commonKey isEqualToString:@"creationDate"])
{
NSString *creationDate = (NSString *)metadataitem.value;
NSLog(@"creationDate: %@",creationDate);
}
}
}
CMTime durationTime = movAsset.duration;
CGFloat duration = CMTimeGetSeconds(durationTime);
NSLog(@"總時(shí)間:%f",duration);
// 視頻截圖
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:movAsset];
generator.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0, 30);
NSValue *timeValue = [NSValue valueWithCMTime:time];
[generator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^
(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
{
if (result == AVAssetImageGeneratorSucceeded)
{
self.vedioImage.image = [UIImage imageWithCGImage:image];
// 成功 do something
}
else
{
// 失敗
}
}];
}
AVURLAsset 需要AVFoundation.framework妨蛹,這樣數(shù)據(jù)就全了症副。
推薦幾個(gè)文章。
http://blog.csdn.net/b719426297/article/details/24312339
http://blog.csdn.net/newjerryj/article/details/7637047
http://blog.csdn.net/u011397277/article/details/52574996
http://www.reibang.com/p/931f8e85dc37