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

Tej*_*tel 0 objective-c ios

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

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

Kam*_*pai 5

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

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

inputURL是需要转换的视频的 url,outputURL将是视频的最终目的地。

还有一件事不要忘记.mp4outputURL视频文件中指定扩展名。

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);
     }];
}
Run Code Online (Sandbox Code Playgroud)