Pergunta

I am newbie and trying to create an app that records video or select video from library, than I am uploading that video to the web. But this files are not playing in some android device. So i want to convert it to mp4 format.

Hence i want to ask that how to convert saved mov to mp4 in objective-C?

FYI i have researched alot but not found any helpful information.

Here's my code:

- (void)openCamera {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;
    picker.showsCameraControls = YES;
    picker.allowsEditing = YES;
    picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    [self presentViewController:picker animated:YES completion:nil];
} 


- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
    {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        videoURL = info[UIImagePickerControllerMediaURL];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
        {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
Foi útil?

Solução

You can check out my so post. It may help you

You are doing right thing.. Now you need to convert this mov file to mp4 as below.

NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
 {
   if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
   {
         NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
         NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
         NSString *videoPath1 =[NSString stringWithFormat:@"%@/xyz.mov",docDir];
         NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
         NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
         [videoData writeToFile:videoPath1 atomically:NO];
       //  UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
   }
 }

    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath1] options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
    {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        videoPath = [NSString stringWithFormat:@"%@/xyz.mp4", [paths objectAtIndex:0]];
        exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
        NSLog(@"videopath of your mp4 file = %@",videoPath);  // PATH OF YOUR .mp4 FILE
        exportSession.outputFileType = AVFileTypeMPEG4;

      //  CMTime start = CMTimeMakeWithSeconds(1.0, 600);
      //  CMTime duration = CMTimeMakeWithSeconds(3.0, 600);           
      //  CMTimeRange range = CMTimeRangeMake(start, duration);            
      //   exportSession.timeRange = range;        
      //  UNCOMMENT ABOVE LINES FOR CROP VIDEO   
        [exportSession exportAsynchronouslyWithCompletionHandler:^{

            switch ([exportSession status]) {

                case AVAssetExportSessionStatusFailed:
                    NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);

                    break;

                case AVAssetExportSessionStatusCancelled:

                    NSLog(@"Export canceled");

                    break;

                default:

                    break;

            }
             UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, nil, nil);
            [exportSession release];

        }];

    }
[self dismissViewControllerAnimated:YES completion:nil];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top