Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone:Programmatically compressing recorded video to share?

Tags:

iphone

I have implemented an overlay view when calling camera view before recording the video.

pickerController.cameraOverlayView =myOverlay; 

Video recording and saving the video into Album after recording the video and sharing via email etc. all works fine.

If i use video quality as "High quality", then the recorded video has become huge size. For example, if i record video for 30 seconds with high quality, recorded video has become around 30 - 40 mb.

pickerController.videoQuality = UIImagePickerControllerQualityTypeHigh; 

How do i program to compress the high quality recorded video before sharing it, like how Apple does with built-in Video recorder?

Please guide me to resolve this.

Thanks!

UPDATED:

This is what i'm trying recently, but still no success: I want to compress the recorded video taken which comes to didFinishPickingMediaWithInfo and store in same photo album actual video path itself, not anywhere else. I tested the same video is compressed to very small size when i pick from photo library, but the same video taken from camera and came via didFinishPickingMediaWithInfo is not compressed, though i used the AVAssetExportSession code below.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{      NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];   if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {      NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];     NSString *urlPath = [videoURL path];      if ([[urlPath lastPathComponent] isEqualToString:@"capturedvideo.MOV"])     {         if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (urlPath))         {             [self copyTempVideoToMediaLibrary :urlPath];           }         else         {             NSLog(@"Video Capture Error: Captured video cannot be saved...didFinishPickingMediaWithInfo()");                         }     }            else     {         NSLog(@"Processing soon to saved photos album...else loop of lastPathComponent..didFinishPickingMediaWithInfo()");     } }     [self dismissModalViewControllerAnimated:YES]; }  - (void)copyTempVideoToMediaLibrary :(NSString *)videoURL {          dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  dispatch_async(mainQueue, ^{      ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];      ALAssetsLibraryWriteVideoCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {         NSLog(@"Saved URL: %@", assetURL);         NSLog(@"Error: %@", error);          if (assetURL != nil) {              AVURLAsset *theAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:videoURL] options:nil];              NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:theAsset];              AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:theAsset presetName:AVAssetExportPresetLowQuality];              [exportSession setOutputURL:[NSURL URLWithString:videoURL]];             [exportSession setOutputFileType:AVFileTypeQuickTimeMovie];              [exportSession exportAsynchronouslyWithCompletionHandler:^ {                 switch ([exportSession status]) {                     case AVAssetExportSessionStatusFailed:                         NSLog(@"Export session faied with error: %@", [exportSession error]);                         break;                     default:                         //[self mediaIsReady];                         break;                 }             }];         }     };      [library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoURL] completionBlock:completionBlock]; }); } 
like image 740
Getsy Avatar asked Apr 16 '11 14:04

Getsy


People also ask

How do I compress an iPhone video to share?

There is no built-in iOS feature that allows you to compress a video. However, iPhone users can adjust the size of video recordings in their camera settings before recording. Using a third-party app like Video Compress will allow you to reduce the file size of a video after recording it.

Does iPhone compress video before sending?

They take amazing videos, but did you know that iOS automatically compresses the video files (reduce quality) before it lets you upload to a website or transfer to another device? It does this because most of the time users don't need or want the highest quality videos.

How do I compress a video on my iPhone without losing quality?

The easiest way to make a video smaller on your iPhone is to use a third-party compression app. Video Compress is a free app on the App Store that allows you to compress your videos to make them smaller without affecting the quality.

How is video data compressed?

Video compression tools work by using video encoding algorithms known as codecs. Codecs, by encoding video sequences, reduce the number of bits needed to represent a video. After encoding, the video sequences are wrapped in video containers (such as MP4 and AVI) and then output as compressed videos.


2 Answers

If you want to compress the video for remote sharing and keep the original quality for local storage on the iPhone, you should look into AVAssetExportSession or AVAssetWriter.

Also read up on how iOS manages Assets.

- (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:AVAssetExportPresetLowQuality];     exportSession.outputURL = outputURL;     exportSession.outputFileType = AVFileTypeQuickTimeMovie;     [exportSession exportAsynchronouslyWithCompletionHandler:^(void)      {         handler(exportSession);         [exportSession release];     }]; }  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  {        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];     NSURL *outputURL = [NSURL fileURLWithPath:@"/Users/josh/Desktop/output.mov"];     [self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)      {          if (exportSession.status == AVAssetExportSessionStatusCompleted)          {              printf("completed\n");          }          else          {              printf("error\n");           }      }];  } 
like image 71
tidwall Avatar answered Sep 21 '22 21:09

tidwall


I guess the video is already compressed by the h264 codec. But you can try to use AVFoundation to capture the video files from camera. But I suspect you'll end up with the same file sizes.

Here is some statistics for the 10 seconds video file recorded on the iPhone 4 with different quality pressets.

high (1280х720) = ~14MB = ~11Mbit/s 640 (640х480) = ~4MB = ~3.2Mbit/s medium (360х480) = ~1MB = ~820Kbit/s low (144х192) = ~208KB = ~170Kbit/s 
like image 42
Alexey Kozhanov Avatar answered Sep 22 '22 21:09

Alexey Kozhanov