-(void)actionWithAsset:(PHAsset *)asset coverImage:(UIImage *)coverImage{
WS(weakSelf);
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
// NSURL *outputUrl = [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",movName]]]; //輸出路徑
//保存至沙盒路徑
NSString *FileName = [NSString stringWithFormat:@"%@.mp4",[DataFoundation ret32bitString]];
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoPath = [NSString stringWithFormat:@"%@", pathDocuments];
NSString *FileLocalUrl = [videoPath stringByAppendingPathComponent:FileName];
NSURL *outputUrl = [NSURL fileURLWithPath:FileLocalUrl];
SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:asset];
encoder.outputFileType = AVFileTypeMPEG4;
encoder.outputURL = outputUrl;
//視頻設(shè)置
encoder.videoSettings = @
{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: @(coverImage.size.width),
AVVideoHeightKey: @(coverImage.size.height),
AVVideoCompressionPropertiesKey: @
{
AVVideoAverageBitRateKey: @6000000,
AVVideoProfileLevelKey: AVVideoProfileLevelH264High40,
},
};
//音頻設(shè)置
encoder.audioSettings = @
{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: @2,
AVSampleRateKey: @44100,
AVEncoderBitRateKey: @128000,
};
__weak typeof(self) weakSelf = self;
[encoder exportAsynchronouslyWithCompletionHandler:^
{
if (encoder.status == AVAssetExportSessionStatusCompleted)
{
// NSData *outputData = [NSData dataWithContentsOfURL:encoder.outputURL]; //壓縮后的視頻
// [weakSelf saveAtta:outputData withName:movName toPath:USER_Chat_Folder_Path];//保存
CGFloat MaxVideoLength = ([weakSelf.ugcRecordDic getFloatValueForKey:@"MaxVideoLength" defaultValue:0.0] / 1024.00);// 視頻最大大小
long fileSize = fileSizeAtPath(FileLocalUrl);
if (fileSize / 1024.00 / 1024.00 > MaxVideoLength) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:weakSelf.view];
MakeOneCacleBtnAlert([[UIApplication sharedApplication].delegate window].rootViewController, @"提醒", [NSString stringWithFormat:@"添加失敗,視頻壓縮后需限制在%@M以內(nèi)!",formatFloat(MaxVideoLength)], @"確定");
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
});
return ;
}
NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:FileLocalUrl error:&error];
NSString *time;
if (fileAttributes) {
if (fileAttributes != nil) {
NSDate *fileModDate;
//文件修改日期
if ((fileModDate = [fileAttributes objectForKey:NSFileModificationDate])) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSString *currentDateString = [dateFormatter stringFromDate:fileModDate];
if (currentDateString != nil && currentDateString.length >= 20) {
time = [DateFoundation formateDate:[currentDateString substringToIndex:20] withFormate:@"yyyy-MM-dd HH:mm:ss"];
//NSLog(@"Modification date: %@\n currentDateString -- %@ \n time -- %@\n ", fileModDate,currentDateString,time);
}
}
}
}
// dispatch_async(dispatch_get_main_queue(), ^{
// [MBProgressHUD hideHUDForView:weakSelf.view];
// });
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"name":FileName?FileName:@"",
@"path":FileLocalUrl?FileLocalUrl:@"",
@"size":sizeOfFile(fileSize),
@"time":time?time:@"",
@"type":@"1"
}];
LocalVedioModel *localVedioModel = [LocalVedioModel mj_objectWithKeyValues:dic];
localVedioModel.coverImage = coverImage;
[weakSelf localVedioMLinkChangeResult:localVedioModel];
// dispatch_async(dispatch_get_main_queue(), ^{
// [weakSelf noStudent];
// [weakSelf.tableV reloadData];
// });
// // NSData *data = [NSData dataWithContentsOfFile:model.FileLocalUrl];
// //model.fileData = data;
}
else if (encoder.status == AVAssetExportSessionStatusCancelled)
{
NSLog(@"Video export cancelled");
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:weakSelf.view];
[MBProgressHUD showMoreLine:@"添加取消" view:nil];
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
});
}
else
{
NSLog(@"Video export failed with error: %@ (%ld)", encoder.error.localizedDescription, (long)encoder.error.code);
// log error to text view
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:weakSelf.view];
[MBProgressHUD showMoreLine:@"添加失敗" view:nil];
weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
});
}
}];
}];
}
iOS-SDAVAssetExportSession 的使用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)贸弥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人海渊,你說(shuō)我怎么就攤上這事绵疲。” “怎么了臣疑?”我有些...
- 文/不壞的土叔 我叫張陵盔憨,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我讯沈,道長(zhǎng)郁岩,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮问慎,結(jié)果婚禮上萍摊,老公的妹妹穿的比我還像新娘。我一直安慰自己蝴乔,他們只是感情好记餐,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布驮樊。 她就那樣靜靜地躺著薇正,像睡著了一般。 火紅的嫁衣襯著肌膚如雪囚衔。 梳的紋絲不亂的頭發(fā)上挖腰,一...
- 那天,我揣著相機(jī)與錄音练湿,去河邊找鬼猴仑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛肥哎,可吹牛的內(nèi)容都是我干的辽俗。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼篡诽,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼崖飘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起杈女,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤朱浴,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后达椰,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體翰蠢,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年啰劲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了梁沧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站啃憎,受9級(jí)特大地震影響芝囤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一悯姊、第九天 我趴在偏房一處隱蔽的房頂上張望羡藐。 院中可真熱鬧,春花似錦悯许、人聲如沸仆嗦。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)垃僚。三九已至,卻和暖如春谆棺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背碍岔。 一陣腳步聲響...
- 正文 我出身青樓亮元,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爆捞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子煮甥,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- CoreEditText github地址:https://github.com/muyusunny/CoreEd...
- 在開(kāi)發(fā)中我們會(huì)經(jīng)常使用到block劲妙,而使用block會(huì)經(jīng)常遇到循環(huán)引用的問(wèn)題,為了解決循環(huán)引用的問(wèn)題我們需要使用w...
- Web image(網(wǎng)絡(luò)圖像) 該庫(kù)提供了一個(gè)支持來(lái)自Web的遠(yuǎn)程圖像的UIImageView類別它提供了: 添加...
- iOS 中禁止使用某個(gè)方法的時(shí)候,我們常常會(huì)選擇重寫(xiě)方法澄惊。例如: 這樣重寫(xiě)方法富雅,還是能調(diào)用~ 在一些iOS文件中經(jīng)...
- 用 copy 修飾的 NSString 如果在無(wú)意中被一個(gè) NSMutableString 類型的變量賦值没佑,原先的...