iOS-SDAVAssetExportSession 的使用

-(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;
                           });
                       }
                   }];
    }];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芦缰,一起剝皮案震驚了整個(gè)濱河市谨敛,隨后出現(xiàn)的幾起案子啰挪,更是在濱河造成了極大的恐慌侵佃,老刑警劉巖勃黍,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件块请,死亡現(xiàn)場(chǎng)離奇詭異娜氏,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)墩新,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)贸弥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人海渊,你說(shuō)我怎么就攤上這事绵疲。” “怎么了臣疑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵盔憨,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我讯沈,道長(zhǎng)郁岩,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮问慎,結(jié)果婚禮上萍摊,老公的妹妹穿的比我還像新娘。我一直安慰自己蝴乔,他們只是感情好记餐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布驮樊。 她就那樣靜靜地躺著薇正,像睡著了一般。 火紅的嫁衣襯著肌膚如雪囚衔。 梳的紋絲不亂的頭發(fā)上挖腰,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音练湿,去河邊找鬼猴仑。 笑死,一個(gè)胖子當(dāng)著我的面吹牛肥哎,可吹牛的內(nèi)容都是我干的辽俗。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼篡诽,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼崖飘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起杈女,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤朱浴,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后达椰,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體翰蠢,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年啰劲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了梁沧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蝇裤,死狀恐怖廷支,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情猖辫,我是刑警寧澤酥泞,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站啃憎,受9級(jí)特大地震影響芝囤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一悯姊、第九天 我趴在偏房一處隱蔽的房頂上張望羡藐。 院中可真熱鬧,春花似錦悯许、人聲如沸仆嗦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)垃僚。三九已至,卻和暖如春谆棺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背碍岔。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工朵夏, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人询吴。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓亮元,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爆捞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子煮甥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容