AVAssetExportSession 视频转码

来源:互联网 发布:自学c语言 编辑:程序博客网 时间:2024/06/06 04:40
// 视频转码
- (void) convertVideoWithModel:(NSString *) localVideoPath {

    TYWeakify(self, weakSelf);
    
    //转码配置
    NSURL *inputURL = [NSURL fileURLWithPath:localVideoPath];
    
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPreset960x540];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@.mp4",localVideoPath]]; //转码后的视频保存目录
    self.liveShareView.localVideoURL = [NSString stringWithFormat:@"%@.mp4",localVideoPath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        
        int exportStatus = exportSession.status;
        switch (exportStatus) {
                
            case AVAssetExportSessionStatusFailed: {
                
                NSError *exportError = exportSession.error;
                NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                break;
            }
                
            case AVAssetExportSessionStatusCompleted: {
                
                NSLog(@"视频转码成功");
                [weakSelf uploadVideo];
                
            }
        }
    }];
}

原创粉丝点击