美文网首页
AVAssetExportSession使用问题

AVAssetExportSession使用问题

作者: 海上飞鸟 | 来源:发表于2020-04-01 16:39 被阅读0次

使用问题

AVAssetExportSession可以对视频进行编辑导出。具体代码如下

AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath] options:nil];
    NSArray *exportArray = [AVAssetExportSession exportPresetsCompatibleWithAsset:urlAsset];
    NSLog(@"exportArray = %@",exportArray);
//    if ([exportArray containsObject:AVAssetExportPresetLowQuality]) {
        AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:urlAsset presetName:AVAssetExportPresetHighestQuality];
        //素材文件夹
        NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents"];
        //先判断有没有DownloadEditFile文件夹
        if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSString * savePath = [filePath stringByAppendingFormat:@"/test.mp4"];
        
        [session setOutputURL:[NSURL fileURLWithPath:savePath]];
        [session setOutputFileType:AVFileTypeQuickTimeMovie];
        [session exportAsynchronouslyWithCompletionHandler:^{
            switch ([session status]) {
                case AVAssetExportSessionStatusCompleted:
                    NSLog(@"转吗完成");
                    break;
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"转吗失败 %@,,,,%@",[[session error] localizedDescription],[session error]);
                default:
                    break;
            }
        }];

容易出错的问题点

1.在创建AVURLAsset时

我们在使用NSURL时,要使用fileURLWithPath创建,不能使用URLWithString进行创建。

2.在设置OutputURL导出录制时

问题同上,也是使用fileURLWithPath

3.很容易忽略的点

就是我们在设置到处文件路径时,要确保路径文件不存在,如果使用已经存在的路径,会照成到处视频失败。
我们可以使用时间戳作为名字这样更安全。

NSString * savePath = [filePath stringByAppendingFormat:@"/%f.mp4",[NSDate date].timeIntervalSince1970*1000];

4.使用AVAssetExportSession可以吧h265编码的视频转化为h264

因为现在有些播放器在播放h265编码格式的视频时,会出现画面或者音频不同步或者卡顿等问题,那我们就可以先通过AVAssetExportSession吧视频素材先转为h264编码格式的,再进行播放等操作就可以了。

相关文章

网友评论

      本文标题:AVAssetExportSession使用问题

      本文链接:https://www.haomeiwen.com/subject/tueauhtx.html