0

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 ?

Milander
  • 1,021
  • 11
  • 18
  • What is the video codec inside the AVI video? Use a tool like **MediaInfo** (check as `text` mode) if you don't know the AVI details. – VC.One Oct 15 '17 at 10:25
  • All info MediaInfo gives me is: 275 kb/s, 640x480 (4:3), at 10.000 FPS, AVS (Baseline@L3)(1 Ref frame) @VC.One – Milander Oct 16 '17 at 09:57
  • _"**AVS** (Baseline@L3)"_?? Is it typo? If actually you meant **AVC** then you have a compatible video codec for MP4 container... Is there sound too with correct codec (AAC or MP3)? When you say _"exportSession status always fails"_ no other useful error messages? If problem is not video codec, is not audio codec, then problem must be AVI file itself. Camera brand could also be making some weird customized AVI container bytes. – VC.One Oct 16 '17 at 14:54
  • I'm having the same issue. I've used MediaInfo to get info on the .avi, the results are: 7 332kb/s, 1920*1080 (16:9), at 25.000 FPS, AVC (Baseline@L3.1) (CABAC / 1 Ref Frames). The error I get when trying to export the file is "Cannot Open" Did anyone manage to fix this? – Swinny89 Jun 26 '18 at 15:27

1 Answers1

0

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

    }];
}
user5226582
  • 1,946
  • 1
  • 22
  • 37