Exporting AVCaptureSession video in a size that matches the preview layer
Asked Answered
M

1

8

I'm recording video using AVCaptureSession with the session preset AVCaptureSessionPreset640x480. I'm using an AVCaptureVideoPreviewLayer in a non-standard size (300 x 300) with the gravity set to aspect fill while recording. It's setup like this:

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
_previewLayer.frame = _previewView.bounds; // 300 x 300
[_previewView.layer addSublayer:_previewLayer];

After recording the video, I want to write it to a file in quicktime format. During playback, I'm once again playing the video in a 300 x 300 non-standard size layer. Because these videos will ultimately be transferred over a network connection, it seems wasteful to keep the full 640x480 video.

What's the best way to export a video to match my 300 x 300 preview layer? I'm an AVFoundation noob, so if I'm going about this the wrong way please let me know. I just want the recorded video displayed in the preview layer during recording to match the video that is exported on disk.

Mistranslate answered 9/2, 2013 at 1:42 Comment(1)
I think this How to Answer[1] should help u. [1]: https://mcmap.net/q/1326238/-video-capture-with-1-1-aspect-ratio-in-iosFumble
B
13

Video Resolution and Video Size are two different things. Resolution stands for clarity, higher resolution means higher clarity. Whereas, Video size is the bounds in which to display the video. Depending on the video resolution and aspect ratio of the video, the video will stretch or shrink, when seen in the viewer.

Now as the facts have been explained, You can find Your answer here:

How do I use AVFoundation to crop a video

Perform the steps in this order:

  1. Record Video to disk.
  2. Save from Disk to asset library.
  3. Delete from disk.
  4. Perform the steps mentioned in the above link.

NOTE: Perform the steps after recording and writing your video to asset library, saveComposition being the saved asset.

and provide your size in this step:videoComposition.renderSize = CGSizeMake(320, 240); as videoComposition.renderSize = CGSizeMake(300, 300);

And an advice. Since writing the file to disk, then to library, then again back to disk is kind of a lengthy operation. Try doing it all asynchronously using a dispatch_queue or operationBlock

Cheers, Have Fun.

Bendigo answered 11/2, 2013 at 8:42 Comment(3)
I'll mark this as the best answer since it's been helpful, but I still haven't been able to get the linked crop code to work properly.Mistranslate
are you not being able to crop the video at all ?? what is the problem ??Bendigo
I would need to force the live video inside preview layer to have a specific resolution, depending on the screen size of the device because I have an overlay over the live video which displays a marker, which is directly dependent on the resolution of each frame and I don't it to be stretched. Is there a way to force the video to be of a specific resolution?Rotate

© 2022 - 2024 — McMap. All rights reserved.