2015-09-04 176 views

回答

4

您需要使用AVAssetExportSession将视频转换为.mp4格式,以下方法将.avi格式视频转换为.mp4

检查行exportSession.outputFileType = AVFileTypeMPEG4;它指定视频的输出格式。

这里inputURL是需要转换的视频网址,outputURL将是视频的最终目的地。

还有一件事别忘了在outputURL视频文件中指定.mp4扩展名。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyVideo.mp4"]; 
NSURL *outputURL = [NSURL fileURLWithPath:filePath]; 

[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession) 
{ 
    if (exportSession.status == AVAssetExportSessionStatusCompleted) { 
     // Video conversation completed 
    }   
}]; 

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler { 
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough]; 
    exportSession.outputURL = outputURL; 
    exportSession.outputFileType = AVFileTypeMPEG4; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { 
     handler(exportSession); 
    }]; 
} 
相关问题