iOS以编程方式将AVI转换为MP4格式

我在我的应用程序中有一个查询,因为我想将我的AVI格式的video转换为MP4电影格式,所以有任何方法以编程方式执行此操作。

任何代码段都将不胜感激。

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

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

这里inputURL是需要转换的videourl, outputURL将是video的最终目的地。

还有一件事不要忘记在outputURLvideo文件中指定.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); }]; }