iOS 視頻壓縮處理

1.生成保存文件路徑

- (NSString *)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];
    
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];// 用時間, 給文件重新命名, 防止視頻存儲覆蓋,
    [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    //創建目錄
    NSString *createPath = [NSString stringWithFormat:@"%@/Video", pathDocuments];
    // 判斷文件夾是否存在,如果不存在,則創建
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
    } else {
        NSLog(@"FileImage is exists.");
    }
    NSString *resultPath = [createPath stringByAppendingPathComponent:[NSString stringWithFormat:@"outputJFVideo-%@.mov", [formater stringFromDate:[NSDate date]]]];
    NSLog(@"%@",resultPath);
    return resultPath;
}

2.對視頻進行壓縮

- (void)yaSuoShiPinWithfilepath:(NSURL *)filepath {
    NSString *outPath = [self creatSandBoxFilePathIfNoExist];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:filepath options:nil];
    AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputURL = [NSURL fileURLWithPath:outPath];
    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:
            {
                NSLog(@"視頻轉碼成功");
                NSData *data = [NSData dataWithContentsOfFile:outPath];
                imageShowCollectModel *model = [[imageShowCollectModel alloc] init];
                model.picImage = [self firstFrameWithVideoURL:filepath size:CGSizeMake(80, 80)];;
                model.type = @"video";
                model.videoData = data;
                [self.dataSource addObject:model];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissViewControllerAnimated:YES completion:^{}];
                });
            }
        }
    }];
    [self.imageCollectView reloadData];
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容