iOS-阿里云集成上傳視頻的準備工作

首先我在選擇上傳視頻的時候用到的第三方是?TZImagePickerController

這個第三方非常牛 選擇的圖片 還能選擇視頻非常棒 棒棒噠 手動點贊

上傳流程

1.請求上傳地址加憑證或STS油讯,相關(guān)概念請參見相關(guān)文檔。

2.初始化上傳實例填帽,實例化上傳有兩種方式:上傳地址加憑證和STS方式涝焙。

3.回調(diào)設(shè)置,所有的上傳狀態(tài)包括進度,上傳成功,上傳失敗追迟,憑證過期都在這里進行處理。

4.添加上傳文件進入上傳列表伐坏,目前主要支持視頻文件和圖片文件的上傳怔匣。

5.啟動上傳

6.回調(diào)處理

上傳步驟

\bullet ?初始化SDK?明文設(shè)置模式(不推薦)握联,2.STS鑒權(quán)模式(推薦)桦沉,3.自簽名模式

\bullet 上傳方式

VOD(短視頻上傳)每瞒,OSS(文件上傳)

上傳成功的返回數(shù)據(jù)

VOD上傳返回視頻id(字符串),OSS上傳返回文件名(字符串)

?? imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];

? ? // 是否顯示可選原圖按鈕

? ? imagePickerVc.allowPickingOriginalPhoto = NO;

? ? // 是否允許顯示視頻

? ? imagePickerVc.allowPickingVideo = YES;

? ? // 是否允許顯示圖片

? ? imagePickerVc.allowPickingImage = NO;

? ? imagePickerVc.videoMaximumDuration = 30;//最長拍攝時間

? ? [self presentViewController:imagePickerVc animated:YES completion:nil];

通過第三方選擇的視頻后其實是mov的不是我們想要的格式所以我們要手動轉(zhuǎn)換成mp4的

這個代理方法里獲取到視頻

-(void)imagePickerController:(TZImagePickerController*)picker

?? ? ? didFinishPickingVideo:(UIImage*)coverImage

? ? ? ? ? ? ? ? sourceAssets:(PHAsset*)asset{

?? //導(dǎo)出視頻

? ? [[TZImageManager manager] getVideoOutputPathWithAsset:asset success:^(NSString *outputPath) {

? ? ? ? _videoUrl= outputPath;

? ? ? ?AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:outputPath] options:nil];

? ? ? ? [self choseVedioCompeletWithVedioAsset:urlAsset

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andAVAudioMix:nil

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andVedioInfo:nil

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? andImageSize:CGSizeZero];


? ? }failure:^(NSString*errorMessage,NSError*error) {

? ? ? ? NSLog(@"獲取視頻失敗");

? ? }];

}

然后獲取到他的urlAsset ?vedioInfo?size

- (void)choseVedioCompeletWithVedioAsset:(AVURLAsset*)urlAsset

?? ? ? ? ? ? ? ? ? ? ? ? ? andAVAudioMix:(AVAudioMix*)audioMix

? ? ? ? ? ? ? ? ? ? ? ? ? ? andVedioInfo:(NSDictionary*)vedioInfo

? ? ? ? ? ? ? ? ? ? ? ? ? ? andImageSize:(CGSize)size{


? ? __weak typeof(self) weakSelf = self;

? ? [self convertMovToMp4FromAVURLAsset:urlAsset

? ? ? ? ? ? ? ? ? ? andCompeleteHandler:^(NSURL*_NonnullfileUrl) {

? ? ? ? ? ? ? ? ? ? ? ? [weakSelfuploadVideoWithFileUrl:fileUrl];

? ? ? ? ? ? ? ? ? ? }];

}

獲取到后正式開始轉(zhuǎn)換

- (void)convertMovToMp4FromAVURLAsset:(AVURLAsset*)urlAsset andCompeleteHandler:(void(^)(NSURL*fileUrl))fileUrlHandler{

? ? AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:urlAsset.URL options:nil];

? ? NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

? ? if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) {

? ? ? ? //? 在Documents目錄下創(chuàng)建一個名為FileData的文件夾

? ? ? ? NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"Cache/VideoData"];

? ? ? ? NSFileManager *fileManager = [NSFileManager defaultManager];

? ? ? ? BOOLisDir =FALSE;

? ? ? ? BOOLisDirExist = [fileManagerfileExistsAtPath:pathisDirectory:&isDir];

? ? ? ? if(!(isDirExist && isDir)) {

? ? ? ? ? ? BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

? ? ? ? ? ? if(!bCreateDir){

?? ? ? ? ? ? ? ?NSLog(@"創(chuàng)建文件夾失敶柯丁剿骨!%@",path);

? ? ? ? ? ? }

? ? ? ? ? ? NSLog(@"創(chuàng)建文件夾成功,文件路徑%@",path);

? ? ? ? ? ? //文件路徑

? ? ? ? ? ? vodeopPath= path;

? ? ? ? }

? ? ? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

? ? ? ? [formattersetLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];

? ? ? ? [formattersetDateFormat:@"yyyy_MM_dd_HH_mm_ss"];//每次啟動后都保存一個新的日志文件中

? ? ? ? NSString*dateStr = [formatterstringFromDate:[NSDatedate]];

? ? ? ? NSString*resultPath = [pathstringByAppendingFormat:@"/%@.mp4",dateStr];

? ? ? ? videoName= dateStr;

? ? ? ? NSLog(@"file path:%@",resultPath);

? ? ? ? NSLog(@"resultPath = %@",resultPath);

? ? ? ? AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? presetName:AVAssetExportPresetMediumQuality];

? ? ? ? exportSession.outputURL= [NSURLfileURLWithPath:resultPath];

? ? ? ? exportSession.outputFileType = AVFileTypeMPEG4;

? ? ? ? exportSession.shouldOptimizeForNetworkUse = YES;

? ? ? ?[exportSessionexportAsynchronouslyWithCompletionHandler:^(void)

? ? ? ? {

?? ? ? ? ? ? switch(exportSession.status) {

?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusUnknown:

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(exportSession.outputURL);

? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusUnknown --------%@",exportSession.outputURL);

?? ? ? ? ? ? ? ? ? ? break;

?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusWaiting:

?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusWaiting");

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);

?? ? ? ? ? ? ? ? ? ? break;

?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusExporting:

?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusExporting");

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);

?? ? ? ? ? ? ? ? ? ? break;

?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusCompleted:

?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusCompleted");

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(exportSession.outputURL);

?? ? ? ? ? ? ? ? ? ? break;

?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusFailed:

?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusFailed");

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);

?? ? ? ? ? ? ? ? ? ? break;


?? ? ? ? ? ? ? ? case AVAssetExportSessionStatusCancelled:

?? ? ? ? ? ? ? ? ? ? NSLog(@"AVAssetExportSessionStatusCancelled");

?? ? ? ? ? ? ? ? ? ? fileUrlHandler(nil);

?? ? ? ? ? ? ? ? ? ? break;

?? ? ? ? ? ? }

?? ? ? ? }];

? ? }

}

還有個縮略圖的方法

? //縮略圖

? ? [[TZImageManager manager] getPhotoWithAsset:asset

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? photoWidth:kScreenWidth

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(UIImage*photo,NSDictionary*info,BOOLisDegraded) {

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(!isDegraded) {

}]


其實還有點什么雜七雜八的東西 大家有需要的可以問 我看到了也盡量回答哈

還是那句話大神請繞道 這只是一只菜雞一點想法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末埠褪,一起剝皮案震驚了整個濱河市浓利,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌钞速,老刑警劉巖贷掖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異渴语,居然都是意外死亡苹威,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門驾凶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來牙甫,“玉大人,你說我怎么就攤上這事调违】卟福” “怎么了?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵技肩,是天一觀的道長且轨。 經(jīng)常有香客問我,道長亩鬼,這世上最難降的妖魔是什么殖告? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮雳锋,結(jié)果婚禮上黄绩,老公的妹妹穿的比我還像新娘。我一直安慰自己玷过,他們只是感情好爽丹,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著辛蚊,像睡著了一般绣的。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上阱缓,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天嚎货,我揣著相機與錄音,去河邊找鬼。 笑死碑宴,一個胖子當(dāng)著我的面吹牛软啼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播延柠,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼祸挪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贞间?” 一聲冷哼從身側(cè)響起贿条,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎增热,沒想到半個月后整以,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡峻仇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年悄蕾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片础浮。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡帆调,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出豆同,到底是詐尸還是另有隱情番刊,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布影锈,位于F島的核電站芹务,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鸭廷。R本人自食惡果不足惜枣抱,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望辆床。 院中可真熱鬧佳晶,春花似錦、人聲如沸讼载。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽咨堤。三九已至菇篡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間一喘,已是汗流浹背驱还。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人议蟆。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓灼伤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親咪鲜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

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