Failed to convert .avi to .mp4 in iOS objective-c

…衆ロ難τιáo~ 提交于 2019-12-11 15:08:20

问题


I am using a camera to record video's and save them into my documents folder. The problem I am facing is that the video's I got from the camera are .avi files and have to be converted to .mp4 (or any other allowed format).

I use the code below:
SOURCE: iOS Convert AVI to MP4 Format programatically

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);
     }];
}

the exportSession status always fails. But when i try to convert a .mov into a .mp4 is does work.

How come I cannot convert .avi files into .mp4 files ?


回答1:


Try to print the error message:

switch ([exportSession status]) {
        case AVAssetExportSessionStatusFailed:
            NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
            break;
        case AVAssetExportSessionStatusCancelled:
            NSLog(@"Export canceled");
            break;
            case AVAssetExportSessionStatusCompleted:
            NSLog(@"Successfully");
            break;
        default:
            break;
        }

    }];
}


来源:https://stackoverflow.com/questions/46724178/failed-to-convert-avi-to-mp4-in-ios-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!