zlyunduan

#import <AVFoundation/AVFoundation.h>

 

- (void)movToMP4:(NSString *)path completionHandler:(void(^)(NSURL * URL))finalUrl{

    

// path文件存放路径

    AVURLAsset *avAsset = [AVURLAssetURLAssetWithURL:[NSURLfileURLWithPath:path] options:nil];

    NSArray *compatiblePresets = [AVAssetExportSessionexportPresetsCompatibleWithAsset:avAsset];

    

    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])

        

    {

        AVAssetExportSession *exportSession = [[AVAssetExportSessionalloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];

        NSString *exportPath = [NSStringstringWithFormat:@"%@/%@.mp4",[NSHomeDirectory() stringByAppendingString:@"/tmp"],@"1"];

        exportSession.outputURL = [NSURLfileURLWithPath:exportPath];

        NSLog(@"exportPath%@", exportPath);

        exportSession.outputFileType = AVFileTypeMPEG4;

        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            

            switch ([exportSession status]) {

                caseAVAssetExportSessionStatusFailed:

                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);

                    break;

                caseAVAssetExportSessionStatusCancelled:

                    NSLog(@"Export canceled");

                    break;

                caseAVAssetExportSessionStatusCompleted:{                    NSLog(@"转换成功");

                    finalUrl(exportSession.outputURL);

                }

                    break;

                default:

                    break;

            }

        }];

    }

}

随笔随记,以免忘记

分类:

技术点:

相关文章: