iOS 視訊 轉碼MP4 /視訊裁剪 /上傳
///////////////////////////////
//視訊轉碼方法 傳相簿或者相機得到的url和給視訊命名
-(void)shipinZhuanma:(NSURL *)url withName:(NSString *)name{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
[SVProgressHUD showWithStatus:@"處理中..."];
[self creatSandBoxFilePathIfNoExist];
//儲存至沙盒路徑
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
NSString *sandboxPath = [videoPath stringByAppendingPathComponent:name];
//轉碼配置
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
//AVAssetExportPresetMediumQuality可以更改,是列舉型別,官方有提供,更改該值可以改變視訊的壓縮比例
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:sandboxPath];
//AVFileTypeMPEG4 檔案輸出型別,可以更改,是列舉型別,官方有提供,更改該值也可以改變視訊的壓縮比例
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exportSession.status;
NSLog(@"%d",exportStatus);
switch (exportStatus)
{
case AVAssetExportSessionStatusFailed:
{
// log error to text view
NSError *exportError = exportSession.error;
NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
//轉碼失敗
break;
}
case AVAssetExportSessionStatusCompleted:
{
[SVProgressHUD dismiss];
NSData *data = [NSData dataWithContentsOfFile:sandboxPath];
if (data) {
NSString *videoDataString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//把視訊二進位制流 寫入沙盒
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
//字典寫檔案--> ~/Documents/字典
NSDictionary*dic = @{@"shipinName":name,@"shipinDate":videoDataString};
//拼出檔案地址
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飛行圈視訊字典"];
//寫入檔案
[dic writeToFile:dicPath atomically:YES];
}
}
}
}];
}
- (void)creatSandBoxFilePathIfNoExist
{
//沙盒路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSLog(@"databse--->%@",documentDirectory);
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//建立目錄
NSString *createPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
// 判斷資料夾是否存在,如果不存在,則建立
if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
[fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
} else {
NSLog(@"FileImage is exists.");
}
}
//上傳視訊資料方法
- (void)videoGO_FuwuQI{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
[SVProgressHUD showWithStatus:@" "];
// __weak typeof(self) weakSelf = self;
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飛行圈視訊字典"];
//讀出檔案內容
NSDictionary*readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
//////這是視訊上傳伺服器的地址
// NSString *svr = [NSString stringWithFormat:@"%@JieKou/Shipinku_Jiekou/shipingupload.aspx",glaFly_upIMG];
NSString *svr = [NSString stringWithFormat:@"%@%@",glaFly_upIMG,self.shangURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"image/png",nil];
//////這是視訊上傳伺服器的約好的鍵值
NSDictionary *parameters =@{@"videoname":readDic[@"shipinName"],@"videodata":readDic[@"shipinDate"]};
// NSURLSessionDataTask *abc =
[manager POST:svr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
// dispatch_async(dispatch_get_main_queue(), ^{
// [weakSelf.jinDuTiao setProgress:1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount animated:YES];
// weakSelf.jiduLab.text = [NSString stringWithFormat:@"%.2f%",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount * 100];
// });
} success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
// NSLog(@"上傳成功");
[SVProgressHUD dismiss];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD dismiss];
NSLog(@"視訊上傳出錯:%@",error);
}];
}
-(void)shipinCaiJian:(NSURL *)url withName:(NSString *)name start:(CGFloat)startTime end:(CGFloat)endTime{
[self cropWithVideoUrlStr:url withName:name start:startTime end:endTime completion:^(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess) {
}];
}
///////////////////////////////視訊擷取
- (void)cropWithVideoUrlStr:(NSURL *)videoUrl withName:(NSString *)name start:(CGFloat)startTime end:(CGFloat)endTime completion:(void (^)(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess))completionHandle
{
[self creatSandBoxFilePathIfNoExist];
//儲存至沙盒路徑
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *videoPath = [NSString stringWithFormat:@"%@/zhuanMaVideo", pathDocuments];
NSString *sandboxPath = [videoPath stringByAppendingPathComponent:name];
//轉碼配置
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
//AVAssetExportPresetMediumQuality可以更改,是列舉型別,官方有提供,更改該值可以改變視訊的壓縮比例
AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputURL = [NSURL fileURLWithPath:sandboxPath];
//AVFileTypeMPEG4 檔案輸出型別,可以更改,是列舉型別,官方有提供,更改該值也可以改變視訊的壓縮比例
exportSession.outputFileType = AVFileTypeMPEG4;
/////////////////////////////////////////////////
///////////////////////////////-------華麗的分割線-------////////////////////////////////////////////////
/////////////////////////////////////////////////
CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
CMTime duration = CMTimeMakeWithSeconds(endTime - startTime,asset.duration.timescale);
CMTimeRange range = CMTimeRangeMake(start, duration);
exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
[SVProgressHUD dismiss];
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
{
NSLog(@"合成失敗:%@", [[exportSession error] description]);
completionHandle(exportSession.outputURL, endTime, NO);
}
break;
case AVAssetExportSessionStatusCancelled:
{
completionHandle(exportSession.outputURL, endTime, NO);
}
break;
case AVAssetExportSessionStatusCompleted:
{
completionHandle(exportSession.outputURL, endTime, YES);
NSData *data = [NSData dataWithContentsOfFile:sandboxPath];
if (data) {
NSString *videoDataString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//把視訊二進位制流 寫入沙盒
NSString*docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
//字典寫檔案--> ~/Documents/字典
NSDictionary*dic = @{@"shipinName":name,@"shipinDate":videoDataString};
//拼出檔案地址
NSString*dicPath = [docPath stringByAppendingPathComponent:@"飛行圈視訊字典"];
//寫入檔案
[dic writeToFile:dicPath atomically:YES];
}
}
break;
default:
{
completionHandle(exportSession.outputURL, endTime, NO);
} break;
}
}];
}
@end
相關文章
- iOS圖片,視訊上傳&視訊內容旋轉iOS
- iOS 視訊剪下、旋轉,視訊新增音訊、新增水印,視訊匯出iOS音訊
- iOS整合FFmpeg及視訊格式轉碼iOS
- html5播放mp4視訊程式碼HTML
- 多檔案斷點續傳,上傳視訊自動轉MP4和截圖,圖片格式轉換斷點
- IOS音視訊(二)AVFoundation視訊捕捉iOS
- iOS開發 iOS整合FFmpeg及視訊格式轉碼iOS
- android短視訊開發,上傳視訊自動新增水印Android
- 踩坑Webuploader視訊上傳Web
- 視訊硬編碼(iOS端)iOS
- 短視訊商城在ios短視訊開發上的應用效果iOS
- iOS開發系列--音訊播放、錄音、視訊播放、拍照、視訊錄製(轉)iOS音訊
- 短視訊原始碼,視訊轉為圖片儲存原始碼
- iOS - 網易雲信(天天狼人殺誰上麥誰音視訊,多人視訊)iOS
- 編譯並裁剪 FFmpeg 在 Android 上做視訊編輯編譯Android
- 視訊轉碼解決方案
- Compressor視訊轉碼工具
- iOSVideo(視訊)iOSIDE
- iOS視訊播放(二)iOS
- iOS 學習視訊 資料集合 (視訊 +部落格)iOS
- ASP.NET MVC+LayUI視訊上傳ASP.NETMVCUI
- iOS開發:音訊播放、錄音、視訊播放、拍照、視訊錄製iOS音訊
- Compressor for Mac(視訊轉碼工具)Mac
- iOS拍個小視訊iOS
- iOS VR視訊開發iOSVR
- Android 音視訊開發 視訊編碼,音訊編碼格式Android音訊
- 視訊投影(二維視訊投影到三維模型上)模型
- [譯] 優化 MP4 視訊以獲得更快的流傳輸速度優化
- 視訊模組 視訊分析
- 轉載:iOS音視訊實時採集硬體編碼iOS
- Redux 原始碼深度解析(附帶視訊1月14號上傳)Redux原始碼
- python+ffmpeg視訊轉碼轉格式Python
- 視訊轉換工具
- 阿里雲視訊點播轉碼阿里
- Mac視訊轉碼器EditReady for MacMac
- 上傳視訊介面:使用for迴圈,把視訊從本地上傳到伺服器,生成視訊和圖片地址,並儲存到log檔案A1伺服器
- web技術支援| 從視訊元素流式傳輸到視訊元素Web
- 音視訊--視訊入門