iosavcapturesessionavcapturedeviceavcapturemoviefileoutput

How can I reduce the file size of a .mov video created with AVCaptureSession?


I am able to record a video using AVCaptureSession.But i want to reduce it size.How can i do this ? I am getting final URL in Delegate "captureOutput" method.

    VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    VideoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error]; 
    if (!error) 
    { 
        if ([CaptureSession canAddInput:VideoInputDevice]) 
        {
            [CaptureSession addInput:VideoInputDevice]; 
        }
        else 
        {
            NSLog(@"Couldn't add video input"); 
        } 
    else 
    { 
        NSLog(@"Couldn't create video input"); 
    } 
} 
else 
{ 
    NSLog(@"Couldn't create video capture device"); 
} 

AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; 

Solution

  • You want to use an AVAssetExportSession. You code will look something like this:

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:myAsset presetName:exportPreset];
    
    exporter.videoComposition = _videoComposition;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.shouldOptimizeForNetworkUse = NO;
    url = [url URLByAppendingPathExtension:CFBridgingRelease(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)(exporter.outputFileType), kUTTagClassFilenameExtension))];
    exporter.outputURL = url;
    
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        // handle the completion in a way meaningful to your app
    }];
    

    You export preset should be one of these:

    AVF_EXPORT NSString *const AVAssetExportPreset640x480           NS_AVAILABLE(10_7, 4_0);
    AVF_EXPORT NSString *const AVAssetExportPreset960x540           NS_AVAILABLE(10_7, 4_0);
    AVF_EXPORT NSString *const AVAssetExportPreset1280x720          NS_AVAILABLE(10_7, 4_0);
    AVF_EXPORT NSString *const AVAssetExportPreset1920x1080         NS_AVAILABLE(10_7, 5_0);
    AVF_EXPORT NSString *const AVAssetExportPreset3840x2160         NS_AVAILABLE(10_10, NA);