Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Camera zoom in/out while recording video using AVFoundation

In the latest iOS 7.1 the native camera app can zoom in/out while recording video, and the video saved in the Photos indeed shows the zoom in/out effect.

now, I am using AVFoundation to implement custom Video. I can zoom in/out while recording video by using videoMaxScaleAndCropFactor to modify AVCaptureVideoPreviewLayer. However, the saved video doesn't show the zoom in/out effect.

Is there any hint to implement this function ????

like image 302
Alex Cheng Avatar asked Mar 31 '14 04:03

Alex Cheng


2 Answers

Try this:

float zoomLevel = 2.0f;
float zoomRate = 2.0f;
if ([device respondsToSelector:@selector(rampToVideoZoomFactor:)]
    && device.activeFormat.videoMaxZoomFactor >= zoomLevel) {
  if ([[device lockForConfiguration:nil]) {
  [device rampToVideoZoomFactor:zoomLevel withRate:zoomRate];
  [device unlockForConfiguration];
  }
}

That gives a smooth zoom. For an instant zoom (e.g. responding to small changes caused by a UISlider firing off lots of data) use setVideoZoomFactor: instead of rampToVideoZoomFactor:withRate:.

like image 172
Wildaker Avatar answered Sep 29 '22 14:09

Wildaker


I am also searching for that. The link below gives a solution for zooming video.

http://www.iphonelife.com/blog/87/imaging-video-guru-reporting-lossless-video-zooming-ios7

The logic for zooming video is:

int selectedAVCaptureDeviceFormatIdx = 15;

[self.videoDevice lockForConfiguration:nil];

AVCaptureDeviceFormat* currdf = [self.videoDevice.formats objectAtIndex:selectedAVCaptureDeviceFormatIdx];
self.videoDevice.activeFormat = currdf;
if (selectedAVCaptureDeviceFormatIdx==12 || selectedAVCaptureDeviceFormatIdx==13)
    self.videoDevice.activeVideoMaxFrameDuration = CMTimeMake(1,60);

NSLog(@"%f", self.videoDevice.activeFormat.videoMaxZoomFactor);
NSLog(@"videoZoomFactorUpscaleThreshold: %f", self.videoDevice.activeFormat.videoZoomFactorUpscaleThreshold);

// If you want to zoom to the threshold of possible zooming before binning occurs
self.videoDevice.videoZoomFactor = videoDevice.activeFormat.videoZoomFactorUpscaleThreshold;
// If you want to set your own zoom factor
//self.videoDevice.videoZoomFactor = 3.0f;// here zoom given in CGFloat like 1, 2, 3, 4, 5.

[self.videoDevice unlockForConfiguration:nil];
like image 37
Mitul Bhadeshiya Avatar answered Sep 29 '22 13:09

Mitul Bhadeshiya