How to compress video in iOS

Hey I am here to introduce video compression in iOS . Sometime we need to compress capture video before uploading it to server.

– (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL


                                     handler:(void (^)(AVAssetExportSession*))handler


    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];

    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];

    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: urlAsset presetName:AVAssetExportPresetLowQuality];

    CMTime startTime = CMTimeMake(0, 1);

    //CMTime stopTime = urlAsset.duration;

    CMTime stopTime = CMTimeMake(urlAsset.duration.value, 1);

    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime);

  session.outputURL = outputURL;

    //session.outputFileType = AVFileTypeQuickTimeMovie;

    session.outputFileType =AVFileTypeMPEG4;

    session.timeRange = exportTimeRange;

    [session exportAsynchronouslyWithCompletionHandler:^(void)





Call this as following

NSURL *uploadURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@”msy.mp4″]];

[self convertVideoToLowQuailtyWithInputURL:[rep url] outputURL:uploadURL handler:^(AVAssetExportSession *session)


                              if (session.status == AVAssetExportSessionStatusCompleted)


              NSString *path = [uploadURL path];

               NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];

               NSLog(@”size after compress video is %lu”,(unsigned long)data);                                                     [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                        // put your code


    }  else{

                   NSLog(@”error on compressing……..”);



Thanks .


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: