4

In my app, the user can either record a video or select one from his library. Naturally, I use a UIImagePickerController with it's sourceType set to either UIImagePickerControllerSourceTypeCamera or UIImagePickerControllerSourceTypePhotoLibrary. The videoQuality is set to UIImagePickerControllerQualityTypeMedium in both cases.

Now, I did the following: I took a 15 sec video with my iPhone lying on it's back, so it's pitch-black. When I choose this video from the library, it is about 0.6 MB big. When I shoot the same video from within my app (15 sec, pitch-black), I get a file of over 4 MB.

Can anybody confirm this? I can hardly believe that I'm the first one to notice but then again, there is not much space for me to screw it up here (which I probably did nonetheless). Or does anybody have an explanation/solution for this?

(I'm at iOS 5.1 with an iPhone 4)

Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52

3 Answers3

0

have you figured it out?

Now I have the same problem, a video in PhotoLibrary(2 minutes more); When I get it using UIImagePickerController, it's just about 30 Mb; But I get by the asset.defaultRepresentation(use this way(Getting video from ALAsset)), it reaches about 300Mb; Maybe the UIImagePickerController compressed the data in some way; I need to figure it out, but make no progress.......

================

EDIT: UIVideoEditorController can compress video to small size; and you can set the videoQuality just like UIImagePickerController.

maybe like this: when you use UIImagePickerController to chose a video, and set allowsEditing=YES, it will present UIVideoEditorController to compress video, then you get the compressed video in small size.

Community
  • 1
  • 1
traximus
  • 177
  • 3
  • 15
  • I find a class UIVideoEditorController(https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIVideoEditorController_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40009027), maybe it can compress the video data. – traximus Mar 26 '13 at 03:23
0

I've figured it out now. The solution is not to decrease the dimensions, but the bitrate. That is I think what Apple is doing when you select a video from the library.

Check out my answer here: https://stackoverflow.com/a/16035330/884119

Community
  • 1
  • 1
Erik
  • 2,138
  • 18
  • 18
-1

Best way to compress the Video.

Here is the code.

       -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 

    NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

   NSData *data = [NSData dataWithContentsOfURL:videoURL];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

             NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
             NSString *caldate = [NSString stringWithFormat:@"%@.mov",[now description]];
            caldate = [caldate stringByReplacingOccurrencesOfString:@" " withString:@""];


             NSString  *documentsDirectory = [paths objectAtIndex:0];
             NSString *path = [NSString stringWithFormat:@"%@/%@", documentsDirectory,caldate];

            NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
            NSURL *selectedVideoUrl;

            if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
                     == kCFCompareEqualTo) {

                               tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
                               selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
                     }

                   NSLog(@"old move %@",path);


        NSURL *url = [NSURL fileURLWithPath:tempFilePath];
        [data writeToFile:path atomically:YES];
        //Delete the original asset
        NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString  *documentsDirectory1 = [paths1 objectAtIndex:0];
        NSString *path1 = [NSString stringWithFormat:@"%@/%@", documentsDirectory1,caldate];
        NSURL *url1 = [NSURL fileURLWithPath:path1];

        NSLog(@"new move %@",path);

        [self convertVideoToLowQuailtyWithInputURL:url outputURL:url1 handler:Nil];
        [picker  dismissModalViewControllerAnimated: YES];

}

    -(void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
                               outputURL:(NSURL*)outputURL
                                 handler:(void (^)(AVAssetExportSession*))handler{

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = outputURL;
if ([[UIApplication sharedApplication]canOpenURL:inputURL]){
    NSLog(@"open");
}
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
NSLog(@"errrsfseSession %@", exportSession.error);
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {
     if(exportSession.status != AVAssetExportSessionStatusCompleted){
         NSLog(@"exportSession  %@", exportSession.error);
     }
     if (exportSession.status == AVAssetExportSessionStatusCompleted)
     {
        NSLog(@"doneszdfsadj");

     }

 }];
    }
ios_av
  • 324
  • 3
  • 8
  • This does not answer the question. Your example just reduces the dimensions of the video. The problem here is that while the dimensions are equal, the video chosen from the library are is much smaller than that recorded within the app. – Erik Apr 13 '13 at 20:56